-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_lab.c
152 lines (140 loc) · 3.82 KB
/
stack_lab.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
#include <stdio.h>
#include <stdlib.h>
/********** STACK DEFINITION **************************************/
#define STACKSIZE 5
struct stack
{
int items[STACKSIZE];
int top;
};
/********** DEFINITION OF PRIMITIVE OPERATIONS : push ,pop**************/
void push(struct stack *s, int x)
{
if (s->top == (STACKSIZE - 1))
{
printf("Stack Overflow\n");
exit(0);
}
s->top++;
s->items[s->top] = x;
}
int pop(struct stack *s)
{
int x;
if (s->top == -1)
{
printf("Stack Underflow!\n");
exit(0);
}
x = s->items[s->top];
s->top--;
return x;
}
int stackTop(struct stack *s)
{
if (s->top == -1)
{
printf("Underflow");
exit(0);
}
return (s->items[s->top]);
}
/*************************** Display function to check the Stack Contenets****/
void display(struct 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("\n%d", s->items[i]);
}
int main()
{
int i, x, choice, flag, y, x1, x2, n;
/*********** Stack and temporary Declaration ******************/
struct stack st, tempst;
/***************** Stack and temporary Intialization : Empty Stack **/
st.top = -1;
tempst.top = -1;
printf("Demonstration of Working of data structure STACK\n\n");
while (1)
{
printf("\n1:PUSH\n2:POP\n3:Display\n4:Third Element from Top\n5:Pop n Elements\n6:Third Element from bottom\n7:exit ");
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("Poped element is %d", x);
break;
case 3:
display(&st);
break;
// Third Element from the top case 4
case 4:
if (st.top >= 2) // atlest 3 elements must be in stack
{
x1 = pop(&st);
x2 = pop(&st);
y = stackTop(&st); // 3 element from top
push(&st, x2);
push(&st, x1);
printf("\nThird Element From Top %d", y);
}
else
{
printf("\nStack has less then 3 elements");
}
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", x);
}
}
else
printf("\n No of elements in stack is less than %d", n);
break;
// Third element from BOTTOM
case 6:
if (st.top >= 2) // atlest 3 elements must be in stack
{
n = st.top - 2;
for (i = 1; i <= n; i++)
{
x = pop(&st); // pop from original stack
push(&tempst, x); // push on temporary stack
}
y = stackTop(&st); // 3rd element from bottom
for (i = 1; i <= n; i++)
{
x = pop(&tempst); // pop from temporary stack
push(&st, x); // push on original stack
}
printf("\nThird Element From Top %d", y);
}
else
printf("\nStack has less then 3 elements");
break;
case 7:
exit(0);
} //switch
} //while
return 0;
} //main