CS202 - Assignment Eight

Due: 2016-04-29 11:55p

Objectives

Part 1: Allocate a multi-dimensional array [5 points]

I have written a short program that should look very familiar from class (hw8a_starter.c). I have, however, left off the function that handles the memory allocation. Please write the memory allocation function for me. There is one catch – you are only allowed one call to malloc (and no calls to calloc).

Submit this code as hw8a.c

Part 2: Write a simple linked list implementation [20 points]

Note that for this assignment, I’m assuming you have now seen linked lists, if this is not the case, come chat with me.

Again, I’ve written part of a program for you (hw8b_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. We can build fancier linked lists with special structs that hold the list, or double links, but we will keep it simple. We have a node type and our list is just a pointer to a node.

Then you need to write three functions:

addValue()
This function takes in a list and a value and returns the resulting list. The value should be added to the end of the list. Yes, this is a horrible thing to do with a linked list that is not doubly linked…
removeValue()
This takes in a list and a value and removes the first occurrence of the value from the list. It returns the resulting list.

print() :This takes a list and returns nothing. It should print out the contents of the list on a single line.

Here is an example of the code in action:


Enter a command:   
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
5 9 5 2 
Enter a command: r 2
Enter a command: p
5 9 5 
Enter a command: r 5
Enter a command: p
9 5 
Enter a command: r 5
Enter a command: p
9 
Enter a command: q

Submit this code as hw8b.c

Part 3: Repeat with an array [20 points]

Now I want you to redo the list program, but this time we will use an array as the backing store. As we observed, arrays do not know how long they are. We will correct this by creating a struct that holds the array as well as the length. You will probably also want another value to hold the length of the list. Rewrite the three functions using this new structure. When you first alloc space for your array, you will need to pick a size. 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 overflows…

Submit this code as hw8c.c


Turning in your work

Please submit these three files on Moodle. You should replace any of my comments with comments of your own.