mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 10:35:34 +08:00
Add a simple queue class
This commit is contained in:
11
data_structure/queue/makefile
Normal file
11
data_structure/queue/makefile
Normal file
@@ -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
data_structure/queue/queue.cpp
Normal file
90
data_structure/queue/queue.cpp
Normal file
@@ -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
data_structure/queue/queue.h
Normal file
34
data_structure/queue/queue.h
Normal file
@@ -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
data_structure/queue/test_queue.cpp
Normal file
38
data_structure/queue/test_queue.cpp
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user