-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
49 lines (45 loc) · 868 Bytes
/
stack.c
File metadata and controls
49 lines (45 loc) · 868 Bytes
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
#include "stack.h"
#include "tree.h"
#include <stdio.h>
#include <stdlib.h>
Stack* createStack(){
Stack* s = (Stack*)malloc(sizeof(Stack));
s->top = NULL;
s->button = NULL;
return s;
}
void freeStack(Stack* s){
SNode* n;
while((n = s->top)){
s->top = n->next;
free(n);
}
}
void push(Stack* s, struct Tree* t, int level){
if(!s){
s = createStack();
}
SNode* n = (SNode*)malloc(sizeof(SNode));
n->tree = t;
n->level = level;
n->next = NULL;
if(!s->button){
s->button = n;
s->top = n;
}else{
n->next = s->top;
s->top = n;
}
}
SNode* pop(Stack* s){
if(!s || !s->button){
return NULL;
}
SNode* n = s->top;
if(s->top == s->button){
s->top = s->button = NULL;
}else{
s->top = n->next;
}
return n;
}