-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.cpp
54 lines (53 loc) · 798 Bytes
/
Queue.cpp
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
#include "Node.h"
#include <iostream>
template<typename T>
class Queue {
public:
Queue(){
front = rear = NULL;
length = 0;
}
~Queue(){
makeEmpty();
}
void enqueue(T data){
Node<T>* p = new Node<T>(data);
if(front == NULL)
front = p;
else
rear->next = p;
rear = p;
length++;
}
T dequeue(){
if(front == NULL)
return NULL;
Node<T> *p = front;
T d = front->data;
front = front->next;
if(front == NULL)
rear = NULL;
delete p;
length--;
return d;
}
T top(){
if(front == NULL)
return NULL;
return front->data;
}
bool isEmpty(){return front == NULL;}
void makeEmpty(){
Node<T>* p = front;
while(p != NULL){
p = p->next;
delete front;
front = p;
}
rear = NULL;
}
private:
Node<T>* front;
Node<T>* rear;
int length;
};