forked from Aryan405/Hacktoberfest2020-Task3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.c
114 lines (106 loc) · 1.81 KB
/
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
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
#include <stdio.h>
#include <stdlib.h>
//Global Variable
int size, choice, ele;
//Creating Stack
struct stack
{
int arr[100];
int top;
} st;
//Inserting Element
void push(int element)
{
if ((st.top) == size)
{
printf("\n Stack is Full");
}
else
{
st.top--;
printf("\nEnter a Value ");
scanf("%d", &ele);
st.arr[st.top] = ele;
}
}
//Removing Element
int pop()
{
if ((st.top) == -1)
{
printf("\nStack is Empty");
}
else
{
int out;
out = st.arr[st.top];
st.top++;
return out;
}
}
//Peek
int peek()
{
int display;
display = st.arr[st.top];
return display;
}
//Display Stack
void display()
{
if ((st.top) >= 0)
{
printf("\n\nElements in the Stack");
int i;
for (i = st.top; i >= 0; i++)
{
printf("\n%d", st.arr[i]);
}
}
else
{
printf("No elements to Display");
}
}
int main()
{
st.top = -1;
printf("Enter a Stack size less than 100 : ");
scanf("%d", &size);
printf("\nStack Operations.....");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.PEEK\n\t 4.DISPLAY\n\t 5.EXIT");
do
{
printf("\nEnter Your Choice ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
push(ele);
break;
}
case 2:
{
printf("%d", pop());
}
case 3:
{
printf("%d", peek());
}
case 4:
{
display();
break;
}
case 5:
{
printf("\n\t EXIT Point");
break;
}
default:
printf("\nEnter a correct choice (1,2,3,4,5)");
}
} while (choice = 5);
return 0;
}