-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
106 lines (95 loc) · 2.09 KB
/
Copy pathStack.c
File metadata and controls
106 lines (95 loc) · 2.09 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
#include <stdio.h>
int top = -1;
int Stack[];
int isFull(int num)
{
if (top == num-1){return 1;}
else {return 0;}
}
int isEmpty(int num)
{
if (top == -1){return 1;}
else {return 0;}
}
void push(int num)
{
if (isFull(num))
{
printf("You can't push values in Stack, otherwise it Overflows\n");
return;
}
else
{
int value;
printf("Enter value to put in Stack : ");
scanf("%d", &value);
top++;
Stack[top] = value;
}
}
void pop(int num)
{
if (isEmpty(num))
{
printf("You can't pop values in Stack, otherwise it Underflows\n");
return;
}
else
{
printf("%d popped from the stack\n", Stack[top]);
top--;
}
}
void peek(int num) {
if (isEmpty(num)) {
printf("Stack is empty\n");
} else {
printf("Top element is: %d\n", Stack[top]);
}
}
void display(int num) {
if (isEmpty(num)) {
printf("Stack is empty\n");
} else {
printf("Stack elements : ");
for (int i = 0; i <= top; i++) {
printf("%d ", Stack[i]);
}
printf("\n");
}
}
int main()
{
int choice, num;
printf("Enter the size of Stack : ");
scanf("%d", &num);
Stack[num];
while (1)
{
printf("\n0. exit()\n1. push()\n2. pop()\n3. peek()\n4. display()\n\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Pushing...\n");
push(num);
break;
case 2:
printf("Popping...\n");
pop(num);
break;
case 3:
printf("Peeking...\n");
peek(num);
break;
case 4:
printf("Displaying...\n");
display(num);
break;
case 0:
printf("Exiting...\n");
return 0;
}
}
return 0;
}