-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.c
88 lines (75 loc) · 1.5 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
union Element
{
int i;
float f;
char c;
long l;
double d;
};
typedef struct
{
Element *element;
int top;
int size;
// 0 means Ok, 1 means overflow, -1 means underflow
int flag;
} Stack;
Stack stack(int num) {
Stack stack;
stack.size = num;
stack.element = (Element*) calloc(num, sizeof(Element));
stack.top = -1;
return stack;
}
void push(Stack* stack, Element e) {
if (stack->top > stack->size) {
stack->flag = 1;
}
else if (stack->flag == -1)
stack->flag = 0;
stack->top++;
stack->element[stack->top] = e;
}
Element pop(Stack* stack) {
if (stack->top == -1) {
stack->flag = -1;
}
else if (stack->flag == 1)
stack->flag = 0;
return stack->element[stack->top--];
}
Element top(Stack* stack) {
return stack->element[stack->top];
}
// 1 means true, 0 menas false
int empty(Stack* stack) {
return stack->top == -1 ? 1 : 0;
}
int size(Stack* stack) {
return stack->size;
}
int main()
{
printf("Stack Implementation: \nEnter the size of the stack!");
int n;
scanf("%d", &n);
Stack s = stack(n);
Element e;
e.i = 10;
push(&s, e);
e.i = 20;
push(&s, e);
e.i = 30;
push(&s, e);
printf("%d ", pop(&s));
printf("%d ", pop(&s));
printf("%d ", pop(&s));
printf("\n%d ", empty(&s));
printf("\n%d ", size(&s));
printf("\n%d ", top(&s));
free(s.element);
return 0;
}