CS202 - Assignment Eight
Due: 2018-11-14 09:00a
Objectives
- Do some work with memory allocation
- Learn about using structs
Linked list
Note that for this assignment, I'm assuming you have now seen linked lists, if this is not the case, come chat with me.
I've written part of a program for you (hw8_starter.c). The little program should provide an interactive interface for adding and removing items from a list. I wrote the interaction portion, but I don't have time to implement the actual data storage portion of the code. There are several things you will need to do.
First, you must create a new type that can hold a linked list node. It will need fields for the payload (an integer) and a next pointer.
Aside: When I showed you structs, I jumped right to using typedef
. The typedef
is a problem for recursive structures, because the name isn't available when we want to use it. Here is what the color struct looks like without typedef.
struct color {
byte red;
byte: green;
byte: blue;
};
struct color c;
c.red = 255;
Note that I added a name after struct
. I then could instantiate a new color item with struct color
as the type (the struct
is required). Using typedef
just means that we don't need to include the struct
as part of the type name. We can still wrap the whole thing in a typedef
-- we can even reuse the name. In this case, we could use struct color
inside of the struct itself and color
everywhere else.
Then, create a second a structure that represents the list as a whole. It should hold the length of the list, as well as pointers to the head and the tail of the list. Call this list
so it interfaces with my code correctly.
Then you need to write four functions:
create_list()
: This function should create an empty list object.
add_value()
: This function takes in a list and a value, adds the value to the end of the list, and then returns the resulting list.
remove_value()
: This takes in a list and a value and removes the first occurrence of the value from the list. It returns the resulting list. If the item can't be found in the list, it should inform the user before returning.
print()
:This takes a list and returns nothing. It should print out the length of the list followed by the contents on a single line.
Here is an example of the code in action:
Commands are a single character followed optionally by a number. The valid commands are:
- a <num> (add a number to the list)
- p (print the list)
- r <num> (remove value <num> from the list if present)
- q (quit)
Enter a command: a 5
Enter a command: a 9
Enter a command: a 5
Enter a command: a 2
Enter a command: p
[4] 5 9 5 2
Enter a command: r 2
Enter a command: p
[3] 5 9 5
Enter a command: r 5
Enter a command: p
[2] 9 5
Enter a command: r 5
Enter a command: p
[1] 9
Enter a command: q
Submit this code as hw8_list.c
Array list
Now I want you to redo the list program, but this time we will use an array as the backing store. As arrays do not know their own length, we will again create a list
struct. The struct
should hold a pointer to the array, the length of the array, and the length of the list (which are not necessarily the same!).
Starting from the starter code, re-implement the four functions using the new array list, otherwise keeping the prototypes intact.
Note that you will need to allocate some initial storage capacity for the array. Start with 5. Your code should properly handle running out of room in the array without any change to the interface (so, you can't just say "sorry, out of room"). The typical approach is to double the size of the array if it is about to overflow.
Submit this code as hw8_array.c
Deliverables
- The two files must be named as specified above.
- Make sure that the assignment runs on Linux before you submit it. Do not assume that it will -- check it.
- At the very top of the file, you will place a block comment that follows this format exactly:
/*
Assignment 8
Name:
Email:
Date:
Collaborators:
To compile:
To run:
*/
The name
, date
, and email
fields should be self explanatory. In the collaborators
field, you should list everyone who helped you with this assignment including classmates, tutors, and myself. In the to compile
and to run
fields, provide the commands required to compile and execute your code (i.e., someone should be able to copy these commands directly onto a command line and run your code). The to run
should include usage instructions (what arguments need to be provided and what they do).
The assignment should be submitted on Canvas.
Rubric
points | explanation |
---|---|
2 | linked list structs |
3 | create_list (linked) |
5 | add_value (linked) |
5 | remove_value (linked) |
3 | print (linked) |
1 | array list struct |
3 | create_list (array) |
5 | add_value (array) |
5 | remove_value (array) |
3 | print (array) |
5 | following submission instructions |