-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack.c
More file actions
112 lines (99 loc) · 1.79 KB
/
stack.c
File metadata and controls
112 lines (99 loc) · 1.79 KB
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
#include <stdio.h>
#include <stdlib.h>
//Global Variable
int size,choice,element;
//Creating Stack
struct stack{
int arr[100];
int top;
}st;
//Inserting Element
void push(element)
{
if((st.top)==size)
{
printf("\n Stack is Full");
}
else
{
printf("\nEnter a Value ");
scanf("%d",&element);
st.top = st.top + 1;
st.arr[st.top] = element;
}
}
//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)== -1)
{
printf("No elements to Display");
}
else
{
int i;
printf("Elements in the Stack \n");
for(i=st.top; i>=0; --i)
printf("\n%d\n",st.arr[i]);
}
}
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("%d");break;
}
case 2:
{
printf("%d",pop()); break;
}
case 3:
{
printf("%d",peek()); break;
}
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;
}