Chapter 17 Linked List

Document Sample
Chapter 17 Linked List Powered By Docstoc
					Chapter 17
Linked List

          Bernard Chen
           Spring 2006
 Comparison with array
1. Arrays                          arrayname
  contiguous
  direct access of elements
  insertion / deletion difficult
2. Linked Lists
  noncontiguous
  must scan for element
  insertion /deletion easy
                                    a

Iterating through a
data structure

 for (int i = 0; i < length; i++)
     cout<< a[i];


 for (ListNode p = theList.first; p != null; p = p.next)
     cout<< p.data ;
  Adding an element
                 A0                  A1                 A2


class ListNode
{                            first               last
  Object   data;
  ListNode* next;
}

At any point, we can add a new last item x by doing this:
Last->next = new ListNode();
last = last->next;
Last->data = x;
Last->next = null;
         A0                  A1                  A2



                     first                last
class ListNode
{
  Object   data;
  ListNode* next;
}

At any point, we can add a new last item x by doing this:

Last->next = new ListNode();
last = last->next;
Last->data = x;
Last->next = null;
        A0                  A1                  A2



                    first                last
class ListNode
{
  Object   data;
  ListNode* next;
}

At any point, we can add a new last item x by doing this:

Last->next = new ListNode();
last = last->next;
Last->data = x;
Last->next = null;
       A0                  A1                  A2           x



                   first                last
class ListNode
{
  Object   data;
  ListNode* next;
}

At any point, we can add a new last item x by doing this:

last->next = new ListNode();
last = last->next;
Last->data = x;
Last->next = null;
        A0                  A1                  A2          x



                    first                last
class ListNode
{
  Object   data;
  ListNode* next;
}

At any point, we can add a new last item x by doing this:

Last->next = new ListNode();
last = last->next;
Last->data = x;
Last->next = null;
  Inserting an element
             A0                      A1                     A2



     first                   current                         last
class ListNode
{
   Object       element;
   ListNode* next;
}
At any point, we can add a new last item x by doing this:
tmp = new ListNode();
Tmp->element = x;
Tmp->next = current->next;
Current->next = tmp;
  Inserting an element
             A0                     A1                      A2


     first                  current
                                                                 last
class ListNode
{
   Object       element;                    tmp
   ListNode* next;
}
At any point, we can add a new last item x by doing this:
tmp = new ListNode();
Tmp->element = x;
Tmp->next = current->next;
Current->next = tmp;
  Inserting an element
               A0                     A1                    A2


       first                  current              x             last
class ListNode
{
   Object       element;                       tmp
   ListNode* next;
}
At any point, we can add a new last item x by doing this:
tmp = new ListNode();
Tmp->element = x;
Tmp->next = current->next;
Current->next = tmp;
  Inserting an element
               A0                     A1                    A2


       first                  current              x             last
class ListNode
{
   Object       element;                       tmp
   ListNode* next;
}
At any point, we can add a new last item x by doing this:
tmp = new ListNode();
Tmp->element = x;
Tmp->next = current->next;
current->next = tmp;
  Inserting an element
                 A0                     A1                  A2


         first                  current              x           last
class ListNode
{
   Object       element;                        tmp
   ListNode* next;
}
At any point, we can add a new last item x by doing this:
tmp = new ListNode();
Tmp->element = x;
Tmp->next = current->next;
Current->next = tmp;
     Simplified version
Current->next = new ListNode(x, current->next)


Current->next = tmp;

         tmp = new ListNode();

                 Tmp->element = x;

                         Tmp->next = current->next;
Deleting an element

    A0               A1                 A2


 current                             last
      class ListNode
      {
        Object   element;
        ListNode* next;
      }

      Current->next = current->next->next;
 Deleting an element

      A0               A1                  A2


   current                              last
class ListNode
{
  Object   element;
  ListNode* next;
}

Current->next = current->next->next;   Memory leak!
Delete a Node
Node *deletedNode = current->next;
Current->next = current->next->next;
Delete deletedNode;
     Header node

                       a                b                 c


                 header
Header nodes allow us to avoid special cases [in the code] such
as insertion of the first element and removal of the last element.

The header node holds no data but serves to satisfy the
requirement that every node have a previous node.

Not necessarily a standard implementation.
    Doubly Linked Lists
                   a         b                  c


   head                                  tail

class DoubleListNode   A doubly linked list allows
{                      bidirectional traversal by
  Object   element;    storing two pointers per
  ListNode* next;      node.
  ListNode* prev;
}
   Empty Doubly Linked List



                 head       tail
// constructor
DoubleList()
{
    head = new   DoubleListNode ();
    tail = new   DoubleListNode ();
    head->next   = tail;
    tail->prev   = head;
}
Inserting into a Doubly Linked List

                a                     c


hea                            tail
d          current
newNode = new DoublyLinkedListNode()
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode
  Inserting into a Doubly Linked List

                  a                       c


head                       b       tail
             current
newNode = new DoublyLinkedListNode()
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode
  Inserting into a Doubly Linked List


                 a                        c


 head                      b       tail
                  current
newNode = new DoublyLinkedListNode()
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode
  Inserting into a Doubly Linked List


                  a                        c


 head                          b    tail
                  current
newNode = new DoublyLinkedListNode()
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode
  Inserting into a Doubly Linked List


                 a                            c


 head                      b           tail
                  current
newNode = new DoublyLinkedListNode()
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode
  Inserting into a Doubly Linked List


                  a                       c


 head        current       b       tail
newNode = new DoublyLinkedListNode()
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode
  Inserting into a Doubly Linked List


                 a                        c


 head                      b       tail
                  current
newNode = new DoublyLinkedListNode()
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode
        Circular Linked Lists

        a        b         c    d



first
Sorted Linked List
   A sorted link list is one in which items
    are in sorted order. It can be derived
    from a list class.
   The major difference from linked list is
    the insertion operation.
     In Class exercise
   Suppose you have a pointer to a node in a
    single linked list that is guaranteed not to be
    the last node in the list. You don’t have
    pointers to any other nodes. Describe an
    O(1) algorithm that logically removes the
    value stored in such a node from the linked
    list, maintaining the integrity of the linked
    list.