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:
Include a main function, that:
Please enter 5 values! 1: 21 2: 45 3: 79 4: 99 5: 3 Here is your list: head--><--21--><--45--><--79--><--99--><--3--><--tail search for: 79 <--79--> remove: 99 head--><--21--><--45--><--79--><--3--><--tail