-
Notifications
You must be signed in to change notification settings - Fork 2
/
StackArray.cpp
104 lines (92 loc) · 1.59 KB
/
StackArray.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
# include<iostream>
using namespace std;
class Stack{
public:
int *arr;
int top;
int array_size;
};
int isEmpty(Stack *S){
if(S->top == -1){
return 1;
}
else{
return 0;
}
}
int isFull(Stack *S){
if(S->top>=S->array_size-1){
return 1;
}
else{
return 0;
}
}
void push(Stack*S){
int val;
if(!isFull(S)){
cout<<"Enter the value : ";
cin>>val;
S->arr[++S->top] = val;
cout<<val<<" is pushed in the stack";
}
else{
cout<<"Overflow!";
}
}
void pop(Stack*S){
int val;
if(!isEmpty(S)){
val = S->arr[S->top--];
cout<<val<<" is popped from the stack";
}
else{
cout<<"Underflow!";
}
}
void peek(Stack*S){
if(!isEmpty(S)){
cout<<S->arr[S->top]<<" is the topmost element of the stack";
}
else{
cout<<"Stack is empty";
}
}
main(){
int n;
Stack *S=NULL;
S = new Stack();
cout<<"Enter the size of the Stack array: ";
cin>>S->array_size;
S->arr = new int(S->array_size);
S->top = -1;
l:
cout<<"\nEnter 1 for push.";
cout<<"\nEnter 2 for pop.";
cout<<"\nEnter 3 for peek.\n";
cin>>n;
switch (n)
{
case 1:
push(S);
break;
case 2:
pop(S);
break;
case 3:
peek(S);
break;
default:
cout<<"Wrong Choice";
break;
}
cout<<"\nDo you want to perform more operations Y/N \n";
char ch;
cin>>ch;
if(ch=='y'){
goto l;
}
if(ch=='n'){
cout<<"\nProgram is ended";
}
}