-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
71 lines (71 loc) · 1.4 KB
/
Copy pathstack.c
File metadata and controls
71 lines (71 loc) · 1.4 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
#include <stdio.h>
int n;
int stack[20];
int top = -1;
void push(int x)
{
if (top == n - 1)
{
printf("Stack is full!\n");
return;
}
stack[++top] = x;
}
void pop()
{
if (top == -1)
{
printf("Stack is empty!\n");
return;
}
printf("Popped element: %d\n", stack[top--]);
}
void display()
{
if (top == -1)
{
printf("Stack is empty!\n");
return;
}
printf("Stack elements: ");
for (int i = 0; i <= top; i++)
{
printf("%d ", stack[i]);
}
printf("\n");
}
int main()
{
printf("Enter the size of the stack:");
scanf("%d",&n);
int choice, x;
while (1)
{
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter element to push:");
scanf("%d", &x);
push(x);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Code executed successfully");
return 0;
default:
printf("Invalid choice!\n");
}
}
return 0;
}