forked from Git-Hero/Level2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_using_array.c
67 lines (56 loc) · 1.08 KB
/
stack_using_array.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 <stdio.h>
#include <stdlib.h>
#define LIMIT 100
struct stack {
int top;
int item[LIMIT];
};
void push(struct stack *s, int num) {
if (s->top == LIMIT) {
printf("Stack overflow!\n");
exit(EXIT_FAILURE);
}
(s->item)[++(s->top)] = num;
}
void pop(struct stack *s) {
if (s->top == 0) {
printf("Stack underflow!\n");
exit(EXIT_FAILURE);
}
--(s->top);
}
int gettop(struct stack s) {
return s.item[s.top];
}
void printstack(struct stack *s) {
int i = (s->top);
for (; i>=0; i--)
printf("%d ", (s->item)[i]);
printf("\n");
}
int main() {
struct stack s;
s.top=-1;
int n, c;
do {
printf("Enter your choice \n");
printf("1. push\n");
printf("2. pop\n");
printf("3. gettop\n");
printf("4. printstack\n");
scanf("%d", &n);
switch (n) {
case 1 : printf("Enter value "); scanf("%d", &n);
push(&s, n);
break;
case 2 : pop(&s); break;
case 3 : printf("%d is the top element\n", gettop(s));
break;
case 4 : printstack(&s);
break;
default : break;
}
printf("Try again(1/0) ? "); scanf("%d", &c);
} while (c == 1);
return 0;
}