### Revision Exercise: Length of a Linked List

A linked list is another way of representing a collection of data of the same type.

Linked lists are made up of nodes, which store one element of the list each, as well as the location (in memory) of the next element.

Nodes can store data of any type, but for this week we will be looking at nodes which store integers, which are defined like this:

```struct node {
struct node *next;
int          data;
};
```
```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_length/list_length.c .
```
``````// Return the length of the linked list pointed by head

// PUT YOUR CODE HERE (change the next line!)
return 42;

}

``````
length is given one argument, head, which is the pointer to the first node in a linked list.

Add code to length so that its returns the length of the list.

For example if the linked list contains these 8 elements:

```1, 7, 8, 9, 13, 19, 21, 42
```

length should return 8.

#### Testing

list_length.c also contains a main function which allows you to test your length function.

This main function:

• converts the command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_length function will be called directly in marking. The main function is only to let you test your list_length function

Here is how you use main function allows you to test list_length:

```dcc list_length.c -o list_length
./list_length 1 2 4 8 16 32 64 128 256
8
./list_length 2 4 6 5 8 9
6
./list_length 13 15 17 17 18
5
./list_length 42 4
2
./list_length
0
```

#### Assumptions/Restrictions/Clarifications.

length should return a single integer.

length should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

length should not use arrays.

length should not call malloc.

length should not call scanf (or getchar or fgets).

length should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_length
```

#### Autotest Results

100% of 486 students who have autotested list_length.c so far, passed all autotest tests.
• 100% passed test 1
• 100% passed test 2
• 100% passed test 3
• 100% passed test 4
• 100% passed test 5
• 100% passed test 6

### Revision Exercise: Sum the elements in a Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_sum/list_sum.c .
```
``````// Return the sum of the elements in the linked list pointed by head

// PUT YOUR CODE HERE (change the next line!)
return 42;

}

``````
sum is given one argument, head, which is the pointer to the first node in a linked list.

Add code to sum so that its returns the sum of the list.

For example if the linked list contains these 8 elements:

```1, 7, 8, 9, 13, 19, 21, 42
```

sum should return 120 because `1 + 7 + 8 + 9 + 13 + 19 + 21 + 42 = 120`.

#### Testing

list_sum.c also contains a main function which allows you to test your sum function.

This main function:

• converts the command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_sum function will be called directly in marking. The main function is only to let you test your list_sum function

Here is how you use main function allows you to test list_sum:

```dcc list_sum.c -o list_sum
./list_sum 1 2 4 8 16 32 64 128 256
511
./list_sum 2 4 6 5 8 9
34
./list_sum 13 15 17 17 18
80
./list_sum 42 4
46
./list_sum
0
```

#### Assumptions/Restrictions/Clarifications.

sum should return a single integer.

sum should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

sum should not use arrays.

sum should not call malloc.

sum should not call scanf (or getchar or fgets).

sum should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_sum
```

#### Autotest Results

100% of 468 students who have autotested list_sum.c so far, passed all autotest tests.
• 100% passed test 1
• 100% passed test 2
• 100% passed test 3
• 100% passed test 4
• 100% passed test 5
• 100% passed test 6

### Revision Exercise: Print the elements in a Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_print/list_print.c .
```
``````// print a linked list in this format:
// 17 -> 34 -> 51 -> 68 -> X

}

``````
print is given one argument, head, which is the pointer to the first node in a linked list.

Add code to print so that it prints the elements in the list

For example if the linked list contains these 8 elements:

```1, 7, 8, 9, 13, 19, 21, 42
```

print should print 1 -> 7 -> 8 -> 9 -> 13 -> 19 -> 21 -> 42 -> X

#### Testing

list_print.c also contains a main function which allows you to test your print function.

This main function:

• converts the command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head

Do not change this main function. If you want to change it, you have misread the question.

Your list_print function will be called directly in marking. The main function is only to let you test your list_print function

