Lab 4 - Doubly Linked List

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

In class on Monday we discussed a list variant called the doubly linked list. This variant has nodes which keep track of not only the node that comes after them, but also the node that comes before them. To further facilitate traversing either forward or backwards through the list, the doubly linked list keeps track of both the "head" (first node) and "tail" (last node).

To implement this, we need to alter insertions and deletions to do the proper actions for four cases:

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 doubly 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 any reverse traversals.

The implementation also does not have node deletion (the remove() function). Your task for this assignment is to implement node deletion following the pseudocode given in class on Monday. That pseudocode is:

Doubly linked list Deletion
  Given - node to delete ("ptr")
          (since it is implemented as a member function in the list, it does
           not need the reference to the list as discussed in class. That is
           only needed by procedural implementations. Also, we can access the
           head and tail directly since this function is inside the list object)

  if list is empty
    print/raise "empty list" error
    return out of function
  end-if
  if ptr is NULL
    print/raise "invalid pointer" error
    return out of function
  end-if

  if ptr is equal to the head AND ptr is equal to the tail
    set head and tail to NULL
  else if ptr is equal to the head
    set head to head->next
    set head->prev to NULL
  else if ptr is equal to the tail
    set tail to tail->prev
    set tail->next to NULL
  else
    set nextNode to ptr->next
    set prevNode to ptr->prev
    set nextNode->prev to prevNode
    set prevNode->next to nextNode
  end-if

  delete ptr
Once you implement the deletion 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 me your updated lab4.cpp file.