-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingArray.c
More file actions
74 lines (67 loc) · 1.38 KB
/
StackUsingArray.c
File metadata and controls
74 lines (67 loc) · 1.38 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
#include<stdio.h>
#define MAX_SIZE 15
int top = -1;
struct stack {
int top;
int array[MAX_SIZE];
} mystack;
void push(int val) {
if (top == MAX_SIZE) {
printf("Stack overflow\n");
return;
}
top++;
mystack.array[top] = val;
return;
}
int pop() {
if (top == -1) {
printf("Stack underflow");
}
int temp = mystack.array[top];
top--;
return temp;
}
int peek() {
if (top == -1) {
printf("Stack Empty");
return -1;
}
int temp = mystack.array[top];
return temp;
}
void display() {
int i;
for (i = 0; i <= top; i++) {
printf("%d\n", mystack.array[i]);
}
}
int main() {
int c = 6, val, ch;
while (c != 0) {
printf("1: push\n2: pop\n3: peek\n4: display\n");
printf("\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter value: ");
scanf("%d", &val);
push(val);
break;
case 2:
printf("%d", pop());
break;
case 3:
printf("%d", peek());
break;
case 4:
display();
break;
default:
printf("Wrong choice");
}
printf("\nTo exit enter 0 ");
scanf("%d", &c);
}
return 0;
}