forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_1.cpp
More file actions
96 lines (72 loc) · 1.78 KB
/
Exercise_1.cpp
File metadata and controls
96 lines (72 loc) · 1.78 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
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
class Stack {
int top;
int counter = 0;
public:
int a[MAX];// Maximum size of Stack
int size = 0;
Stack() { //Constructor here
}
bool push(int x);
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x){
if(counter<MAX){
a[counter]=x;
// cout<<"counter : "<<counter<<endl;
top=x;
counter++; size++;
// cout<<"Count : "<<counter<<" Size : "<<size<<endl;
return true;
}
cout<<"Stack Overflow"<<endl;
return false;
//Your code here
//Check Stack overflow as well
}
int Stack::pop(){
int ret =0;
if(size==0){
cout<<"Stack Underflow"<<endl;
}else{
counter--;
// cout<<"Pop counter : "<<counter<<endl;
ret = a[counter];
top=a[size-2];
size--;
// cout<<"TOP : "<<top<<endl;
}
return ret;
//Your code here
//Check Stack Underflow as well
}
int Stack::peek() {
if(size==0){
return 0;
}else{
return top;
}
//Your code here
//Check empty condition too
}
bool Stack::isEmpty() {
if(size==0){
return true;
}
return false;
//Your code here
}
// Driver program to test above functions
int main() {
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack\n";
// cout<<s.peek();
return 0;
}