提交 d6a5520b 编写于 作者: D danghai

Add a simple queue class

上级 ad23bbdf
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
#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 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
#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;
}
CC= g++
CFLAGS = -c -Wall
all: clean main test_stack
all: main test_stack
stack.o: stack.cpp
$(CC) $(CFLAGS) stack.cpp
test_stack: stack.o
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册