Homework 8 - Linked List
In this homework, you will implement a doubly-linked list in C. You will need to use malloc
and free
to store the list nodes on the heap.
Getting the starter code
Download the starter code using git:
git clone https://github.com/CSO-Starter-Code/HW08-linkedlist.git
cd HW08-linkedlist
Aside: If you would like to use this repository to sync your code with the portal, you will need to have a separate “remote”, since you will not have access to push to our Course’s repository on GitHub. You may do this using the following commands:
On the portal, create a new bare repository:
mst3k@portal05:~$ mkdir cs2130_hw8.git && cd cs2130_hw8.git mst3k@portal05:~$ git init --bare
Then, on on your local machine in the
HW08-linkedlist
directory:mst3k@mymachine$ git remote add portal mst3k@portal.cs.virginia.edu:cs2130_hw8.git mst3k@mymachine$ git fetch --all mst3k@mymachine$ git push portal main mst3k@mymachine$ git branch --set-upstream-to=portal/main
Now when you push your code, it will go to the portal by default.
Lastly, on the portal, check out your new repository:
mst3k@portal02:~$ git clone portal.cs.virginia.edu:cs2130_hw8.git
You’ll be able to run your code from the
cs2130_hw8
directory and usegit push
andgit pull
as you have been throughout the semester.
Edit the C file named linkedlist.c
that contains implementations of functions for manipulating a doubly-linked list as described and declared in the header file linkedlist.h
.
Your code should begin by including the given header file, #include "linkedlist.h"
. We will assume you use this exact linkedlist.h
in our testing; if you change it, we will not use those changes in our compilation and testing of your code. Comments in linkedlist.h
describe what each function is supposed to do.
To get you started, here is one of the functions required for the linked list implementation, fully and correctly implemented for you; it has also been included in the linkedlist.c
file:
/* reference solution provided with assignment description */
void ll_show(ll_node *list) {
ll_node *ptr = ll_head(list);
putchar('[');
while(ptr) {
if (ptr->prev) printf(", ");
if (ptr == list) putchar('*');
printf("%d", ptr->value);
if (ptr == list) putchar('*');
ptr = ptr->next;
}
puts("]");
}
Testing your code
You should manually test your code, including using the lldb
debugger and/or the debugging function provided below. Test all of the corner cases (NULL
arguments; first-, last-, and middle-node arguments, finding values not in the list, etc). Do NOT rely on Gradescope to be your compiler and tester! The Gradescope submission site will open on Monday and will only allow a total of 20 submissions.
Example testing code
We have also provided an example of some test code in the provided main
function. Once everything is implemented correctly, this code:
int main(int argc, const char *argv[]) {
ll_node *x = NULL;
putchar('A'); ll_show(x);
x = ll_insert(25, x, 1);
putchar('B'); ll_show(x);
x = ll_insert(1, x, 0);
putchar('C'); ll_show(x);
x = ll_insert(98, x, 1);
putchar('D'); ll_show(x);
x = ll_insert(0, x, 1);
putchar('E'); ll_show(x);
x = ll_insert(-8, ll_tail(x), 0);
putchar('F'); ll_show(x);
ll_node *y = ll_head(x);
putchar('G'); ll_show(y);
printf("Length: %lu (or %lu)\n", ll_length(y), ll_length(x));
x = ll_remove(x);
putchar('H'); ll_show(x);
putchar('I'); ll_show(y);
x = ll_remove(ll_find(ll_tail(y), 98));
putchar('J'); ll_show(x);
putchar('K'); ll_show(y);
return 0;
}
should display:
A[]
B[*25*]
C[25, *1*]
D[25, *98*, 1]
E[25, *0*, 98, 1]
F[25, 0, 98, 1, *-8*]
G[*25*, 0, 98, 1, -8]
Length: 5 (or 5)
H[]
I[*25*, 0, 98, 1]
J[25, 0, *1*]
K[*25*, 0, 1]
Tips
-
I have yet to see a student implement their first correct linked-list without drawing box-and-arrow pictures.
-
Debugging can be easier if you can see all of the pointers involved. An example function to do that is
/** * Debugging display: shows the address and all the fields of the node, * optionally with the nodes before (if b is true) and after (if f is true). * * Written by CSO1 course staff and made available to all students. */ void ll_dump(ll_node *list, int f, int b) { if (b && list->prev) ll_dump(list->prev, 0, 1); printf("%p: %p\t[%d]\t%p\n", list, list->prev, list->value, list->next); if (f && list->next) ll_dump(list->next, 1, 0); }
-
Don’t use after free! We will test your code with the address sanitizer enabled and may also manually check for additional memory errors.
Submission
Submit your code to Gradescope.
Note: the Gradescope submission will not be available until Monday, and will then only allow a total of 20 submissions. We encourage you to write your code early and test it before submitting to Gradescope.