Lab 4 - Object-Oriented Linked Lists

The purpose of this lab is to implement the node insertion routine for a regular linked list using object-oriented programming.

For this lab, copy the file lab4.cpp to your directory using cp, wget or vi copy/paste. This contains a partial implementation of a linked list and a main() function that implements a menu similar to the menu in Homework 2. It uses object-oriented programming (C++ classes) and an exception class to detect an invalid list when the empty() function is called. It is only a partial implementation because it does not contain the insertion routine.

When manipulating the object-oriented version of a node, you will have to use member functions in the public section of ListNode to manipulate the ListNode fields. So instead of being able to access the next field directly, you will have to use node->getNext() and node->setNext(nextnode). This is due to the encapsulated nature of an object-oriented class, where data in the private section cannot be directly manipulated.

On the other hand, when you are inside the member function for a class, you can access the member variables of that class directly, without using the dot or arrow operator. For example, when inside the list insertion function, you can access head directly because it's a member variable of that particular class.

Another feature of classes is that the new command can give initial values to a new object by invoking a class constructor function. In ListNode, there is a function called ListNode(int elem). This allows us to create a new ListNode and set its data field with just one call to new. For example, if you wish to create a new node that stores the value 5, the following line of code will do so:

ListNode *newNode = new ListNode(element);

Your task for this assignment is to implement node insertion routine using the following pseudocode:

Object-Oriented Linked List Insertion
  Given: Element to insert, address of node to insert after (prev)
         Head is not explicitly given because it's a member variable in the class
  Returns: Nothing, updates the head member variable in the class instead

  if prev is not NULL
    declare a Boolean inList and initialize to false
    declare a ListNode *ptr and initialize to head
    while ptr is not NULL
      if ptr is equal to prev
        set inList to true
        break out of loop
      end-if
      set ptr to ptr->getNext()
    end-while
    if not inList
      print error message
      return
    end-if
  end-if

  declare ListNode *newNode
  try to allocate by calling newNode = new ListNode(element);
  if allocation fails
    print error message
    throw OutOfMemory();
  end-if

  if prev is NULL
    call newNode->setNext(head)
    set head to newNode
  else
    call newNode->setNext(prev->getNext())
    call prev->setNext(newNode)
  end-if
Once you implement the insertion pseudocode, you should be able to use the menu to insert data into the list, then search for an integer, then delete the node containing that integer.

Email Terry your updated lab4.cpp file.