CS115 Exercise: C++ Simple Linked Lists

Get Code For the Online Lab

Please get the zip and extract the files to your cs115 directory:

  1. Sample code shown in the videos (11-LinkedList-Demo)
    curl https://www.labs.cs.uregina.ca/115/CODE/11-LinkedList-Demo.zip -O -s
    unzip 11-LinkedList-Demo.zip

  2.  
  3. Choose one of the following two:
    1. Linked list exercise using "NULL" (11-LinkedList-NULL)
      curl https://www.labs.cs.uregina.ca/115/CODE/11-LinkedList-NULL.zip -O -s
      unzip 11-LinkedList-NULL.zip

      OR

    2. Linked list exercise using "nullptr" (11-LinkedList-nullptr)
      curl https://www.labs.cs.uregina.ca/115/CODE/11-LinkedList-nullptr.zip -O -s
      unzip 11-LinkedList-nullptr.zip

  4.  

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


This page last modified:
Monday, 25-Nov-2024 08:46:18 CST

CS Dept Home Page
CS Dept Class Files
CS115 Lab Files

© Copyright: Department of Computer Science, University of Regina.