P13: ArrayList
Goals
- Get practice thinking about memory
Objective
For this assignment, you are going to implement an ArrayList – the kind of array you are actually more accustomed to working with.
I’ve written part of a program for you (problem13_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.
Your ArrayList should know its own length, and we should be able to add and remove values from it without worrying about the storage space.
To finish the program, you need to add implementations for 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, separated by spaces.
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
Requirements
- The
create_listfunction should create a new ArrayList on the heap - The
add_valuefunction should add a value to the end of the list. If there is insufficient storage, it should make room - The
remove_valuefunction should remove the first occurrence of the passed in value that it finds. - After removing an item,
remove_valueshould shift the values so that there isn’t a gap remove_itemshould print out a message if the value can’t be foundprintshould print the length of the list and then all of the contents on a single line, separated by spaces.- At the top of your C file, there should be a multiline comment that includes your name, the date, instructions for compiling the code and instructions for running it
Implementation tips
You will need a way to store three things: an array that provides the actual storage, the length of the list and the length of the array (not the same thing).
In the declarations of the function that you need to write, you will see many instances of list_t *. Your ArrayList should be in a type called list_t. We haven’t seen this yet, but you can use the typedef command to name your types. For example, here is the struct color we have looked at, and here I am adding the alias color_t.
struct color{
uint8_t r;
uint8_t g;
uint8_t b;
};
typedef struct color color_t;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.
Submission
Upload your .c file to Gradescope.