-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueclass.cpp
More file actions
222 lines (165 loc) · 3.9 KB
/
Copy pathQueueclass.cpp
File metadata and controls
222 lines (165 loc) · 3.9 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//this is the implementation of Queue with event class and it works
#include "Queueclass.h"
#include <iostream>
//#include "event.h"
using namespace std;
class QueueisEmptyError
{
public:
QueueisEmptyError()
{
cout << "Queue is empty error" << endl;
}
};
Queue::Queue()
{
front = NULL;
rear = NULL;
count = 0;
}
void Queue::Enqueue(event* present)
{
// Create a new node
event* tmp = new event();
tmp->timestamp= present->timestamp;
tmp->originalsource = present->originalsource;
tmp->nextevent = NULL;
tmp->pastevent = rear;
//tmp->serialnumber = present->serialnumber;
if ( isEmpty() ) {
// Add the first element
front = rear = tmp;
}
else {
// Append to the list
rear->nextevent = tmp;
rear = tmp;
// cout<<"haha"<<rear->timestamp;
}
count++;
}
event* Queue::Dequeue()
{
if ( isEmpty() )
throw new QueueisEmptyError();
//int ret = front->data;
//Node* tmp = front;
/* this works -- the next code snippet works too gosh
event* ret = new event;
ret->timestamp = front->timestamp;
ret->originalsource = front->originalsource;
ret->nextevent = front->nextevent;
ret->pastevent = front->pastevent;
ret->serialnumber = front->serialnumber;
*/
event* ret = new event;
*ret = *front;
//cout <<ret->timestamp;
event* tmp = front;
//cout<<tmp->timestamp;
// Move the front pointer to next node
front = front->nextevent;
//cout<<front->timestamp;
if(front ==NULL)
{
rear = NULL;
count--;
return ret;
}
front->pastevent = NULL;
count--;
delete tmp;
return ret;
}
event* Queue::Front()
{
if ( isEmpty() )
throw new QueueisEmptyError();
//return front->data;
return front;
}
int Queue::Size()
{
return count;
}
bool Queue::isEmpty()
{
return count == 0 ? true : false;
}
void Queue::print()
{
// vector<int>::iterator pos = heap.begin();
//vector<float>::iterator pos = heap.begin();
// cout << "Heap = ";
// while ( pos != heap.end() ) {
// cout << *pos << " ";
// ++pos;
// }
//cout << endl;
if(count==0)
{
cout<<"Empty"<<endl;
return;
}
event* tt = new event;
tt = front;
if(tt == NULL)
{
cout<<"Empty"<<endl;
}
for(int kk=1; kk<count+1;kk++)
{
cout<<tt->timestamp<<" ";
tt = tt->nextevent;
}
cout<<endl;
return;
}
void Queue::deletethis(event* deleteevent)
{
// ideally i should search for this deleteevent -
// but i will assume that the user has already found it
if(deleteevent->nextevent==NULL && deleteevent->pastevent==NULL )
{
front = NULL;
rear = NULL;
count=0;
delete deleteevent;
return;
}
if(deleteevent->nextevent != NULL && deleteevent->pastevent==NULL)
{
front = front->nextevent;
front->pastevent=NULL;
delete deleteevent;
count--;
//event* ret = new event;
//*ret = *front;
//cout <<ret->timestamp;
// event* tmp = front;
//cout<<tmp->timestamp;
// Move the front pointer to next node
// front = front->nextevent;
//cout<<front->timestamp;
//count--;
//delete tmp;
//return ret;
return;
}
if(deleteevent->nextevent == NULL && deleteevent->pastevent != NULL)
{
rear = rear->pastevent;
rear->nextevent = NULL;
delete deleteevent;
count--;
return;
}
if(deleteevent->nextevent != NULL && deleteevent->pastevent != NULL)
{
deleteevent->pastevent->nextevent = deleteevent->nextevent;
deleteevent->nextevent->pastevent = deleteevent->pastevent;
delete deleteevent;
count--;
return;
}
}