-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
90 lines (82 loc) · 1.78 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
89
90
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 8
#define SCALE 2
#define GROW_CAPACITY(val) (val)*SCALE
#define DEBUG
typedef struct {
int size;
int count;
int *val;
} stack;
void printStack(stack *stk) {
int cnt = stk->count;
while(cnt > 0){
printf(" %d ", stk->val[--cnt]);
}
printf("\n");
}
int pop(stack *stk){
if(stk->count == 0 || stk->size == 0){
fprintf(stderr, "Cannot pop from empty stack.\n");
exit(1);
}
#ifdef DEBUG
printf("POP\n");
stk->count--;
printStack(stk);
return stk->val[stk->count+1];
#endif
return stk->val[stk->count--];
}
void push(stack *stk, int val) {
if(stk->count == stk->size) {
stk->size = GROW_CAPACITY(stk->size);
void *newVal = realloc( (void *)stk->val, stk->size * sizeof(int) );
if(newVal == NULL) {
fprintf(stderr, "Not enough memory.\n");
exit(2);
}
stk->val = newVal;
}
stk->val[stk->count++] = val;
#ifdef DEBUG
printf("PUSH %d\n",val);
printStack(stk);
#endif
}
stack *intStack(int size) {
// INT Stack
stack *stk = (stack *) malloc(sizeof(stack));
stk->val = (int *) malloc(size * sizeof(int));
stk->size = size;
stk->count = 0;
return stk;
}
int main(int argc, char **argv) {
stack *stk = intStack(STACK_SIZE);
push(stk, 1);
push(stk, 2);
push(stk, 3);
push(stk, 4);
push(stk, 5);
push(stk, 6);
push(stk, 7);
push(stk, 8);
push(stk, 9);
push(stk, 10);
push(stk, 11);
pop(stk);
pop(stk);
pop(stk);
pop(stk);
pop(stk);
pop(stk);
pop(stk);
pop(stk);
push(stk, 101);
push(stk, 102);
push(stk, 103);
push(stk, 104);
push(stk, 105);
}