forked from cartoon-raccoon/gdscutm-c-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.c
54 lines (39 loc) · 1.2 KB
/
linkedlist.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
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include "liblinkedlist.h"
void populate_list(linkedlist *list);
// driver code
int main(void)
{
linkedlist *list = linkedlist_create();
// test printing on an empty list
linkedlist_print(list);
// populate the list with numbers
populate_list(list);
// print the new list
linkedlist_print(list);
// try removing an item from the list
int item1 = linkedlist_remove(list, 4);
printf("Removed item at index 4, got %i\n", item1);
// try popping from the list
int item2 = linkedlist_pop(list);
printf("Popped from list, got %i\n", item2);
// try removing from an out of bounds index
int item3 = linkedlist_remove(list, 10);
printf("Out of bounds was handled, got %i\n", item3);
// try an out of bounds index
int *idx = linkedlist_index(list, 10);
if (idx == NULL)
printf("Out of bounds access was handled correctly!\n");
linkedlist_print(list);
linkedlist_destroy(list);
list = NULL;
return 0;
}
void populate_list(linkedlist *list)
{
for (int i = 0; i < 5; i++)
linkedlist_push(list, i + 1);
linkedlist_append(list, 6);
linkedlist_append(list, 7);
}