7 link_t
* list_new(int value
) {
9 link_new
= malloc(sizeof(link_t
));
10 link_new
->value
= value
;
11 link_new
->next
= NULL
;
15 link_t
* list_append(link_t
* head
, int value
) {
18 return head
= list_new(value
);
20 link_t
* head_first
= head
;
21 while (head
->next
!= NULL
) {
24 head
->next
= list_new(value
);
29 link_t
* list_prepend(link_t
* head
, int value
) {
30 link_t
* first_link
= list_new(value
);
32 first_link
->next
= head
;
36 link_t
* list_insert(link_t
* head
, unsigned index
, int value
) {
37 unsigned max_index
= list_count(head
);
40 return list_prepend(head
, value
);
41 } else if (index
== max_index
) {
42 return list_append(head
, value
);
44 link_t
* link_insrt
= list_new(value
);
45 link_t
* head_first
= head
;
46 link_t
* head_next
= NULL
;
47 for (unsigned i
= 0; i
< index
-1; i
++) {
50 head_next
= head
->next
;
51 head
->next
= link_insrt
;
53 head
->next
= head_next
;
58 link_t
* list_delete(link_t
* head
, unsigned index
) {
59 link_t
* head_prev
= NULL
;
60 link_t
* head_next
= NULL
;
61 link_t
* head_ret
= NULL
;
65 } else if (index
== 0) {
66 head_next
= head
->next
;
71 link_t
* head_first
= head
;
72 for (unsigned i
= 0; i
< index
-1; i
++) {
77 head_next
= head
->next
;
80 head
->next
= head_next
;
81 head_ret
= head_first
;
83 if (head_ret
!= NULL
) {
90 link_t
* list_concat(link_t
* first
, link_t
* second
) {
91 link_t
* head_first
= first
;
93 while (first
->next
!= NULL
) {
100 link_t
* list_sort(link_t
* head
) {
103 link_t
* head_first
= head
;
107 while (head
->next
!= NULL
) {
108 if (head
->value
> head
->next
->value
) {
110 head
->value
= head
->next
->value
;
111 head
->next
->value
= tmp
;
116 /* Reloop at the beginning of the list until there's values swaped */
122 static link_t
* _list_merge_sort(link_t
* head1
, link_t
* head2
) {
123 link_t
* head_result
= NULL
;
131 if (head1
->value
< head2
->value
) {
133 head_result
->next
= _list_merge_sort(head1
->next
, head2
);
136 head_result
->next
= _list_merge_sort(head1
, head2
->next
);
141 link_t
* list_merge_sort(link_t
* head
) {
145 if (head
== NULL
|| head
->next
== NULL
) {
151 while (head2
!= NULL
&& head2
->next
!= NULL
) {
153 head2
= head
->next
->next
;
158 head1
= list_merge_sort(head1
);
159 head2
= list_merge_sort(head2
);
160 return _list_merge_sort(head1
, head2
);
163 unsigned list_count(link_t
* head
) {
166 while (head
!= NULL
) {
173 void list_set(link_t
* head
, unsigned index
, int value
) {
176 while (head
!= NULL
&& count
< index
) {
180 if (head
!= NULL
) { head
->value
= value
; }
183 int list_get(link_t
* head
, unsigned index
) {
186 while (head
!= NULL
&& count
< index
) {
197 void list_clear(link_t
* head
) {
198 link_t
* next_link
= NULL
;
200 while (head
!= NULL
) {
201 next_link
= head
->next
;
207 void list_display_values(link_t
* head
) {
210 printf("------Begin------\n");
211 while (head
!= NULL
) {
212 printf("value at [%d]=%d\n", i
, head
->value
);
216 printf("------End------\n");