-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
48 lines (43 loc) · 900 Bytes
/
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
#include "stack.h"
Stack* breakstack;
Stack* contstack;
Stack* loopcounterstack;
Stack* scopeoffsetstack;
void push(Stack** s, int val) {
Stack* new_stack;
new_stack = (Stack*) malloc(sizeof(Stack));
new_stack->val = val;
new_stack->next = NULL;
if (*s == NULL) {
*s = new_stack;
}
else {
new_stack->next = *s;
*s = new_stack;
}
}
int pop(Stack** s) {
int value = 0;
if (*s != NULL) {
value = (*s)->val;
*s = (*s)->next;
}
return value;
}
int pop_and_top(Stack** s) {
if (*s != NULL) {
*s = (*s)->next;
if (*s != NULL) return (*s)->val;
}
return 0;
}
void print_stack(Stack** s) {
Stack* temp;
int i = 0;
temp = *s;
while (temp) {
printf("%d, val:%d\n", i,(*s)->val);
temp = temp->next;
i++;
}
}