CS21 Lab 12: Doubly-Linked Lists

OPTIONAL - NO DUE DATE (but good practice for the final...hint, hint)
This lab is completely optional. You will not receive a grade on this assignment. However, you may want to try these problems as practice for the final exam.

Doubly-Linked Lists

For this lab, run update21 to get a clean copy of the code we wrote in class for the Node and LinkedList classes. In your cs21/labs/12 directory should be the node.py and linkedlist.py files. Read through them and make sure you understand how they work. You can also start with your own files from class, if you want.

So far we have dealt exclusively with what are called singly-linked lists. They are called singly-linked because there is only one link that connects each node to another node in the list. We have called this link next because it points to the next item in the list.

In this exercise, you will write an implementation of a doubly-linked list. A doubly-linked list maintains two links to other nodes in the list: a link called next, which points to the next node in the list (and functions identically to the next pointer we have seen so far), and a link called prev, which points to the previous node in the list. The prev link allows you to move backwards through the list. For example, to find the second-to-last item in a doubly-linked list, start at the tail and follow one prev to get to the second-to-last node.

Here is what a doubly-linked list might look like in memory (just the head and tail fields of the linkedList object are shown):

                Node obj         Node obj         Node obj         Node obj
                --------         --------         --------         --------
                | data-|--> 6    | data-|--> 12   | data-|--> 3    | data-|--> 8
head ---------->| next-|-------->| next-|-------->| next-|-------->| next-|----|
           |--- |-prev |<--------|-prev |<--------|-prev |<--------|-prev |
                --------         --------         --------         --------
tail ------------------------------------------------------------------ 

You will need to do the following:

  1. Modify node.py so that it has a prev link. Add getPrev() and setPrev() methods. Update any other methods to handle the prev instance variable.
  2. Modify linkedlist.py so that it properly maintains the prev links. You will need to update or add/write append(), prepend(), deleteHead(), and deleteTail().
  3. Add functionality to support a method called find(self, key) which returns the Node containing the first instance of the key in the LinkedList (starting from the head of the list). This method should return None if the key is not in the list.
  4. Add functionality to support a method called remove(self, key) which removes the Node containing the first instance of the key in the LinkedList (starting from the head of the list). You will probably want to use the find method you wrote in the previous part as part of this solution.

Include a main function, that:

  1. creates a doubly-linked list from 5 values entered by the user and prints out the resulting list (make sure you have an implementation of the __str__ method for the LinkedList class).
  2. prompts the user to enter a value to search for in the list, calls your find method function, and prints out it's return value.
  3. prompts the user to enter a value to remove from the list, calls your remove method, and then prints out the resulting list
Please enter 5 values!
 1: 21
 2: 45
 3: 79
 4: 99
 5: 3

Here is your list:

search for: 79

    remove: 99