2fd3dfd98f734570e2551f6a774d4af9eae9a62d
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
) {
39 return list_prepend(head
, value
);
40 } else if (index
== list_count(head
)) {
41 return list_append(head
, value
);
43 link_t
* link_insrt
= list_new(value
);
44 link_t
* head_first
= head
;
45 link_t
* head_next
= NULL
;
46 for (unsigned i
= 0; i
< index
-1; i
++) {
49 head_next
= head
->next
;
50 head
->next
= link_insrt
;
52 head
->next
= head_next
;
57 link_t
* list_delete(link_t
* head
, unsigned index
) {
58 link_t
* head_prev
= NULL
;
59 link_t
* head_next
= NULL
;
60 link_t
* head_ret
= NULL
;
64 } else if (index
== 0) {
65 head_next
= head
->next
;
70 link_t
* head_first
= head
;
71 for (unsigned i
= 0; i
< index
-1; i
++) {
76 head_next
= head
->next
;
79 head
->next
= head_next
;
80 head_ret
= head_first
;
82 if (head_ret
!= NULL
) {
89 link_t
* list_concat(link_t
* first
, link_t
* second
) {
90 link_t
* head_first
= first
;
92 while (first
->next
!= NULL
) {
99 link_t
* list_sort(link_t
* head
) {
102 link_t
* head_first
= head
;
106 while (head
->next
!= NULL
) {
107 if (head
->value
> head
->next
->value
) {
109 head
->value
= head
->next
->value
;
110 head
->next
->value
= tmp
;
115 /* Reloop at the beginning of the list until there's values swaped */
121 static link_t
* _list_merge_sort(link_t
* head1
, link_t
* head2
) {
122 link_t
* head_result
= NULL
;
130 if (head1
->value
< head2
->value
) {
132 head_result
->next
= _list_merge_sort(head1
->next
, head2
);
135 head_result
->next
= _list_merge_sort(head1
, head2
->next
);
140 link_t
* list_merge_sort(link_t
* head
) {
144 if (head
== NULL
|| head
->next
== NULL
) {
150 while (head2
!= NULL
&& head2
->next
!= NULL
) {
152 head2
= head
->next
->next
;
157 head1
= list_merge_sort(head1
);
158 head2
= list_merge_sort(head2
);
159 return _list_merge_sort(head1
, head2
);
162 unsigned list_count(link_t
* head
) {
165 while (head
!= NULL
) {
172 void list_set(link_t
* head
, unsigned index
, int value
) {
175 while (head
!= NULL
&& count
< index
) {
179 if (head
!= NULL
) { head
->value
= value
; }
182 int list_get(link_t
* head
, unsigned index
) {
185 while (head
!= NULL
&& count
< index
) {
196 void list_clear(link_t
* head
) {
197 link_t
* next_link
= NULL
;
199 while (head
!= NULL
) {
200 next_link
= head
->next
;
206 void list_display_values(link_t
* head
) {
209 printf("------Begin------\n");
210 while (head
!= NULL
) {
211 printf("value at [%d]=%d\n", i
, head
->value
);
215 printf("------End------\n");