Here is how you use main function allows you to test list_print:

```dcc list_print.c -o list_print
./list_print 1 2 4 8 16 32 64 128 256
1 -> 2 -> 4 -> 8 -> 16 -> 32 -> 64 -> 128 -> 256 -> X
./list_print 2 4 6 5 8 9
2 -> 4 -> 6 -> 5 -> 8 -> 9 -> X
./list_print 42 4
42 -> 4 -> X
./list_print 43
43 -> X
./list_print
X
```

#### Asprintptions/Restrictions/Clarifications.

print should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

print should not use arrays.

print should not call malloc.

print should not call scanf (or getchar or fgets).

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_print
```

#### Autotest Results

98% of 466 students who have autotested list_print.c so far, passed all autotest tests.
• 99% passed test 1
• 99% passed test 2
• 99% passed test 3
• 98% passed test 4
• 99% passed test 5
• 99% passed test 6

### Revision Exercise: Test if a Linked List is Increasing Order

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_increasing/list_increasing.c .
```
``````int increasing(struct node *head) {

// PUT YOUR CODE HERE (change the next line!)
return 42;

}

``````
increasing is given one argument, head, which is the pointer to the first node in a linked list.

Add code to increasing so that its returns 1 if the list is in increasing order - the value of each list element is larger than the element before.

For example if the linked list contains these 8 elements:

```1, 7, 8, 9, 13, 19, 21, 42
```

increasing should return 1 because is is increasing order

#### Testing

list_increasing.c also contains a main function which allows you to test your increasing function.

This main function:

• converts the command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_increasing function will be called directly in marking. The main function is only to let you test your list_increasing function

Here is how you use main function allows you to test list_increasing:

```dcc list_increasing.c -o list_increasing
./list_increasing 1 2 4 8 16 32 64 128 256
1
./list_increasing 2 4 6 5 8 9
0
./list_increasing 13 15 17 17 18 19
0
./list_increasing 2 4
1
./list_increasing 42
1
./list_increasing
1
```

#### Assumptions/Restrictions/Clarifications.

increasing should return a single integer.

increasing should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

increasing should not use arrays.

increasing should not call malloc.

increasing should not call scanf (or getchar or fgets).

You can assume the linked list only contains positive integers.

increasing should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_increasing
```

#### Autotest Results

96% of 490 students who have autotested list_increasing.c so far, passed all autotest tests.
• 99% passed test 1
• 97% passed test 10
• 98% passed test 2
• 98% passed test 3
• 99% passed test 4
• 98% passed test 5
• 98% passed test 6
• 98% passed test 7
• 97% passed test 8
• 98% passed test 9

### Revision Exercise: Count Elements Divisible by 17 in Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_count_favourite/list_count_favourite.c .
```
``````// Return the number of elements divisible by 17 in the linked list

// PUT YOUR CODE HERE (change the next line!)
return 42;

}

``````
count_favourite is given one argument, head, which is the pointer to the first node in a linked list.

Add code to count_favourite so that its returns the number of elements divisible by 17 in the list.

For example if the linked list contains these 8 elements:

```51, 7, 8, 9, 34, 19, 34, 42
```

count_favourite should return 3 because 51, 34 and 34 are divisible by 17.

#### Testing

list_count_favourite.c also contains a main function which allows you to test your count_favourite function.

This main function:

• converts the command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_count_favourite function will be called directly in marking. The main function is only to let you test your list_count_favourite function

Here is how you use main function allows you to test list_count_favourite:

```dcc list_count_favourite.c -o list_count_favourite
./list_count_favourite 51 7 8 9 34 19 34 42
3
./list_count_favourite 2 4 6 5 8 9
0
./list_count_favourite 17 34 51 68 85 102 119 136 153
9
./list_count_favourite
0
```

#### Assumptions/Restrictions/Clarifications.

count_favourite should return a single integer.

count_favourite should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

count_favourite should not use arrays.

