-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_array.cpp
More file actions
91 lines (80 loc) · 1.48 KB
/
stack_array.cpp
File metadata and controls
91 lines (80 loc) · 1.48 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
#include <bits/stdc++.h>
#include <iostream>
#include<vector>
#include<chrono>
#include <cstdlib>
#include <ctime>
using namespace std;
using namespace std::chrono;
class Stack{
private:
int top;
int size =20;
int *stack;
public :
Stack(){
top = -1;
//size = size;
stack = new int[size];
}
void push( int val){
top = top+1;
if(top >size){
cout<<"Error:Stack Overflow"<<endl;
}
else{
stack[top] = val;
}
}
int pop(){
if(top <0){
cout <<"Error StackUnderflow"<<endl;
}
else{
top = top -1;
return stack[top+1];
}
}
bool is_empty(){
return top ==-1;
}
bool is_full ()
{
return top == size;
}
void display(){
for (int i= 0; i<top+1; i++){
cout << stack[i] <<" ";
}
cout <<endl;
}
};
int main(){
srand(time(0));
auto start = high_resolution_clock::now();
Stack S;
S.push(8);
S.push(9);
S.push(10);
S.push(6);
S.push(4);
S.push(3);
S.push(2);
S.push(1);
S.push(7);
S.push(12);
S.display();
for(int i=0; i<5;i++){
S.pop();
}
S.display();
S.push(5);
S.push(30);
S.push(20);
S.display();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
double time = duration.count();
cout << "time taken for the recursive algorithm: " << time << " micro seconds" << endl;
return 0;
}