-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
85 lines (72 loc) · 1.24 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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "stack.h"
/* returns an empty stack. */
stack * build_stack()
{
/* allocate stack memory */
stack * s = (stack*)malloc(sizeof(stack));
/* initial values */
s->front = 0x00;
s->back = 0x00;
s->size = 0;
return s;
}
/* destroys a stack */
void destroy_stack(stack * s)
{
/* Make sure to clear any data still in the stack */
while(s->size > 0)
{
free(stack_pop(s));
}
free(s);
}
/* add a node to the stack */
void stack_push(stack * s, node * n)
{
/* if stack is empty */
if(s->back == 0x00)
{
s->front = n;
s->back = n;
}
else
{
/* Sanity check */
if(s->front == 0x00)
{
printf("[Queue]:: Error. Back pointer is null in a non-empty stack.\r\n");
}
/* shift the back pointer to the new node */
n->next = s->front;
s->front = n;
}
/* adjust size */
s->size++;
}
/* remove a node from the stack, NULL if empty */
node * stack_pop(stack * s)
{
/* Check if empty, return null if so */
if (s->back == 0x00)
{
return 0x00;
}
/* Pop Node from the front */
node * n = s->front;
/* adjust size */
s->size--;
/*Check if stack is now empty */
if(s->size == 0)
{
s->front = 0x00;
s->back = 0x00;
}
else
{
s->front = n->next;
}
return n;
}