count_favourite should not call malloc.

count_favourite should not call scanf (or getchar or fgets).

count_favourite should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_count_favourite
```

#### Autotest Results

100% of 421 students who have autotested list_count_favourite.c so far, passed all autotest tests.

### Revision Exercise: Test if a Value Occurs in Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_contains/list_contains.c .
```
``````// Return 1 if value occurs in linked list, 0 otherwise
int contains(int value, struct node *head) {

// PUT YOUR CODE HERE (change the next line!)
return 42;

}

``````
contains is given two arguments, an int value and head, which is the pointer to the first node in a linked list.

Add code to contains so that its returns 1 if value occurs in the linked and otherwise it returns 0.

For example if the linked list contains these 8 elements:

```1, 7, 8, 9, 13, 19, 21, 42
```

contains should return 8.

#### Testing

list_contains.c also contains a main function which allows you to test your contains function.

This main function:

• converts the first command-line argument to value
• converts the remaining command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_contains function will be called directly in marking. The main function is only to let you test your list_contains function

Here is how you use main function allows you to test list_contains:

```dcc list_contains.c -o list_contains
./list_contains 3 1 2 3 4
1
./list_contains 42 1 2 3 4
0
./list_contains 17 15 17 17 18
1
./list_contains 21 15 17 17 18
0
./list_contains 42
0
```

#### Assumptions/Restrictions/Clarifications.

contains should return a single integer.

contains should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

contains should not use arrays.

contains should not call malloc.

contains should not call scanf (or getchar or fgets).

contains should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_contains
```

#### Autotest Results

98% of 400 students who have autotested list_contains.c so far, passed all autotest tests.
• 98% passed test 1
• 99% passed test 2
• 99% passed test 3
• 99% passed test 4
• 99% passed test 5
• 98% passed test 6
• 99% passed test 7
• 99% passed test 8
• 99% passed test 9

### Revision Exercise: Return Middle Element of Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_get_middle/list_get_middle.c .
```
``````// Return middle element of a linked list
// if list contains [6,7,8,9,10]  8 is returned
// if a list has even number of elements, first of middle two elements returned
// if list contains [1,2,3,4] 2 is returned
// list can not be empty

// PUT YOUR CODE HERE (change the next line!)
return 42;

}

``````
get_middle is given one argument, head, which is the pointer to the first node in a linked list.

Add code to get_middle so that its returns the middle value of the list. If the list an even number of elements the first of the 2 elements in the middleof the list should be returned.

For example if the linked list contains these 8 elements:

```1, 7, 8, 9, 13, 19, 21, 42
```

get_middle should return 9 because 9 and 13 are the middle two elements/

And or example if the linked list contains these 8 elements:

```1, 2, 8, 1,  42
```

get_middle should return 8 because it is the middle element.

get_middle can asusme the list is not empty.

#### Testing

list_get_middle.c also contains a main function which allows you to test your get_middle function.

This main function:

• converts the command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_get_middle function will be called directly in marking. The main function is only to let you test your list_get_middle function

Here is how you use main function allows you to test list_get_middle:

```dcc list_get_middle.c -o list_get_middle
./list_get_middle 1 2 4 8 16 32 64 128 256
16
./list_get_middle 2 4 6 5 8 9
6
./list_get_middle 13 15 17 19 18
17
./list_get_middle 42 4
42
./list_get_middle 42
42
```

#### Assumptions/Restrictions/Clarifications.

get_middle should return a single integer.

get_middle can assume the list has at least one element.

get_middle should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

get_middle should not use arrays.

get_middle should not call malloc.

get_middle should not call scanf (or getchar or fgets).

