-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPush_Pop.cpp
78 lines (70 loc) · 1.3 KB
/
Push_Pop.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
// PUSH AND POP IN STACK
//ROLL-NO->IIIT18153
#include<bits/stdc++.h>
using namespace std;
struct stackarray{
int capacity;
int top;
int *arr;
};
struct stackarray* createstack(int size)
{
struct stackarray* s=new struct stackarray;
s->capacity=size;
s->top=-1;
s->arr=new int[size];
return s;
}
void push(struct stackarray* s,int data)
{
if(s->top==s->capacity-1)
cout<<"Stack is full.This element cannot be inserted.\n";
else
{
s->top++;
s->arr[s->top]=data;
}
}
int pop(struct stackarray* s)
{
if(s->top==-1)
{
cout<<"Stack is empty\n";
return -1;
}
else
{
int data=s->arr[s->top--];
return data;
}
}
int main()
{
int n;
cin>>n;
struct stackarray* S=createstack(n);
while(1)
{
cout<<"1. For Push:\n";
cout<<"2. For pop\n";
cout<<"3. Exit\n";
int t,data;
cin>>t;
switch (t)
{
case 1:
data;
cin>>data;
push(S,data);
break;
case 2:
data=pop(S);
if(data!=-1)
cout<<data<<endl;
break;
default:
return 0;
}
}
return 0;
}