-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
72 lines (65 loc) · 1.12 KB
/
stack.c
File metadata and controls
72 lines (65 loc) · 1.12 KB
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
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
void init_st(Stack** s)
{
*s = (Stack*)malloc(sizeof(**s));
(*s)->top = NULL;
(*s)->len = 0;
}
void free_st(Stack** s)
{
node_sq* iter = (*s)->top, * aux;
while (iter!=NULL) {
if(iter){
aux = iter->next;
free_node(iter);
iter = aux;
}
}
free(*s);
}
int add_stack(Stack* s, int node_num)
{
node_sq* new = (node_sq*)malloc(sizeof(*new));
if(new==NULL){
return 0;
}
new->data = node_num;
if (is_empty_st(s)) {
new->next = NULL;
s->top = new;
}
else {
new->next = s->top;
s->top = new;
}
s->len++;
return 1;
}
int pop_stack(Stack*s)
{
if(is_empty_st(s)) {
return -1;
}
node_sq* nd = s->top;
int aux = nd->data;
nd = s->top;
s->top=s->top->next;
s->len--;
free(nd);
return aux;
}
void print_st(Stack *s)
{
node_sq* iter = s->top;
while(iter){
printf("%3d ", iter->data);
iter=iter->next;
}
puts("");
}
int is_empty_st(Stack*s)
{
return s->top==NULL;
}