get_middle should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_get_middle
```

#### Autotest Results

95% of 417 students who have autotested list_get_middle.c so far, passed all autotest tests.
• 95% passed test 1
• 96% passed test 2
• 96% passed test 3
• 98% passed test 4
• 95% passed test 5

### Revision Exercise: Return Nth Element of Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_get_nth/list_get_nth.c .
```
``````// Return the n-th element of linked list.
// n == 0 returns first element, n == 1, second element, ....
int get_nth(int n, struct node *head) {

// PUT YOUR CODE HERE (change the next line!)
return 42;

}

``````
get_nth is given two arguments, an int n and head, which is the pointer to the first node in a linked list.

Add code to get_nth so that its returns the n-th element the linked element of the linked list.

The elements are counted in the same manner as array elements (zero-based), so the first element in the list is regarded as at position 0, the second element position 1 and so on.

get_nth can assume that the list contains at least `n + 1` elements.

For example if the linked list contains these 8 elements:

```1, 7, 8, 9, 13, 19, 21, 42
```

if n is 1 get_nth should return 7.

#### Testing

list_get_nth.c also get_nth a main function which allows you to test your get_nth function.

This main function:

• converts the first command-line argument to n
• converts the remaining command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_get_nth function will be called directly in marking. The main function is only to let you test your list_get_nth function

Here is how you use main function allows you to test list_get_nth:

```dcc list_get_nth.c -o list_get_nth
./list_get_nth 0  5 6 7 8
5
./list_get_nth 1  5 6 7 8
6
./list_get_nth 2  5 6 7 8
7
./list_get_nth 3  5 6 7 8
8
./list_get_nth 0  42
42
```

#### Assumptions/Restrictions/Clarifications.

get_nth should return a singleinteger.

get_nth can assume n is non-negative.

get_nth can assume the linked list contains at least n + 1 elements.

get_nth should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

get_nth should not use arrays.

get_nth should not call malloc.

get_nth should not call scanf (or getchar or fgets).

get_nth should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_get_nth
```

#### Autotest Results

100% of 400 students who have autotested list_get_nth.c so far, passed all autotest tests.
• 100% passed test 1
• 100% passed test 2
• 100% passed test 3
• 100% passed test 4
• 100% passed test 5
• 100% passed test 6
• 100% passed test 7
• 100% passed test 8

### Revision Exercise: Insert Item On Front of Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_insert_head/list_insert_head.c .
```
``````// Insert a new node containing value at the start of the linked list.
// The head of the new list is returned.

// PUT YOUR CODE HERE (change the next line!)
return NULL;

}

``````
insert_head is given two arguments, value and head. value is an int. head is the pointer to the first node in a linked list.

Add code to insert_head so that it creates a new list node (using malloc) containing value and places it at the start of the list.

insert_head should return a pointer to the new list.

For example if value is 12 and the linked list contains these 3 elements:

```16, 7, 8
```

insert_head should return a pointer to a list with these elements:

```12, 16, 7, 8
```

#### Testing

This main function:

• converts the first command-line argument to value
• converts the remaining command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

```dcc list_insert_head.c -o list_insert_head
[12, 16, 7, 8]
[42, 16]
[2]
```

#### Assumptions/Restrictions/Clarifications.

insert_head should not call scanf (or getchar or fgets).

insert_head should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_insert_head
```

#### Autotest Results

98% of 426 students who have autotested list_insert_head.c so far, passed all autotest tests.
• 98% passed test 1
• 98% passed test 2
• 98% passed test 3
• 98% passed test 4

### Revision Exercise: Insert Item On End of Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_insert_tail/list_insert_tail.c .
```
``````// Insert a new node containing value at the start of the linked list.
// The head of the new list is returned.
struct node *insert_tail(int value, struct node *head) {

// PUT YOUR CODE HERE (change the next line!)
return NULL;

}

``````
insert_tail is given two arguments, value and head. value is an int. head is the pointer to the first node in a linked list.

Add code to insert_tail so that it creates a new list node (using malloc) containing value and places it at the end of the list.

insert_tail should return a pointer to the new list.

For example if value is 12 and the linked list contains these 3 elements:

```16, 7, 8
```

