-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackUsingStruct.c
100 lines (90 loc) · 1.9 KB
/
stackUsingStruct.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int s[10];
int top;
};
//Function prototypes
int push (struct stack* st, int item, int size);
int pop (struct stack* st, int size);
int display(struct stack* st);
int main()
{
int choice, item, size;
struct stack st;
st.top=-1;
printf("Enter the stack size: \n");
scanf ("%d", &size);
while(1)
{
printf("Enter choice, 1->Push, 2->Pop, 3->Display, 4->Exit:\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter the element to push: \n");
scanf("%d", &item);
if (push(&st, item, size))
printf("Pushed %d.\n", item);
break;
case 2: item = pop(&st, size);
if(item)
printf("Popped item is %d.\n", item);
break;
case 3: display(&st);
break;
case 4: exit(0);
}
}
return 1;
}
int push(struct stack* st, int item, int size)
{
if (st->top==size-1)
{
printf("Stack overflow!");
return (0);
}
else
{
st->top++;
st->s[st->top]=item;
return (1);
}
}
int pop (struct stack* st, int size)
{
int item;
if(st->top == -1)
{
printf("Stack underflow!");
return (0);
}
else
{
item = st->s[st->top];
st->top--;
return (item);
}
}
int display (struct stack* st)
{
int n;
if (st->top==-1)
{
printf("Stack underflow!");
return (0);
}
else
{
n=st->top;
printf("Stack content: ");
while(n>-1)
{
printf("%d, ", st->s[n]);
n--;
}
printf("\n");
return (1);
}
}