-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackusingarray.c
More file actions
118 lines (111 loc) · 1.49 KB
/
Copy pathstackusingarray.c
File metadata and controls
118 lines (111 loc) · 1.49 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<stdio.h>
#define N 20
typedef struct stack
{
int a[N];
int top;
}stack;
void push(stack *s,int x)
{
if(s->top==N-1)
printf("\nStack Overflow...");
else
{
s->top=s->top+1;
s->a[s->top]=x;
}
}
int isempty(stack *s)
{
if(s->top==-1)
return 1;
else
return 0;
}
int pop(stack *s)
{
int x;
if(isempty(s))
{
return -1;
}
else
{
x=s->a[s->top];
s->top=s->top-1;
return x;
}
}
int peek(stack *s)
{
if(isempty(s))
return -1;
else
return s->a[s->top];
}
void display(stack *s)
{
int i;
if(isempty(s))
{
printf("\nStack is empty...");
}
else
{
for(i=s->top;i>=0;i--)
{
printf("\t%d",s->a[i]);
}
}
}
int main()
{
int ch,x;
stack s;
s.top=-1;
while(1)
{
printf("\n1:Push\n2:Pop\n3:Peek\n4:Display\n5:Exit\nEnter choice=");
scanf("%d",&ch);
if(ch==5)
break;
switch(ch)
{
case 1:
{
printf("\nEnter element to be pushed=");
scanf("%d",&x);
push(&s,x);
}
break;
case 2:
{
x=pop(&s);
if(x==-1)
printf("\nStack Underflow...");
else
printf("\nPopped Element=%d",x);
}
break;
case 3:
{
x=peek(&s);
if(x==-1)
printf("\nStack is empty...");
else
printf("\nStack top element=%d",x);
}
break;
case 4:
{
display(&s);
}
break;
default:
{
printf("\nInvalid Choice...");
}
}
}
return 0;
}