aboutsummaryrefslogtreecommitdiff
path: root/linkedlist/stack.c
blob: de77b72715436351f446ca8ad646fc401657cdb9 (plain)
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
#include "stack.h"

void stack_add(void* _data, struct stack** _stackpointer)
{
	struct stack* newsp = malloc(sizeof(*newsp));

	newsp->data = _data;
	newsp->next = *_stackpointer;
	*_stackpointer = newsp;
}

void* stack_get(struct stack** _stackpointer)
{
	void* return_data;
	struct stack* oldsp; //Old stackpointer

	if(!*_stackpointer)
		return NULL;

	return_data = (*_stackpointer)->data;
	oldsp = *_stackpointer;
	*_stackpointer = (*_stackpointer)->next;
	free(oldsp);

	return return_data;
}

void stack_init(struct stack** _stackpointer)
{
	stack_flush(_stackpointer);
	*_stackpointer = NULL;
}

void stack_flush(struct stack** _stackpointer)
{
	while(*_stackpointer)
		stack_get(_stackpointer);
}