2020-01-17 07:21:12 +08:00
|
|
|
#include "queue.h"
|
2020-05-30 07:26:30 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <iostream>
|
2020-01-17 07:21:12 +08:00
|
|
|
|
|
|
|
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 --> ";
|
2020-05-30 07:26:30 +08:00
|
|
|
while (current != NULL)
|
|
|
|
{
|
|
|
|
cout << current->data << " ";
|
|
|
|
current = current->next;
|
2020-01-17 07:21:12 +08:00
|
|
|
}
|
2020-05-30 07:26:30 +08:00
|
|
|
cout << endl;
|
2020-01-17 07:21:12 +08:00
|
|
|
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;
|
2020-05-30 07:26:30 +08:00
|
|
|
if (queueFront == NULL)
|
|
|
|
{
|
2020-01-17 07:21:12 +08:00
|
|
|
queueFront = newNode;
|
|
|
|
queueRear = newNode;
|
2020-05-30 07:26:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-17 07:21:12 +08:00
|
|
|
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;
|
2020-05-30 07:26:30 +08:00
|
|
|
if (!isEmptyQueue())
|
|
|
|
{
|
2020-01-17 07:21:12 +08:00
|
|
|
temp = queueFront;
|
|
|
|
queueFront = queueFront->next;
|
|
|
|
delete temp;
|
|
|
|
size--;
|
2020-05-30 07:26:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-17 07:21:12 +08:00
|
|
|
cout << "Queue is empty !" << endl;
|
|
|
|
}
|
|
|
|
}
|