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
Showing
5 changed files
with
174 additions
and
1 deletion.
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,11 @@ | ||
CC= g++ | ||
CFLAGS = -c -Wall | ||
|
||
all: test_queue | ||
queue.o: queue.cpp | ||
$(CC) $(CFLAGS) queue.cpp | ||
test_queue: queue.o | ||
$(CC) test_queue.cpp queue.o -o queue | ||
|
||
clean: | ||
rm *o queue |
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,90 @@ | ||
#include <iostream> | ||
#include <assert.h> | ||
#include "queue.h" | ||
|
||
using namespace std; | ||
|
||
/* Default constructor*/ | ||
template <class Kind> | ||
queue<Kind>::queue() | ||
{ | ||
queueFront = NULL; | ||
queueRear = NULL; | ||
size = 0; | ||
} | ||
|
||
/* Destructor */ | ||
template <class Kind> | ||
queue<Kind>::~queue() | ||
{ | ||
} | ||
|
||
/* Display for testing */ | ||
template <class Kind> | ||
void queue<Kind>::display() | ||
{ | ||
node<Kind> *current = queueFront; | ||
cout << "Front --> "; | ||
while(current != NULL) { | ||
cout<<current->data<< " "; | ||
current = current -> next; | ||
} | ||
cout <<endl; | ||
cout << "Size of queue: " << size << endl; | ||
} | ||
|
||
/* Determine whether the queue is empty */ | ||
template <class Kind> | ||
bool queue<Kind>::isEmptyQueue() | ||
{ | ||
return (queueFront == NULL); | ||
} | ||
|
||
/* Clear queue */ | ||
template <class Kind> | ||
void queue<Kind>::clear() | ||
{ | ||
queueFront = NULL; | ||
} | ||
|
||
/* Add new item to the queue */ | ||
template <class Kind> | ||
void queue<Kind>::enQueue(Kind item) | ||
{ | ||
node<Kind> *newNode; | ||
newNode = new node<Kind>; | ||
newNode->data = item; | ||
newNode->next = NULL; | ||
if (queueFront == NULL) { | ||
queueFront = newNode; | ||
queueRear = newNode; | ||
} else { | ||
queueRear->next = newNode; | ||
queueRear = queueRear->next; | ||
} | ||
size++; | ||
} | ||
|
||
/* Return the top element of the queue */ | ||
template <class Kind> | ||
Kind queue<Kind>::front() | ||
{ | ||
assert(queueFront != NULL); | ||
return queueFront->data; | ||
} | ||
|
||
/* Remove the element of the queue */ | ||
template <class Kind> | ||
void queue<Kind>::deQueue() | ||
{ | ||
node<Kind> *temp; | ||
if(!isEmptyQueue()) { | ||
temp = queueFront; | ||
queueFront = queueFront->next; | ||
delete temp; | ||
size--; | ||
} else { | ||
cout << "Queue is empty !" << 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 @@ | ||
/* This class specifies the basic operation on a queue as a linked list */ | ||
#ifndef QUEUE_H | ||
#define QUEUE_H | ||
|
||
/* Definition of the node */ | ||
template <class Kind> | ||
struct node | ||
{ | ||
Kind data; | ||
node<Kind> *next; | ||
}; | ||
|
||
/* Definition of the queue class */ | ||
template <class Kind> | ||
class queue | ||
{ | ||
public: | ||
void display(); /* Show queue */ | ||
queue(); /* Default constructor*/ | ||
~queue(); /* Destructor */ | ||
bool isEmptyQueue(); /* Determine whether the queue is empty */ | ||
void enQueue (Kind item); /* Add new item to the queue */ | ||
Kind front(); /* Return the first element of the queue */ | ||
void deQueue(); /* Remove the top element of the queue */ | ||
void clear(); | ||
|
||
private: | ||
node<Kind> *queueFront; /* Pointer to the front of the queue */ | ||
node<Kind> *queueRear; /* Pointer to the rear of the queue */ | ||
int size; | ||
}; | ||
|
||
#endif | ||
|
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,38 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include "queue.h" | ||
#include "queue.cpp" | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
queue<string> q; | ||
cout << "---------------------- Test construct ----------------------" << endl; | ||
q.display(); | ||
cout << "---------------------- Test isEmptyQueue ----------------------" << endl; | ||
if(q.isEmptyQueue()) | ||
cout << "PASS" <<endl; | ||
else | ||
cout << "FAIL" <<endl; | ||
cout << "---------------------- Test enQueue ----------------------" << endl; | ||
cout << "After Hai, Jeff, Tom, Jkingston go into queue: "<<endl; | ||
q.enQueue("Hai"); | ||
q.enQueue("Jeff"); | ||
q.enQueue("Tom"); | ||
q.enQueue("Jkingston"); | ||
q.display(); | ||
cout << "---------------------- Test front ----------------------" << endl; | ||
string value = q.front(); | ||
if (value == "Hai") | ||
cout << "PASS" <<endl; | ||
else | ||
cout << "FAIL" <<endl; | ||
cout << "---------------------- Test deQueue ----------------------" << endl; | ||
q.display(); | ||
q.deQueue(); | ||
q.deQueue(); | ||
cout << "After Hai, Jeff left the queue: "<< endl; | ||
q.display(); | ||
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