File tree Expand file tree Collapse file tree 1 file changed +22
-21
lines changed Expand file tree Collapse file tree 1 file changed +22
-21
lines changed Original file line number Diff line number Diff line change 11#include "sort.h"
22
33/**
4- * insertion_sort_list - sorts a doubly linked list of integers in ascending order.
5- * @list: the list to be sorted.
4+ * insertion_sort_list - sort a doubly linked list of integers in ascending
5+ * order using the insertion sort algorithm
6+ * @list: doubly linked list of integers
7+ * Return: void
68 */
79
810void insertion_sort_list (listint_t * * list )
911{
10- listint_t * current , * tmp ;
12+ listint_t * j , * temp , * curr ;
1113
12- if (list == NULL || * list == NULL )
14+ if (! list || ! * list || !( * list ) -> next )
1315 return ;
1416
15- current = (* list )-> next ;
16- while (current != NULL )
17+ curr = (* list )-> next ;
18+ while (curr )
1719 {
18- tmp = current ;
19- current = current -> next ;
20-
21- while (tmp && tmp -> prev && tmp -> prev -> n > tmp -> n )
20+ j = curr ;
21+ while (j -> prev && j -> n < j -> prev -> n )
2222 {
23- tmp -> prev -> next = tmp -> next ;
24- if (tmp -> next != NULL )
25- tmp -> next -> prev = tmp -> prev ;
26-
27- tmp -> next = tmp -> prev ;
28- tmp -> prev = tmp -> prev -> prev ;
29- tmp -> next -> prev = tmp ;
23+ temp = j -> prev ;
24+ if (temp -> prev )
25+ temp -> prev -> next = j ;
26+ j -> prev = temp -> prev ;
27+ temp -> prev = j ;
28+ temp -> next = j -> next ;
29+ if (j -> next )
30+ j -> next -> prev = temp ;
31+ j -> next = temp ;
3032
31- if (tmp -> prev != NULL )
32- tmp -> prev -> next = tmp ;
33- else
34- * list = tmp ;
33+ if (!j -> prev )
34+ * list = j ;
3535 print_list (* list );
3636 }
37+ curr = curr -> next ;
3738 }
3839}
You can’t perform that action at this time.
0 commit comments