Skip to content

Commit

Permalink
ready for sub
Browse files Browse the repository at this point in the history
  • Loading branch information
maalvikabhat authored Mar 27, 2020
1 parent 42f36fd commit 3fb6a17
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions exercises/ex06/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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;
}


Expand All @@ -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;
}


Expand All @@ -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;
}


Expand Down

0 comments on commit 3fb6a17

Please sign in to comment.