insert_tail should return a pointer to a list with these elements:

```16, 7, 8, 12
```

#### Testing

list_insert_tail.c also contains a main function which allows you to test your insert_tail function.

This main function:

• converts the first command-line argument to value
• converts the remaining command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your insert_tail function will be called directly in marking. The main function is only to let you test your insert_tail function

```dcc list_insert_tail.c -o list_insert_tail
./list_insert_tail 12  16 7 8
[16, 7, 8, 12]
./list_insert_tail 42  16
[16, 42]
./list_insert_tail 2
[2]
```

#### Assumptions/Restrictions/Clarifications.

insert_tail should not use arrays.

insert_tail should not call scanf (or getchar or fgets).

insert_tail should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_insert_tail
```

#### Autotest Results

93% of 407 students who have autotested list_insert_tail.c so far, passed all autotest tests.
• 95% passed test 1
• 95% passed test 2
• 95% passed test 3
• 95% passed test 4

### Revision Exercise: Insert Item at Specified Position in Linked List

```cp -n /import/chopin/1/cs1511/public_html/18s1/activities/list_insert_nth/list_insert_nth.c .
```
``````// Insert a new node containing value at position n of the linked list.
// if n == 0, node is inserted at start of list
// if n >= length of list, node is appended at end of list
// The head of the new list is returned.
struct node *insert_nth(int n, int value, struct node *head) {

// PUT YOUR CODE HERE (change the next line!)
return NULL;

}

``````
insert_nth is given three arguments, n, value and head. n is an int. value is an int. head is the pointer to the first node in a linked list.

Add code to insert_nth so that it creates a new list node (using malloc) containing value and places it before position n of the list.

The elements are counted in the same manner as array elements (zero-based), so the first element in the list is regarded as at position 0, the second element position 1 and so on.

If there are less than `n` elements in the list, the new list node should be appended to the list.

insert_nth should return a pointer to the new list.

For example if n is 1 and value is 12 and the linked list contains these 3 elements:

```16, 7, 8
```

insert_nth should return a pointer to a list with these elements:

```16, 12, 7, 8
```

#### Testing

list_insert_nth.c also contains a main function which allows you to test your insert_nth function.

This main function:

• converts the first command-line argument to n
• converts the second command-line argument to value
• converts the remaining command-line arguments to a linked list
• assigns a pointer to the first node in the linked list to head
• prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your insert_nth function will be called directly in marking. The main function is only to let you test your insert_nth function

```dcc list_insert_nth.c -o list_insert_nth
./list_insert_nth 0 12  16 7 8
[12, 16, 7, 8]
./list_insert_nth 1 12  16 7 8
[16, 12, 7, 8]
./list_insert_nth 2 12  16 7 8
[16, 7, 12, 8]
./list_insert_nth 3 12  16 7 8
[16, 7, 8, 12]
./list_insert_nth 42 12  16 7 8
[16, 7, 8, 12]
./list_insert_nth 0 16  42
[16, 42]
./list_insert_nth 0 2
[2]
./list_insert_nth 10 2
[2]
```

#### Assumptions/Restrictions/Clarifications.

insert_nth should not use arrays.

insert_nth should not call scanf (or getchar or fgets).

insert_nth should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use `autotest` to run some simple automated tests:

```1511 autotest list_insert_nth
```

#### Autotest Results

86% of 371 students who have autotested list_insert_nth.c so far, passed all autotest tests.
• 96% passed test 1
• 92% passed test 10
• 94% passed test 11
• 93% passed test 12
• 93% passed test 13
• 92% passed test 14
• 90% passed test 15
• 94% passed test 16
• 93% passed test 17
• 93% passed test 18
• 93% passed test 19
• 95% passed test 2
• 95% passed test 3
• 95% passed test 4
• 94% passed test 5
• 95% passed test 6
• 93% passed test 7
• 93% passed test 8
• 93% passed test 9