Skip to content

Commit

Permalink
Add a simple queue class
Browse files Browse the repository at this point in the history
  • Loading branch information
danghai committed Jan 16, 2020
1 parent ad23bbd commit d6a5520
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 1 deletion.
11 changes: 11 additions & 0 deletions 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
90 changes: 90 additions & 0 deletions data_structure/queue/queue.cpp
@@ -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;
}
}

34 changes: 34 additions & 0 deletions 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 <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

38 changes: 38 additions & 0 deletions data_structure/queue/test_queue.cpp
@@ -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;
}
2 changes: 1 addition & 1 deletion data_structure/stk/makefile
@@ -1,7 +1,7 @@
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
Expand Down

0 comments on commit d6a5520

Please sign in to comment.