From 3fb6a17c79b972e962811f3aeda3de8ea5ca7ad6 Mon Sep 17 00:00:00 2001 From: Maalvika Bhat <42943695+maalvikabhat@users.noreply.github.com> Date: Thu, 26 Mar 2020 20:19:00 -0400 Subject: [PATCH] ready for sub --- exercises/ex06/list.c | 44 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/exercises/ex06/list.c b/exercises/ex06/list.c index 9d658bb1..ab2e0061 100644 --- a/exercises/ex06/list.c +++ b/exercises/ex06/list.c @@ -54,8 +54,13 @@ void print_list(Node **list) { * returns: int or -1 if the list is empty */ int pop(Node **list) { - // FILL THIS IN! - return 0; + Node *head = *list; + if (head->next != NULL) { + int result = head->val; + *list = head->next; + return result; + } + return -1; } @@ -65,7 +70,8 @@ int pop(Node **list) { * val: value to add */ void push(Node **list, int val) { - // FILL THIS IN! + Node *new = make_node(val, *list); + *list = new; } @@ -79,8 +85,21 @@ void push(Node **list, int val) { * returns: number of nodes removed */ int remove_by_value(Node **list, int val) { - // FILL THIS IN! - return 0; + Node *current = *list; + + if (current == NULL) { + return 0; + } + + while (current->next->val != val) { + if (current->next->next == NULL) { + return 0; + } + current = current->next; + } + free(current->next); + current->next = current->next->next; + return 1; } @@ -91,7 +110,20 @@ int remove_by_value(Node **list, int val) { * list: pointer to pointer to Node */ void reverse(Node **list) { - // FILL THIS IN! + Node *current; + Node *next = NULL; + Node *prev = NULL; + + current = *list; + + while (current != NULL) { + next = current->next; + current->next = prev; + prev = current; + current = next; + } + + *list = prev; }