-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.c
67 lines (62 loc) · 1.55 KB
/
Stack.c
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
#include<conio.h>
#include<stdio.h>
#define MAX 5
void push(int );
void pop();
void stackTop();
void display();
int tos = -1, stack[MAX], i = 0;
int main() {
int choice, data;
do {
printf("\nEnter choice\n1. Push\n2. Pop\n3. Top of Stack\n4. Display\n5. Exit\t");
scanf("%d", &choice);
switch(choice) {
case 1: printf("\nEnter your data : ");
scanf("%d", &data);
push(data);
display();
break;
case 2: pop();
display();
break;
case 3: stackTop();
break;
case 4: display();
break;
case 5: //exit(0);
break;
default: printf("\nEnter a valid choice!");
}
}
while(choice != 5);
return 0;
}
void push(int info) {
if(tos == (MAX-1))
printf("\nStack Overflow!");
else
stack[++tos] = info;
}
void pop() {
if(tos == -1)
printf("\nStack Underflow!");
else
printf("\nElement Deleted : %d", stack[tos--]);
}
void stackTop() {
if(tos == -1)
printf("\nStack Underflow!");
else
printf("\nTop of Stack is %d", stack[tos]);
}
void display() {
if(tos == -1)
printf("\nStack Underflow!");
else {
for(i=tos ; i>=0 ; i--) {
printf("\n\t\t\t\t\t%d", stack[i]);
printf("\n\t\t\t\t____________");
}
}
}