-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6_stack.c
80 lines (79 loc) · 1.19 KB
/
6_stack.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
//Aim:To implement push and pop in C on an user given stack.
//Code:
#include<stdio.h>
#include<stdlib.h>
#define size 5
int Top=-1, inp_array[size];
void Push();
void Pop();
int main()
{
int choice;
while(1)
{
printf("Operations:");
printf("\n1.Push \n2.Pop\n3.End");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: exit(0);
default: printf("\nInvalid choice!!");
}
}
}
void Push()
{
int x;
if(Top==size-1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter element to be inserted to the stack:");
scanf("%d",&x);
Top=Top+1;
inp_array[Top]=x;
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("Elements present in the stack after pushing element: \n");
for(int i=Top;i>=0;--i)
printf("%d\n",inp_array[i]);
}
}
}
}
void Pop()
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d",inp_array[Top]);
Top=Top-1;
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nElements present in the stack after popping the element:\n");
for(int i=Top;i>=0;--i)
printf("%d\n",inp_array[i]);
}
}
}
}