aboutsummaryrefslogtreecommitdiff
path: root/linkedlist/list.c
blob: 701cd5f756c39af8d64a6642cb8ee54027dd65bb (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
39
40
41
42
43
44
45
46
#include "list.h"

void list_init(struct list** _listroot)
{
	if(*_listroot)
		while(list_popFront(_listroot));

	*_listroot = NULL;
}

void list_add(int _data, struct list** _listroot)
{
	struct list **current = _listroot;

	while(*current)
		current = &(*current)->next;

	*current = malloc(sizeof(**current));
	(*current)->data = _data; 
}

int list_popFront(struct list** _listroot)
{
	if(!*_listroot)
		return 0;
	
	int data = (*_listroot)->data;
	struct list *oldRoot = *_listroot;

	*_listroot = (*_listroot)->next;
	free(oldRoot);

	return data;
}

int list_get(int _index, struct list** _listroot)
{
	struct list* current = *_listroot;

	for(int i = 0; i < _index; i++)
	{
		current = current->next;
	}

	return current->data;
}