Lab 8
Lab 8
CS-2050
1 Requirements
Note that, although this week’s lab looks very similar to lab 7, there are important differences you must
take into account. Remember that you are allowed to reference past lab solutions posted on the 2050
Canvas.
In this lab, you will be implementing a Vending Machine ADT using linked lists. You are given a partial
typedef in the starter code for this lab. You must complete the definition for the VendingMachine struct in
your implementation file. Note that as the VendingMachine struct is to be defined in lab7.c, you will not
be able to dereference it in main.c.
In this lab, you are given the same struct declarations as with the previous two labs.
1.1 newMachine
i
Info: This function creates a new, empty, vending machine with a dynamic number of item slots. Each
slot must be able to hold an instance of StockItem. You are required to use a linked list as the
primary backing data structure for your implementation in this lab.
This function will return a pointer to the newly created vending machine on success, or NULL on
failure.
1.2 addStockItem
i
Info: This function takes a VendingMachine, a StockItem, and an afterID. It inserts the item into
the vending machine. If the afterID given is > 0, it inserts the new item just after the stock item with
the specified ID. If the afterID given is <= 0, it inserts the new item at the head of the list. If a stock
item with the given afterID is not found on the list, the new item is inserted at the end of the list. It
returns 1 if insertion was successful, or 0 if insertion failed. You may assume that no duplicate stock
items will be inserted.
Examples:
machine = { 2 , 1 }
// After calling addStockItem with item . ID =7 , afterID =0
machine = { 7 , 2 , 1 }
// After calling addStockItem with item . ID =25 , afterID =2
machine = { 7 , 2 , 25 , 1 }
// After calling addStockItem with item . ID =9 , afterID =3
machine = { 7 , 2 , 25 , 1 , 9 }
1
1.3 printIDS
i
Info: This function takes a VendingMachine, and prints the ID for each item it contains, all on one
single line, separated by commas. You do not need to exactly match the format given in the example
below to get full credit, but your output must all be on one line, and must have a comma between
each pair of IDs. Don’t forget a newline at the end!.
Example :
machine = { 7 , 2 , 1 }
// output of printIDS
VendingMachine : 7 , 2 , 1
1.4 restockItem
i
Info: This function takes a VendingMachine, and sets the stock of the item with the given ID to
maxStock. It returns 1 if the item was found and was not already full, or 0 if the item was not found
or was already fully stocked.
1.5 removeStockItem
i
Info: This function takes a VendingMachine, and the ID of a StockItem. If a stock item with the given
ID exists in the vending machine, it will update the result pointer with the stock item, remove the
item from the list, and return 1. If the ID is not associated with a stock item in the vending machine,
no items is removed and it will return 0;
1.6 freeVendingMachine
i
Info: This function takes a VendingMachine, and frees all memory allocated to it.
2
!
Grading: 33 points
* 2 points
5. Write required removeStockItem function
* 12 points
6. Write required freeVendingMachine function
* 3 points
!
Notice:
1. All of your lab submissions must include documentation to receive full points.
2. All of your lab submissions must compile under GCC using the −W all, −W error, and −W pedantic
flags to be considered for a grade.
3. You are expected to provide proper documentation in every lab submission, in the form of
code comments. For an example of proper lab documentation and a clear description of our
expectations, see the lab intro PDF.