100f77dae4d59fe4493988aba4ad06d1c005970a
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_sort(link_t
* head
) {
92 link_t
* head_first
= head
;
96 while (head
->next
!= NULL
) {
97 if (head
->value
> head
->next
->value
) {
99 head
->value
= head
->next
->value
;
100 head
->next
->value
= tmp
;
105 /* Reloop at the beginning of the list until there's is values swaped */
111 unsigned list_count(link_t
* head
) {
114 while (head
!= NULL
) {
121 void list_set(link_t
* head
, unsigned index
, int value
) {
124 while (head
!= NULL
&& count
< index
) {
128 if (head
!= NULL
) { head
->value
= value
; }
131 int list_get(link_t
* head
, unsigned index
) {
134 while (head
!= NULL
&& count
< index
) {
145 void list_clear(link_t
* head
) {
146 link_t
* next_link
= NULL
;
148 while (head
!= NULL
) {
149 next_link
= head
->next
;
155 void list_display_values(link_t
* head
) {
158 printf("------Begin------\n");
159 while (head
!= NULL
) {
160 printf("value at [%d]=%d\n", i
, head
->value
);
164 printf("------End------\n");