forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
90 lines (80 loc) · 1.65 KB
/
queue.cpp
File metadata and controls
90 lines (80 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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;
}
}