-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
44 lines (34 loc) · 801 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
#include "stack.h"
/*
* [item]-->[item]-->[item]-->[item]-->NULL
* ^ top ^bottom
*/
void *stack_top(Stack **s) {
return (*s) ? (*s)->item : NULL;
}
int stack_empty(Stack **s) {
return (*s) == NULL;
}
/* Push new item onto stack */
void *stack_push(Stack **s, void *item) {
Stack *top;
top = malloc(sizeof(Stack));
if (top == NULL)
return NULL;
top->item = item;
top->next = *s;
*s = top;
return top->item;
}
/* Pop stack and return item */
void *stack_pop(Stack **s) {
Stack *next;
void *item;
if (*s == NULL)
return NULL;
next = (*s)->next;
item = (*s)->item;
free(*s);
*s = next;
return item;
}