aboutsummaryrefslogtreecommitdiffstats
path: root/structure.h
blob: 73ed377b4b05386d8cb327d84fdccab683ed0fd5 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * list.h
 * decleration of different lists and corresponding functions
 *
 * Erik Liodden
 */

#ifndef LIST_H
#define LIST_H

/****************************************************************************
 * Stack                                                                    *
 ****************************************************************************/

struct stack {
	void *elems;
	size_t elem_size;
	int log_length;
	int alloc_length;
	void (*freefn)(void *);
};

void stack_init(struct stack *s, size_t elem_size, void (*freefn)(void *));
void stack_dispose(struct stack *s);
void stack_push(struct stack *s, void *elem_addr);
void stack_pop(struct stack *s, void *elem_addr);
void stack_peek(struct stack *s, void *elem_addr);
int stack_length(struct stack *s);

/****************************************************************************
 * Queue                                                                    *
 ****************************************************************************/

struct queue {
	void *elems;
	size_t elem_size;
	int head;
	int tail;
	int log_length;
	int alloc_length;
	void (*freefn)(void *);
};

void queue_init(struct queue *q, size_t elem_size, void (*freefn)(void *));
void queue_dispose(struct queue *q);
void queue_enqueue(struct queue *q, void *elem_addr);
void queue_dequeue(struct queue *q, void *elem_addr);
int queue_length(struct queue *q);




/* linked lists */
struct llist {
	struct llist_node *head;
	struct llist_node *tail;
};

/* linked list node */
struct llist_node {
	struct llist_node *prev;
	struct llist_node *next;
	void *data;
};

/*
 * llist_init:
 * initialise a new linked list
 * returns a pointer to the new list
 */
struct llist *llist_init(void);

/*
 * llist_insert_after:
 * insert new_node after node
 */
void llist_insert_after(struct llist *list, struct llist_node *node,
		struct llist_node *new_node);

/*
 * llist_insert_before:
 * insert new_node before node.
 */
void llist_insert_before(struct llist *list, struct llist_node *node,
		struct llist_node *new_node);

/*
 * llist_insert_beginning:
 * insert new_node at the beginning of the list
 */
void llist_insert_beginning(struct llist *list, struct llist_node *new_node);

/*
 * llist_insert_data_beginning:
 * create a new note at the beginning of the list point to data
 */
void llist_insert_data_beginning(struct llist *list, void *data);

/*
 * llist_insert_int_beginning:
 * create a new note at the beginning of the list point to an int
 */
void llist_insert_int_beginning(struct llist *list, int data);

/*
 * llist_insert_end:
 * insert new_node at the end of the list
 */
void llist_insert_end(struct llist *list, struct llist_node *new_node);

/*
 * llist_insert_data_end:
 * create a new note at the end of the list point to data
 */
void llist_insert_data_end(struct llist *list, void *data);

/*
 * llist_insert_int_end:
 * create a new note at the beginning of the list point to an int
 */
void llist_insert_int_end(struct llist *list, int data);

/*
 * llist_remove:
 * remove node from list and returns a pointer to the removed node
 */
struct llist_node *llist_remove(struct llist *list, struct llist_node *node);

/*
 * llist_delete_node:
 * removes node from list and free the memory allocated by the node
 */
void llist_delete_node(struct llist *list, struct llist_node *node);

/*
 * llist_free_node:
 * assume the node is allocated on the heap
 */
void llist_free_node(struct llist_node *node);

/*
 * llist_free_data_node:
 * assume both node and data are allocated on the heap. frees memory of both
 */
void llist_free_data_node(struct llist_node *node);

/*
 * llist_free_data_list:
 * assume the list is created using llist_insert_int_* or similar; eg all data
 * and nodes are allocated on the heap.
 * the function frees all memory assosiated with the list
 */
void llist_free_data_list(struct llist *list);

#endif /* LIST_H */