diff --git a/data_structure/queue/makefile b/data_structure/queue/makefile new file mode 100644 index 000000000..420909804 --- /dev/null +++ b/data_structure/queue/makefile @@ -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 diff --git a/data_structure/queue/queue.cpp b/data_structure/queue/queue.cpp new file mode 100644 index 000000000..728adfc18 --- /dev/null +++ b/data_structure/queue/queue.cpp @@ -0,0 +1,90 @@ +#include +#include +#include "queue.h" + +using namespace std; + +/* Default constructor*/ +template +queue::queue() +{ + queueFront = NULL; + queueRear = NULL; + size = 0; +} + +/* Destructor */ +template +queue::~queue() +{ +} + +/* Display for testing */ +template +void queue::display() +{ + node *current = queueFront; + cout << "Front --> "; + while(current != NULL) { + cout<data<< " "; + current = current -> next; + } + cout < +bool queue::isEmptyQueue() +{ + return (queueFront == NULL); +} + +/* Clear queue */ +template +void queue::clear() +{ + queueFront = NULL; +} + +/* Add new item to the queue */ +template +void queue::enQueue(Kind item) +{ + node *newNode; + newNode = new node; + 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 +Kind queue::front() +{ + assert(queueFront != NULL); + return queueFront->data; +} + +/* Remove the element of the queue */ +template +void queue::deQueue() +{ + node *temp; + if(!isEmptyQueue()) { + temp = queueFront; + queueFront = queueFront->next; + delete temp; + size--; + } else { + cout << "Queue is empty !" << endl; + } +} + diff --git a/data_structure/queue/queue.h b/data_structure/queue/queue.h new file mode 100644 index 000000000..715def1ef --- /dev/null +++ b/data_structure/queue/queue.h @@ -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 +struct node +{ + Kind data; + node *next; +}; + +/* Definition of the queue class */ +template +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 *queueFront; /* Pointer to the front of the queue */ + node *queueRear; /* Pointer to the rear of the queue */ + int size; +}; + +#endif + diff --git a/data_structure/queue/test_queue.cpp b/data_structure/queue/test_queue.cpp new file mode 100644 index 000000000..caf80318c --- /dev/null +++ b/data_structure/queue/test_queue.cpp @@ -0,0 +1,38 @@ +#include +#include +#include "queue.h" +#include "queue.cpp" + +using namespace std; + +int main() +{ + queue q; + cout << "---------------------- Test construct ----------------------" << endl; + q.display(); + cout << "---------------------- Test isEmptyQueue ----------------------" << endl; + if(q.isEmptyQueue()) + cout << "PASS" <