-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path03-Stack (Array).c
More file actions
75 lines (64 loc) · 1007 Bytes
/
03-Stack (Array).c
File metadata and controls
75 lines (64 loc) · 1007 Bytes
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
//Stack
#include <stdio.h>
#include<stdlib.h>
#define size 20
int a[size], top=-1;
void display()
{
printf("\nThe stack is:\n");
if(top==-1)
{
printf("empty\n");
return;
}
for(int i=0;i<=top;i++)
printf("%d ",a[i]);
printf("\n");
}
void push(int n)
{
if(top==n-1)
printf("Stack overflow\n");
else
{
top++;
printf("\nEnter the element to push\n");
scanf("%d", &a[top]);
display();
}
}
void pop()
{
if(top==-1)
printf("Stack underflow\n");
else
{
printf("\nPopped element %d from stack\n", a[top]);
top--;
display();
}
}
void main()
{
int n, choice;
printf("Enter the stack size\n");
scanf("%d", &n);
while(1)
{
printf("\nStack Menu\n__________\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: push(n);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("Invalid choice\n");
}
}
}