summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code/ads/ll/ll.c
blob: ba0a0df92dfdeb30d1ff52d2d3b9f7354b11e4f3 (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
47
48
49
50
51
52
53
54
55
56
57
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct ll_t ll_t;

typedef struct ll_t {
	void *data;
	ll_t *next;
} ll_t;

ll_t *llalloc(){
	ll_t *output = malloc(sizeof(ll_t));
	if (output == NULL)
		return NULL;
	output->next = NULL;
	return output;
}

void llsetdata(ll_t *node, void *data){
	node->data = malloc(sizeof(data));
	memcpy(node->data, data, sizeof(data));
}

void llsetnext(ll_t *node, ll_t *next){
	if (node->next == NULL)
		node->next = next;
	else
		llsetnext(node->next, next);
}

void *llgetat(ll_t *head, int index){
	if (index == 0)
		return head->data;
	else {
		if (head->next != NULL)  {
			return llgetat(head->next, index - 1);
		}else {
			return NULL;
		}
	}
}

ll_t *llgetendnode(ll_t *head){
	if (head->next == NULL)
		return head;
	else 
		return llgetendnode(head->next);
}

void llfreeall(ll_t *head){
	if (head->next != NULL)
		llfreeall(head->next);
	free(head->data);
	free(head);
}