CS115 Exercise: C++ Simple Linked Lists

Get Code For the Online Lab

The starting code is available on Replit:

To compile the linked list code, type

make

The purpose of the program is to demonstrate operations on a simple singly linked list.

Exercise

Now you should be ready to start adding code to that program.

  1. First, in LinkedList.cpp, add the code to print out the linked list. The function called LinkedList::printList() is provided in the code; you need to add the implementation.

    Compile and run this C++ program.

  2. Now, add the code to add a single node element to the end of the list. The function called LinkedList::appendItem() is provided in the code; you need to add the implementation.

    Compile and run this C++ program.

  3. Add the code to delete a node from the list. The function called LinkedList::deleteItem() is provided in the code; you need to add the implementation.

    Compile and run this C++ program.

    Try deleting the first, last and middle node. Also try to remove something that is not in the list (an appropriate message should be printed).

Test Run

Notice that you code should work when you are trying to delete and item at the beginning, at the end, and in the middle. It should also still work and provide a message if an item does not exist. Please try the steps below to ensure that your code is working as expected before submitting.

  1. Make the list: 4 12 16 999
  2. Print
  3. Insert an 8
  4. Print
  5. Append a 30
  6. Print
  7. Delete the 4
  8. Print
  9. Delete the 30
  10. Print
  11. Delete the 12
  12. Print
  13. Try to delete a 2
  14. Print

[an error occurred while processing this directive]