-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
853413b
commit 9ea7803
Showing
12 changed files
with
560 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <iostream> | ||
#include "ChainNode.h" | ||
|
||
ChainNode::ChainNode(int e, ChainNode *p) | ||
{ | ||
element = e; | ||
next = p; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#if !defined(_CHAINNODE_H) | ||
#define _CHAINNODE_H | ||
|
||
#include <iostream> | ||
|
||
class ChainNode | ||
{ | ||
public: | ||
int element; | ||
ChainNode *next; | ||
public: | ||
ChainNode(int e = 0, ChainNode *p = NULL); | ||
}; | ||
|
||
#endif // _CHAINNODE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/*************************************************************************************** | ||
* ID: 65037743 | ||
* NAME: Mr.Kidsadakorn Nuallaoong | ||
* DESCRIPTION: ArrayStack Testing | ||
***************************************************************************************/ | ||
#include <iostream> | ||
#include "ChainNode.h" | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
ChainNode a; // * ==> e = 0, p = NULL | ||
ChainNode b(10, &a); // * ==> e = 10, p = &a | ||
ChainNode c(20, &b); // * ==> e = 20, p = &b | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
run: | ||
@g++ -c main.cpp -o main.o -g | ||
@g++ -c ChainNode.cpp -o ChainNode.o -g | ||
@g++ main.o ChainNode.o -o LabW3 -g | ||
|
||
@./LabW3 | ||
|
||
@rm -f *.o LabW3 | ||
|
||
all: main.o ChainNode.o | ||
g++ main.o ChainNode.o -o week4 -g | ||
|
||
main.o: main.cpp ChainNode.cpp | ||
g++ -c main.cpp -o main.o -g | ||
g++ -c ChainNode.cpp -o ChainNode.o -g | ||
|
||
clean: | ||
rm -f *.o LabW3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "FastArrayQueue.h" | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
FastArrayQueue::FastArrayQueue(int maxSize) | ||
{ | ||
this->maxSize = maxSize; | ||
curSize = 0; | ||
in = 0; | ||
out = 0; | ||
L = new int[this->maxSize]; | ||
} | ||
|
||
FastArrayQueue::~FastArrayQueue() | ||
{ | ||
delete []L; | ||
} | ||
|
||
void FastArrayQueue::enQueue(int e) | ||
{ | ||
// * validate (queue Full) | ||
if(curSize >= maxSize){ | ||
cerr << "Error : Queue is full..." << endl; | ||
return; | ||
} | ||
|
||
// * enqueue alg.(example) | ||
L[in] = e; | ||
in = (in + 1) % maxSize; | ||
|
||
curSize++; | ||
} | ||
|
||
int FastArrayQueue::deQueue() | ||
{ | ||
// * validate (queue empty) | ||
if(curSize <= 0){ | ||
cerr << "Error : Queue is Empty..." << endl; | ||
return -1; | ||
} | ||
|
||
// * dequeue alg.(example) | ||
int t = L[out]; | ||
L[out] = 0; | ||
out = (out+1) % maxSize; | ||
curSize--; | ||
|
||
return t; | ||
} | ||
|
||
int FastArrayQueue::front() | ||
{ | ||
// * validate (queue empty) | ||
if(curSize <= 0){ | ||
cerr << "Error : Queue is Empty..." << endl; | ||
return -1; | ||
} | ||
|
||
// * get front (example) | ||
|
||
return L[out]; | ||
} | ||
|
||
int FastArrayQueue::rear() | ||
{ | ||
// * validate (queue empty) | ||
if(curSize <= 0){ | ||
cerr << "Error : Queue is Empty..." << endl; | ||
return -1; | ||
} | ||
|
||
// * get front (example) | ||
|
||
return L[((in-1) + maxSize) % maxSize]; | ||
} | ||
|
||
void FastArrayQueue::display() | ||
{ | ||
// * Queue. {e0, e1, en...,} curSize/maxSize | ||
cout << "Q : {"; | ||
if(curSize == 0){ | ||
cout << "}"; | ||
} else { | ||
for(int i = 0; i < curSize; i++){ | ||
cout << L[(out + i) % maxSize]; | ||
if(i < curSize-1){ | ||
cout << ", "; | ||
} else { | ||
cout << "}"; | ||
} | ||
} | ||
} | ||
cout << ", " << "Size : " << curSize << "/" << maxSize << ", in : " << in << ", out : " << out << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#if !defined(_FASTARRAYQUEUE_H) | ||
#define _FASTARRAYQUEUE_H | ||
|
||
#define DEFAULT_MAX_SIZE 5 | ||
|
||
class FastArrayQueue | ||
{ | ||
private: | ||
int *L; | ||
int curSize; | ||
int maxSize; | ||
int in; | ||
int out; | ||
|
||
|
||
public: | ||
FastArrayQueue(int maxSize = DEFAULT_MAX_SIZE); // * constructor | ||
~FastArrayQueue(); | ||
|
||
// * method | ||
void enQueue(int e); | ||
int deQueue(); | ||
int front(); | ||
int rear(); | ||
|
||
void display(); | ||
|
||
void append(int e){ enQueue(e); }; | ||
int remove(){ return deQueue(); }; | ||
int head(){ return front(); }; | ||
int back(){ return rear(); }; | ||
}; | ||
|
||
#endif // _FASTARRAYQUEUE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*************************************************************************************** | ||
* ID: 65037743 | ||
* NAME: Mr.Kidsadakorn Nuallaoong | ||
* DESCRIPTION: FastArrayQueue | ||
***************************************************************************************/ | ||
#include <iostream> | ||
#include "FastArrayQueue.h" | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
FastArrayQueue Queue; | ||
|
||
int choice, index, element, round; | ||
|
||
do | ||
{ | ||
cout << "----------------- MENU -----------------" << endl | ||
<< endl; | ||
|
||
cout << "================ DISPLAY ===============" << endl | ||
<< endl; | ||
|
||
Queue.display(); | ||
|
||
cout << endl | ||
<< "========================================" << endl | ||
<< endl; | ||
|
||
cout << "1: Add Queue, enQueue()" << endl; | ||
cout << "2: Get first , deQueue" << endl; | ||
cout << "3: Show front Queue, front()" << endl; | ||
cout << "4: shoe last Queue, rear()" << endl; | ||
|
||
cout << "0: Exit!!!!" << endl; | ||
|
||
cout << endl; | ||
|
||
cout << "Enter your choice : "; | ||
cin >> choice; | ||
|
||
switch (choice) | ||
{ | ||
case 1: // * enQueue | ||
cout << "[Massage] Enter element : "; | ||
cin >> element; | ||
Queue.enQueue(element); | ||
break; | ||
|
||
case 2: // * pop | ||
element = Queue.deQueue(); | ||
cout << "[Massage] get element : " << element << endl; | ||
break; | ||
|
||
case 3: // * top | ||
element = Queue.front(); | ||
cout << "[Massage] front element : " << element << endl; | ||
break; | ||
|
||
case 4: // * top | ||
element = Queue.rear(); | ||
cout << "[Massage] Last element : " << element << endl; | ||
break; | ||
|
||
case 0: | ||
cout << "Bye...." << endl | ||
<< endl; | ||
cout << "-------------- Call method --------------" << endl; | ||
break; | ||
|
||
default: // * Exit | ||
cout << "[Massage] Wrong choice,try again..." << endl; | ||
} | ||
|
||
} while (choice != 0); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
run: | ||
@g++ -c main.cpp -o main.o | ||
@g++ -c FastArrayQueue.cpp -o FastArrayQueue.o | ||
@g++ main.o FastArrayQueue.o -o FastArrayQueue | ||
|
||
@./FastArrayQueue | ||
|
||
@rm -f *.o FastArrayQueue | ||
|
||
all: main.o FastArrayQueue.o | ||
g++ main.o FastArrayQueue.o -o FastArrayQueue -g | ||
|
||
main.o: main.cpp | ||
g++ -c main.cpp -o main.o | ||
|
||
FastArrayQueue.o: FastArrayQueue.cpp | ||
g++ -c FastArrayQueue.cpp -o FastArrayQueue.o | ||
|
||
clean: | ||
rm -f *.o FastArrayQueue |
Oops, something went wrong.