Commit | Line | Data |
---|---|---|
f8051b35 JB |
1 | #include <stdlib.h> |
2 | ||
3 | #include "clist.h" | |
4 | ||
5 | link_t* list_new(int value) { | |
8f2b2843 JB |
6 | link_t* link_new; |
7 | link_new = malloc(sizeof(link_t)); | |
8 | link_new->value = value; | |
9 | link_new->next = NULL; | |
10 | return link_new; | |
f8051b35 JB |
11 | } |
12 | ||
13 | link_t* list_append(link_t* head, int value) { | |
14 | ||
15 | if (head == NULL) { | |
16 | return head = list_new(value); | |
17 | } else { | |
18 | link_t* head_first = head; | |
19 | while (head->next != NULL) { | |
20 | head = head->next; | |
21 | } | |
22 | head->next = list_new(value); | |
23 | return head_first; | |
24 | } | |
25 | } | |
26 | ||
27 | link_t* list_prepend(link_t* head, int value) { | |
28 | link_t* first_link = list_new(value); | |
29 | ||
30 | first_link->next = head; | |
31 | return first_link; | |
32 | } | |
33 | ||
0d650b41 | 34 | link_t* list_insert(link_t* head, unsigned index, int value) { |
0d650b41 | 35 | |
3de00528 JB |
36 | if (index == 0) { |
37 | return list_prepend(head, value); | |
38 | } else if (index == list_count(head)) { | |
39 | return list_append(head, value); | |
40 | } else { | |
41 | link_t* link_insrt = list_new(value); | |
42 | link_t* head_first = head; | |
43 | link_t* head_next = NULL; | |
44 | for (unsigned i = 0; i < index-1; i++) { | |
45 | head = head->next; | |
46 | } | |
47 | head_next = head->next; | |
48 | head->next = link_insrt; | |
49 | head = link_insrt; | |
50 | head->next = head_next; | |
51 | return head_first; | |
0d650b41 | 52 | } |
0d650b41 JB |
53 | } |
54 | ||
55 | link_t* list_delete(link_t* head, unsigned index) { | |
0d650b41 JB |
56 | link_t* head_prev = NULL; |
57 | link_t* head_next = NULL; | |
74801358 | 58 | link_t* head_ret = NULL; |
0d650b41 JB |
59 | |
60 | if (index == 0) { | |
61 | head_next = head->next; | |
62 | free(head); | |
63 | head = head_next; | |
74801358 | 64 | head_ret = head; |
0d650b41 | 65 | } else { |
3de00528 | 66 | link_t* head_first = head; |
0d650b41 JB |
67 | for (unsigned i = 0; i < index-1; i++) { |
68 | head = head->next; | |
69 | } | |
70 | head_prev = head; | |
71 | head = head->next; | |
72 | head_next = head->next; | |
73 | free(head); | |
74 | head = head_prev; | |
75 | head->next = head_next; | |
74801358 JB |
76 | head_ret = head_first; |
77 | } | |
78 | if (head_ret != NULL) { | |
79 | return head_ret; | |
80 | } else { | |
81 | return NULL; | |
0d650b41 JB |
82 | } |
83 | } | |
84 | ||
f8051b35 | 85 | unsigned list_count(link_t* head) { |
490c6927 | 86 | int count = 0; |
f8051b35 | 87 | |
8f2b2843 | 88 | while (head != NULL) { |
f8051b35 JB |
89 | ++count; |
90 | head = head->next; | |
91 | } | |
92 | return count; | |
93 | } | |
94 | ||
95 | void list_set(link_t* head, unsigned index, int value) { | |
8f2b2843 | 96 | unsigned count = 0; |
f8051b35 | 97 | |
8f2b2843 JB |
98 | while (head != NULL && count < index) { |
99 | ++count; | |
f8051b35 JB |
100 | head = head->next; |
101 | } | |
8f2b2843 | 102 | if (head != NULL) { head->value = value; } |
f8051b35 JB |
103 | } |
104 | ||
105 | int list_get(link_t* head, unsigned index) { | |
8f2b2843 JB |
106 | unsigned count = 0; |
107 | ||
108 | while (head != NULL && count < index) { | |
109 | ++count; | |
110 | head = head->next; | |
111 | } | |
112 | if (head != NULL) { | |
f8051b35 | 113 | return head->value; |
8f2b2843 | 114 | } else { |
f8051b35 JB |
115 | return -1; |
116 | } | |
117 | } | |
118 | ||
74801358 | 119 | void list_clear(link_t* head) { |
8f2b2843 | 120 | link_t* next_link = NULL; |
f8051b35 | 121 | |
74801358 JB |
122 | while (head != NULL) { |
123 | next_link = head->next; | |
124 | free(head); | |
125 | head = next_link; | |
f8051b35 JB |
126 | } |
127 | } |