-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackAsarray.c
82 lines (80 loc) · 1.04 KB
/
stackAsarray.c
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
#include<stdio.h>
#define MAXS 3
struct stack
{
int A[MAXS];
int top;
};
void initialize(struct stack *s)
{
s->top=-1;
}
void push(struct stack *s,int x)
{
if(s->top==MAXS)
printf("stack overflow\n");
else
{
s->top++;
s->A[s->top]=x;
printf("Inserted\n");
}
}
int pop(struct stack *s)
{
int x;
if(s->top==-1)
{
printf("stack underflow\n");
return -1;
}
x=s->A[s->top];
s->top--;
return x;
}
void display(struct stack s)
{
int i;
i=s.top;
while(i!=-1)
{
printf("%d ",s.A[i]);
i--;
}
}
int main()
{
struct stack s;
int ch,x;
initialize(&s);
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Exit\n");
do
{
printf("\nEnter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value\n");
scanf("%d",&x);
push(&s,x);
break;
case 2:
x=pop(&s);
if(x!=-1)
{
printf("deleted value=%d\n",x);
}
break;
case 3:
display(s);
break;
default:
printf("you entered wrong choice :( \n");
}
}while(ch<4);
return 0;
}