## Week-09 Laboratory Exercises

### Exercise-01: Return How Many Even Numbers are in a Linked List (individual)

Sample solution for count_even_list.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
struct node *next;
int          data;
};

struct node *strings_to_list(int len, char *strings[]);

int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);

printf("%d\n", result);

return 0;
}

// return the number of even values in a linked list
int num_even = 0;
while (p != NULL) {
if (p->data % 2 == 0) {
num_even = num_even + 1;
}
p = p->next;
}
return num_even;
}

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->data = atoi(strings[i]);
}
}


Alternative solution for count_even_list.c
#include <stdio.h>

struct node {
struct node *next;
int          data;
};

// return the number of even values in a linked list
// cute, recursive solution
return 0;
} else {
}
}



### Exercise-02: Delete First Element of a Linked List (individual)

Sample solution for list_delete_first.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
struct node *next;
int          data;
};

struct node *strings_to_list(int len, char *strings[]);

int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);

return 0;
}

// Delete the first node in list.
// The deleted node is freed.
// The head of the list is returned.

struct node *delete_first(struct node *head) {
// list is empty no node to delete
return NULL;
}
}

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->data = atoi(strings[i]);
}
}

printf("[");

for (struct node *n = head; n != NULL; n = n->next) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
}
printf("]\n");
}



### Exercise-03: Delete First Element Containing A Value from a Linked List (pair)

Sample solution for list_delete_contains.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
struct node *next;
int          data;
};

struct node *delete_contains(int value, struct node *head);
struct node *strings_to_list(int len, char *strings[]);

int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s value list-elements\n", argv[0]);
return 1;
}
int value = atoi(argv[1]);
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 2, &argv[2]);

return 0;
}

// Delete the first node in the list containing i
// The deleted node is freed.
// If no node contains i, the list is not changed
// The head of the list is returned.

struct node *delete_contains(int value, struct node *head) {
// list is empty no node to delete
return NULL;
}
}
}

// find node before first node containing i
while (n->next->next != NULL && n->next->data != value) {
n = n->next;
}

if (n->next->data == value) {
struct node *new_next = n->next->next;
free(n->next);
n->next = new_next;
}
}

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->data = atoi(strings[i]);
}
}

printf("[");

for (struct node *n = head; n != NULL; n = n->next) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
}
printf("]\n");
}


Alternative solution for list_delete_contains.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
struct node *next;
int          data;
};

// Delete the first node in list the containing value - recursive version
// The deleted node is freed.
// If no node contains i, the list is not changed
// The head of the list is returned.

struct node *delete_contains(int value, struct node *head) {
return NULL;
}
}
}



### Exercise-04: Remove Consecutive Repeated Values From List (pair)

Sample solution for list_delete_repeated.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
struct node *next;
int          data;
};

struct node *strings_to_list(int len, char *strings[]);

int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, argv + 1);

return 0;
}

// Delete any consecutive repeated values from a linked list - recursive version.
// Deleted nodes are freed.
// The head of the list is returned.

struct node *delete_repeated(struct node *head) {
// list of length < 2 does not need to be changed
}

while (p->next != NULL) {
if (p->data == p->next->data) {
struct node *repeated = p->next;
p->next = p->next->next;
free(repeated);
} else {
p = p->next;
}
}

}

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->data = atoi(strings[i]);
}
}

printf("[");

for (struct node *n = head; n != NULL; n = n->next) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
}
printf("]\n");
}


Alternative solution for list_delete_repeated.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
struct node *next;
int          data;
};

// Delete any consecutive repeated values from a linked list - recursive version.
// Deleted nodes are freed.
// The head of the list is returned.

struct node *delete_repeated(struct node *head) {
}
}
}



### Exercise-05: Challenge Exercise: Reverse a Linked List (individual) [Challenge Exercise]

Sample solution for list_reverse.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
struct node *next;
int          data;
};

struct node *strings_to_list(int len, char *strings[]);

int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);

return 0;
}

// Place the list into reverse order.
// The head of the list is returned.

struct node *reverse(struct node *head) {
return NULL;
}
struct node *previous = NULL;
while (x != NULL) {
struct node *y = x->next;
x->next = previous;
previous = x;
x = y;
}
return previous;
}

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->data = atoi(strings[i]);
}
}

printf("[");

for (struct node *n = head; n != NULL; n = n->next) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
}
printf("]\n");
}


Alternative solution for list_reverse.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
struct node *next;
int          data;
};

// Place the list into reverse order.
// The head of the list is returned.

struct node *reverse(struct node *head) {
// lists of 0 or 1 node don't need to be changed
}

//reverse rest of list