-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog1.c
168 lines (128 loc) · 3.22 KB
/
prog1.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<stdio.h>
#include<stdlib.h>
#define STACKSIZE 50
struct stack
{
int items[STACKSIZE] ;
int top ;
};
typedef struct stack STACK;
void push(STACK *s, int x)
{
if(s->top == (STACKSIZE-1))
{
printf("Stack Overflow\n");
exit(0);
}
s->items[++ s->top] = x;
}
int pop(struct stack *s)
{
int x;
if(s->top == -1)
{
printf("Stack Underflow!\n");
exit(0);
}
return (s->items[s->top--]);
}
int stackTop(STACK *s)
{
if(s->top == -1)
{
printf("Underflow");
exit(0);
}
return (s->items[s->top]);
}
void display(STACK *s)
{
int i;
if(s->top == -1)
{
printf("Stack Empty!\n");
return;
}
printf("Stack Elements are: ");
for(i = 0; i <= s->top; i++)
{
printf("%d\t",s->items[i]);
}
}
int main()
{
int i, x, choice, flag, Y, x1 ,x2 ,n;
STACK st, tempst;
st.top = -1;
tempst.top = -1;
printf("Stack Basic operation using Array Implementation. \n\n");
while(1)
{
printf("\n\n\n1:PUSH\n2:POP\n3:Display\n4:Third Element from Top\n5:Pop n Elements\n6:Third Element from bottom\n7:exit\n");
printf("\nEnter the Choice :");
scanf("%d", &choice);
switch(choice)
{
case 1:printf("\nEnter the element to be pushed: ");
scanf("%d",&x);
push(&st,x);
break;
case 2:x = pop(&st);
printf("\nPopped element is %d\n",x);
break;
case 3 :display(&st);
break;
case 4:if( st.top >= 2)
{
x1 = pop(&st);
x2 = pop(&st);
Y = stackTop(&st);
push(&st,x2);
push(&st,x1);
printf("Third Element From Top %d.\n",Y);
}
else
{
printf("Stack has less than 3 elements.\n");
}
break;
case 5:printf("Enter the number of elements to be popped:");
scanf("%d",&n);
if( n <= (st.top+1))
{
printf("\npopped elements are : ");
for(i =1;i<=n;i++)
{
x=pop(&st);
printf("%d\t",x);
}
}
else{
printf("Number of elements in stack is less than %d.\n",n);
}
break;
case 6:if( st.top >= 2)
{
n = st.top - 2;
for(i =1;i<=n;i++)
{
x=pop(&st);
push(&tempst,x);
}
Y =stackTop(&st);
for(i =1;i<=n;i++)
{
x=pop(&tempst);
push(&st,x);
}
printf("\nThird Element From Bottom %d\n",Y);
}
else{
printf("\nStack has less then 3 elements\n");
}
break;
case 7:exit(0);
}
}
return 0;
}