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; };Download list_length.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/18s1/activities/list_length/list_length.c .Your task is to add code to this function in list_length.c:
// Return the length of the linked list pointed by head
int length(struct node *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.
This main function:
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
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
cp -n /web/cs1511/18s1/activities/list_sum/list_sum.c .Your task is to add code to this function in list_sum.c:
// Return the sum of the elements in the linked list pointed by head
int sum(struct node *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
.
This main function:
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
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
cp -n /web/cs1511/18s1/activities/list_print/list_print.c .Your task is to add code to this function in list_print.c:
// print a linked list in this format:
// 17 -> 34 -> 51 -> 68 -> X
void print(struct node *head) {
// PUT YOUR CODE HERE
}
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
This main function:
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
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
cp -n /web/cs1511/18s1/activities/list_increasing/list_increasing.c .Your task is to add code to this function in 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
This main function:
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
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
cp -n /web/cs1511/18s1/activities/list_count_favourite/list_count_favourite.c .Your task is to add code to this function in list_count_favourite.c:
// Return the number of elements divisible by 17 in the linked list
int count_favourite(struct node *head) {
// 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.
This main function:
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
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
cp -n /web/cs1511/18s1/activities/list_contains/list_contains.c .Your task is to add code to this function in 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.
This main function:
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
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
cp -n /web/cs1511/18s1/activities/list_get_middle/list_get_middle.c .Your task is to add code to this function in 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
int get_middle(struct node *head) {
// 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.
This main function:
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
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
cp -n /web/cs1511/18s1/activities/list_get_nth/list_get_nth.c .Your task is to add code to this function in 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.
This main function:
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
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
cp -n /web/cs1511/18s1/activities/list_insert_head/list_insert_head.c .Your task is to add code to this function in 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.
struct node *insert_head(int value, struct node *head) {
// 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
This main function:
Do not change this main function. If you want to change it, you have misread the question.
Your insert_head function will be called directly in marking. The main function is only to let you test your insert_head function
dcc list_insert_head.c -o list_insert_head ./list_insert_head 12 16 7 8 [12, 16, 7, 8] ./list_insert_head 42 16 [42, 16] ./list_insert_head 2 [2]
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
cp -n /web/cs1511/18s1/activities/list_insert_tail/list_insert_tail.c .Your task is to add code to this function in 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
This main function:
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]
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
cp -n /web/cs1511/18s1/activities/list_insert_nth/list_insert_nth.c .Your task is to add code to this function in 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
This main function:
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]
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