1 #include "src/lib/ll.h"
\r
3 void print_list(node_t * head)
\r
5 node_t * current = head;
\r
7 while (current != NULL)
\r
9 printf("%d\n", current->val);
\r
10 current = current->next;
\r
14 void pushe(node_t * head, int val)
\r
16 node_t * current = head;
\r
17 while (current->next != NULL)
\r
19 current = current->next;
\r
22 // now we can add a new variable
\r
23 current->next = malloc(sizeof(node_t));
\r
24 current->next->val = val;
\r
25 current->next->next = NULL;
\r
28 void pushs(node_t ** head, int val)
\r
31 new_node = malloc(sizeof(node_t));
\r
33 new_node->val = val;
\r
34 new_node->next = *head;
\r
38 int pop(node_t ** head)
\r
41 node_t * next_node = NULL;
\r
43 if (*head == NULL) {
\r
47 next_node = (*head)->next;
\r
48 retval = (*head)->val;
\r
55 int remove_last(node_t * head)
\r
60 /* if there is only one item in the list, remove it */
\r
61 if (head->next == NULL) {
\r
67 /* get to the last node in the list */
\r
69 while (current->next->next != NULL) {
\r
70 current = current->next;
\r
73 /* now current points to the last item of the list, so let's remove current->next */
\r
74 retval = current->next->val;
\r
75 free(current->next);
\r
76 current->next = NULL;
\r
81 int remove_by_index(node_t ** head, int n)
\r
85 node_t * current = *head;
\r
86 node_t * temp_node = NULL;
\r
92 for (i = 0; i < n-1; i++) {
\r
93 if (current->next == NULL) {
\r
96 current = current->next;
\r
99 temp_node = current->next;
\r
100 retval = temp_node->val;
\r
101 current->next = temp_node->next;
\r