-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackclass.cpp
214 lines (175 loc) · 3.91 KB
/
stackclass.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
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
#include "stackclass.h"
#include <iostream>
using namespace std;
class StackUnderFlowException
{
public:
StackUnderFlowException()
{
cout << "Stack underflow" << endl;
}
};
stackclass:: stackclass()
{
top = NULL;
count = 0;
}
void stackclass::Push(event* present)
{
event* temp = new event();
temp->timestamp = present->timestamp;
temp->originalsource = present->originalsource;
temp->nextevent = NULL;
temp->pastevent = top;
//temp->serialnumber = present->serialnumber;
//temp->link = top;
if ( isEmpty() ) {
// Add the first element
top= temp;
}
else {
// Append to the list
top->nextevent = temp;
top = temp;
// cout<<"haha"<<rear->timestamp;
}
//top = temp;
count++;
}
event* stackclass:: Pop()
{
if ( top == NULL )
{
throw new StackUnderFlowException();
}
event* ret = new event;
*ret = *top;
event* tmp = top;
top = top->pastevent;
if(top != NULL)
{
top->nextevent = NULL;
}
//cout<<front->timestamp;
count--;
delete tmp;
return ret;
/*
int ret = top->data;
Node* temp = top->link;
delete top;
top = temp;
count--;
return ret;
*/
}
event* stackclass:: Top()
{
//return top->data;
if ( isEmpty() )
throw new StackUnderFlowException();
//return front->data;
return top;
}
int stackclass:: Size()
{
return count;
}
bool stackclass:: isEmpty()
{
return ( top == NULL ) ? true : false;
}
void stackclass::print()
{
if(count==0)
{
cout<<"Empty"<<endl;
return;
}
event* gg = new event;
gg = top;
for(int kk=1; kk<count+1;kk++)
{
cout<<gg->timestamp<<" ";
gg = gg->pastevent;
}
cout<<endl;
return;
}
void stackclass::deletethis(event* deleteevent)
{
if(deleteevent->nextevent==NULL && deleteevent->pastevent==NULL )
{
//front = NULL;
//rear = NULL;
top = NULL;
count=0;
delete deleteevent;
return;
}
if(deleteevent->nextevent != NULL && deleteevent->pastevent==NULL)
{
//front = front->nextevent;
//front->pastevet=NULL;
deleteevent->nextevent->pastevent = NULL;
delete deleteevent;
count--;
return;
}
if(deleteevent->nextevent == NULL && deleteevent->pastevent != NULL)
{
//rear = rear->pastevent;
//rear->nextevent = NULL;
top = top->pastevent;
top->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;
}
}
/*
int main()
{
ListStack s;
try {
if ( s.isEmpty() )
{
cout << "Stack is empty" << endl;
}
// Push elements
s.Push(100);
s.Push(200);
// Size of stack
cout << "Size of stack = " << s.Size() << endl;
// Top element
cout << s.Top() << endl;
// Pop element
cout << s.Pop() << endl;
// Pop element
cout << s.Pop() << endl;
// Pop element
cout << s.Pop() << endl;
}
catch (...) {
cout << "Some exception occured" << endl;
}
}
*/
/*
OUTPUT:-
Stack is empty
Size of stack = 2
200
200
100
Stack underflow
Some exception occured
*/