-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
76 lines (71 loc) · 1.12 KB
/
stack.c
File metadata and controls
76 lines (71 loc) · 1.12 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
#include<stdio.h>
#include <stdlib.h>
#define max 50
int stack[max],top=-1;
void push()
{
int elem;
if(top==max-1)
{
printf("Stack over flow!");
return;
}
printf("Enter the elements to be pushed");
scanf("%d",&elem);
top++ ; //move to top
stack[top]=elem; //push operation taken place
printf("%d Pushed to the stack.\n",elem);
}
void pop()
{
if (top==-1)
{
printf("The stack isunder flow! No elements in the stack.\n");
return;
}
printf("Popped element:%d\n",stack[top]);
top--;
}
void display()
{
int i;
if (top == -1) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements are:\n");
for (i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
int main()
{
int ch;
while(1)
{
printf("\n---Stack Menu---\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit (0);
default:
printf("Invalid entry please provide a valid input");
}
}
return 0;
}