-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathll.c
More file actions
96 lines (88 loc) · 1.95 KB
/
ll.c
File metadata and controls
96 lines (88 loc) · 1.95 KB
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
#include "ll.h"
#include <stdio.h>
#include <stdlib.h>
// More Java-y ☕️
struct ll_node {
int value;
struct ll_node *next;
};
struct ll {
struct ll_node *head;
struct ll_node *tail;
int n_elements;
char *name_of_list;
};
// ☕️
// C-y 🌊
struct ll_t {
int value;
struct ll_t *next;
};
// Insert an element to a linked list.
//
// list may be NULL, in which case a new linked list will be created.
//
// Returns the list that results from inserting the element.
ll_t *ll_insert(ll_t *list, int v) {
// when we insert an element, we will always insert it at the head.
ll_t *new_node = malloc(sizeof(ll_t));
new_node->value = v;
// 1. point to old head; if list is NULL, next is NULL.
new_node->next = list;
// 2. this is the new head
return new_node;
}
// Get the value at index idx.
//
// val will contain the value at index idx, if idx is a valid index.
//
// Returns 0 if idx outside the range of elements in the list.
int ll_at(ll_t *list, int idx, int *val) {
ll_t *cur = list; // current location in the list; start at head
if (idx < 0) {
return 0;
}
int i = 0;
while (cur && i < idx) {
cur = cur->next;
i++;
}
if (!cur) {
// i was too large
return 0;
}
*val = cur->value;
return 1;
/*
// 🤮
for (int i = 0, cur = list; cur && i < idx; i++, cur = cur->next) { }
*/
}
// Print elements in the given list.
void ll_print(ll_t *list) {
printf("[");
for (ll_t *cur = list; cur; cur = cur->next) {
printf("%d%s", cur->value, cur->next ? ", " : "");
}
/* alternatively...
ll_t *cur = list;
while (cur) {
printf("%d%s", cur->value, cur->next ? ", " : "");
cur = cur->next;
}
*/
printf("]\n");
}
// Free the given linked list.
void ll_free(ll_t *list) {
// starting at the head, free each node.
ll_t *cur = list;
while (cur) {
// remember the next node
ll_t *tmp = cur->next;
// free the current node
free(cur);
// advance cur
cur = tmp;
}
}