-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
82 lines (70 loc) · 1.59 KB
/
Copy pathstack.cpp
File metadata and controls
82 lines (70 loc) · 1.59 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
#include <iostream>
using namespace std;
#define MAX 10
class Stack{
public:
int arr[MAX];
Stack(){
top = -1;
}
void push(int item){
if(top > MAX -1 ){
cout << "Stack Overflow" << endl;
}
else{
top += 1;
arr[top] = item;
cout << "Element added: " << item << endl;
}
}
int pop(){
if(top < 0){
cout << "Stack Underflow" << endl;
return -1;
}
else{
int item = arr[top--];
return item;
}
}
int peek(){
if(top < 0){
cout << "Stack Underflow" << endl;
}
else{
int item = arr[top];
return item;
}
return 0;
}
void PrintStack(){
for (int i = top; i >= 0; i--){
cout << arr[i] << endl;
}
}
private:
int top;
};
int main(){
Stack st;
int a;
cout << "Enter how many items you want in stack: ";
cin >> a;
for(int i = 0; i < a; i++){
int b;
cout << "Enter element no. " << i << ": ";
cin >> b;
st.push(b);
}
int value = st.pop();
if(value != -1){
cout << "Deleted item: " << value << endl;
}
int top = st.peek();
if(top != -1){
cout << "Top Element: " << top << endl;
}
cout << "Stack: " << endl;
st.PrintStack();
return 0;
}