-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
53 lines (41 loc) · 759 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
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
typedef struct stack {
char value;
struct stack *next;
} stack_node_t;
typedef struct {
stack_node_t *top;
} stack_t;
char pop(stack_t *s);
void push(stack_t *s, char c);
int main(void)
{
stack_t s = {NULL};
push(&s, 'A');
push(&s, 'B');
push(&s, 'C');
while(s.top != NULL) {
printf("%c\n", pop(&s));
}
// Push and pop accordingly.
return 0;
}
char pop(stack_t *s)
{
stack_node_t *free_node;
char c;
free_node = s->top;
c = free_node->value;
s->top = free_node->next;
free(free_node);
return c;
}
void push(stack_t *s, char c)
{
stack_node_t *new_node;
new_node = (stack_node_t *) malloc(sizeof(stack_node_t));
new_node->value = c;
new_node->next = s->top;
s->top = new_node;
}