-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
244 lines (191 loc) · 5.25 KB
/
Copy pathmain.cpp
File metadata and controls
244 lines (191 loc) · 5.25 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
using namespace std;
class Stack {
public:
virtual void push(int data) = 0;
virtual int pop() = 0;
virtual int stackTop() = 0;
virtual bool isEmpty() = 0;
virtual bool isFull() = 0;
virtual void display() = 0;
};
class ArrStack : public Stack{
private:
int* stackArr;
int size;
int top =-1;
public:
explicit ArrStack(int size=10){
this->size=size;
stackArr = new int[size];
}
void push(int data){
if(isFull()){
cout<<"Stack Overflow"<<endl;
}
else{
stackArr[++top] = data;
}
}
int pop(){
if(isEmpty())
cout<<"Stack Underflow"<<endl;
else
return stackArr[top--];
}
bool isEmpty(){
return top==-1;
}
bool isFull(){
return top==(size-1);
}
int stackTop(){
if(isEmpty()){
cout<<"Stack is empty"<<endl;
return -1;
}
else
return stackArr[top];
}
void display(){
cout<<endl;
for(int i=top;i>=0;i--)
cout<<stackArr[i]<<endl;
cout<<endl;
}
};
class LinkListStack : public Stack{
class Node{
public:
int data;
Node* next = nullptr;
explicit Node(int data) : data(data) {}
};
private:
Node* head;
public:
explicit LinkListStack(){
head = nullptr;
}
void push(int data){
Node* node = new Node(data);
if(isEmpty())
head = node;
else{
node->next = head;
head=node;
}
}
int pop(){
if(isEmpty()) {
cout << "Stack Underflow" << endl;
return -1;
}
else{
int data = head->data;
head=head->next;
return data;
}
}
int stackTop(){
if(isEmpty()){
cout<<"Stack is Empty"<<endl;
return -1;
}
else return head->data;
}
bool isFull(){
return false;
}
bool isEmpty(){
return head== nullptr;
}
void display(){
cout<<endl;
Node* current = head;
while (current != nullptr) {
cout << current->data << endl;
current = current->next;
}
cout<<endl;
}
};
int timeTakenForPush(Stack& stack, vector<int>& pushInputs){
int time=0;
for(auto data:pushInputs){
// time start
auto start_time = std::chrono::high_resolution_clock::now();
stack.push(data);
// time end
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
time+=duration.count();
}
return time;
}
int timeTakenForPop(Stack& stack,int numOFPops){
int time =0;
for(int i=0;i<numOFPops;i++) {
// time start
auto start_time = std::chrono::high_resolution_clock::now();
stack.pop();
// time end
auto end_time = std::chrono::high_resolution_clock::now();
// calculate time
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
time+=duration.count();
}
return time;
}
int timeTakenForDisplay(Stack& stack){
int time =0;
// time start
auto start_time = std::chrono::high_resolution_clock::now();
stack.display();
// time end
auto end_time = std::chrono::high_resolution_clock::now();
// calculate time
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
time+=duration.count();
return time;
}
vector<int> createRandomArray(int size) {
vector<int> randArray(size);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dis(0, 99);
for (int i = 0; i < size; i++)
randArray[i] = dis(gen);
return randArray;
}
int timeTaken(Stack& stack,vector<int>& pushInputs,int numOfPops){
int timeForPush =timeTakenForPush(stack,pushInputs);
cout<<typeid(stack).name()<<" is displaying after pushing"<<endl;
int timeForDisplay01 = timeTakenForDisplay(stack);
int timeForPop = timeTakenForPop(stack,numOfPops);
cout<<typeid(stack).name()<<" is displaying after popping"<<endl;
int timeForDisplay02 = timeTakenForDisplay(stack);
return (timeForPush+timeForDisplay01+timeForPop+timeForDisplay02);
}
void testStack(int numOFPushes, int numOfPops){
int totalTimeForArrStack=0;
int totalTimeForLinklistStack =0;
for(int i=0;i<10;i++) {
ArrStack arrStack;
LinkListStack linkListStack;
auto pushInputs = createRandomArray(numOFPushes);
auto timeTakenForArrStack = timeTaken(arrStack, pushInputs, numOfPops);
auto timeTakenForLinkListStack = timeTaken(linkListStack, pushInputs, numOfPops);
totalTimeForArrStack+=timeTakenForArrStack;
totalTimeForLinklistStack+=timeTakenForLinkListStack;
}
cout<<"Time taken for Array implementation of Stack : "<< totalTimeForArrStack/10<<endl;
cout<<"Time taken for Linklist implementation of Stack : "<<totalTimeForLinklistStack/10<<endl;
}
int main() {
testStack(10,5);
return 0;
}