blob: 7c83c87a4fe775c3cb12b9afc1552431c7d5c10a (
plain) (
blame)
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
|
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
Stack *stackAlloc() {
Stack *new = malloc(sizeof(Stack));
new->first = NULL;
new->last = NULL;
return new;
}
void stackAdd(Stack *stack, int item) {
struct queueItem *new = malloc(sizeof(struct queueItem));
new->item = item;
if (stack->first) {
// printf("stack->first is NOT NULL");
new->next = NULL;
new->previous = stack->first;
stack->first->next = new;
stack->first = new;
} else {
// printf("stack->first is NULL");
new->next = NULL;
new->previous = NULL;
stack->first = new;
stack->last = new;
}
}
int stackPop(Stack *stack) {
if (!stack->first) {
return -1;
}
int out = stack->first->item;
struct queueItem *toFree = stack->first;
stack->first = toFree->previous;
free(toFree);
return out;
}
void stackFree(Stack *stack) {
struct queueItem *s = stack->first;
struct queueItem *t = NULL;
while (s) {
t = s->previous;
free(s);
s = t;
}
free(stack);
}
|