diff --git a/linked_list.c b/linked_list.c index be9030151dd807510cae6fdf2ab6f064e9da6b6f..0f7cf7fba2a629ad778f0683295314553fb77767 100644 --- a/linked_list.c +++ b/linked_list.c @@ -2,6 +2,7 @@ #include <stdio.h> #include <stdlib.h> + struct list_item { int value; struct list_item * next; @@ -19,20 +20,30 @@ void append(struct list_item *first, int x){ current->next = new_item; } + void prepend(struct list_item *first, int x){ struct list_item * new_item = (struct list_item *) malloc(sizeof(struct list_item)); - new_item->next = first; + new_item->next = first->next; new_item->value = x; + first->next = new_item; } + void print(struct list_item *first){ struct list_item * current = first; - while (current->next != NULL) { - current = current->next; - printf("%d \n", current->value); + if (current->next != NULL) { + while (current->next != NULL) { + current = current->next; + printf("%d \n", current->value); + } + } + else { + printf("Empty arry \n"); } + printf("\n"); } + void input_sorted(struct list_item *first, int x){ struct list_item * current = first; while ((current->next != NULL) && (current->next->value < x)) { @@ -44,15 +55,17 @@ void input_sorted(struct list_item *first, int x){ current->next = new_item; } + void clear(struct list_item *first){ struct list_item * current; while (first->next != NULL) { - current = first; - first = current->next; + current = first->next; + first->next = current->next; free(current); } } + int main(int argc, char ** argv) { struct list_item root; @@ -64,6 +77,19 @@ int main(int argc, char ** argv) input_sorted(&root, 4); input_sorted(&root, 3); input_sorted(&root, 1); +// print(&root); + + append(&root, 16); + append(&root, 17); + append(&root, 16); +// print(&root); + + prepend(&root, 22); + prepend(&root, 21); + prepend(&root, 20); + + clear(&root); print(&root); + return 0; } diff --git a/ll b/ll new file mode 100755 index 0000000000000000000000000000000000000000..ca338def08a842fb4f9a0a691b01b96c01dc4790 Binary files /dev/null and b/ll differ