-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
177 lines (143 loc) · 3.51 KB
/
queue.cpp
File metadata and controls
177 lines (143 loc) · 3.51 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* This file contains an implementation of a Queue - a first in first out (FIFO) data structure
* This contains an iterator as well
* Code by: Humayun Kabir, humayun.k1@gmail.com */
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "node.h"
using namespace std;
//Iterator class
template<class T>
class QueueIter;
//Queue class
template<class T>
class Queue {
friend class QueueIter<T>;
private:
Node<T> *head, *tail;
int Size;
public:
typedef QueueIter<T> iterator;
//begin function
iterator begin() { return iterator(head); }
//end function
iterator end() { return iterator(tail->next); }
//Default constructor
Queue() {
head = tail = NULL;
Size = 0;
}
//inserts a new element into the queue
void Enqueue(T inVal) {
Node<T>* newNode = new Node<T>();
newNode->val = inVal;
newNode->next = NULL;
if( head == NULL ) {
head = tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
Size++;
}
//deletes and returns the oldest element from the queue
T Dequeue() {
try {
if(head != NULL) {
//There is only one element in the queue
if( head == tail ) {
T retVal = head->val;
Node<T>* tempHead = head;
head = tail = NULL;
delete tempHead;
Size--;
return retVal;
}
else { //there are more than one element in the queue
T retVal = head->val;
Node<T>* tempHead = head;
head = head->next;
Size--;
tempHead->next = NULL;
delete tempHead;
return retVal;
}
}
else {
throw string("Queue: Underflow");
}
}
catch(string s) {
cout<<s<<endl;
exit(1);
}
catch(...) {
cout<<"Undefined exception occurred"<<endl;
}
}
//returns number of elements in the queue
int size() { return Size; }
//checks if the queue is empty
bool isEmpty() { return Size == 0; }
//Destructor
~Queue() {
delete head;
}
};
//Queue Iterator class
template<class T>
class QueueIter {
private:
Node<T> *head;
public:
typedef std::forward_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef size_t size_type;
typedef T value_type;
typedef T * pointer;
typedef T & reference;
//Constructor
QueueIter(Node<T> *x = 0) : head(x) {}
//Equality operator
bool operator==(const QueueIter & rhs) {
return head == rhs.head;
}
//not equal to operator
bool operator != (const QueueIter & rhs) {
return !(*this == rhs);
}
//dereference operator
reference operator*() { return head->val; }
//prefix increment operator-- ++iter
QueueIter & operator++(){ head = head->next; return *this; }
//Postfix increment operator-- iter++
QueueIter operator++(int) {
QueueIter clone(*this);
head = head->next;
return clone;
}
};
/*
//Test the data structure
int main() {
Queue<int> aQueue;
srand( time(NULL) );
int length = 5;
//generate 5 random number and insert into queue
for(int i = 0; i < length; i++) {
aQueue.Enqueue( rand() % 100 );
}
//Queue contains
cout<<"The queue contains: \n";
for(Queue<int>::iterator it = aQueue.begin(); it != aQueue.end(); it++)
cout<<*it<<endl;
//Delete an element from the queue
cout<<"Dequeue an element: \n";
cout<< aQueue.Dequeue() <<endl;
//Queue contains
cout<<"Now the queue contains: \n";
for(Queue<int>::iterator it = aQueue.begin(); it != aQueue.end(); it++)
cout<<*it<<endl;
return 0;
}*/