Docstoc

Singly Linked Lists

Document Sample
Singly Linked Lists Powered By Docstoc
					            Singly Linked Lists
- Ed. 2, 3: Chapter 4
- Ed. 4.: Chapter 3
                                 Wiley              Gary



                 Bill

                                     McFee


         Scott          Scott wants to find information about
                        computer networks.
Definition: A linked list is a colleciton of nodes that
together form a linear ordering.

node: A compound object that stores a reference to an
element and a reference, called next, to another node.

                                       Element

  Node
           Reference to an
           element
           Reference to            next
           another node
     head

                next          next             next             next


  element      element        element          element
         Baltimore     Rome          Seattle          Toronto


link: The next reference inside a node is a link or pointer to
another node.
We can start from a given node, and move from it to the next
and so on. This is called link hopping or pointer hopping.


     head

                next          next             next             next


  element      element        element          element
         Baltimore     Rome          Seattle          Toronto
head: The first node of a linked list
tail: The last node of a linked list - it has a null next reference.


      head                                                       tail

                 next          next             next             next


   element      element        element          element
          Baltimore     Rome          Seattle          Toronto



Such a linked list is called a singly linked list.
Illustration of a linked list in memory:

 50B0

                  5110   Toronto

 50A0

                  5100
 5090
                                           node   pointer to a
        5070      50F0
 5080   50D0                                      next node
                  50E0   Rome
        0
        5110
 5070
                         Seattle
                                                  pointer to
        5080      50D0
 5060   50E0                                      an element
                         Baltimore
        5060      50C0
        50C0
 5050
50B0

                     5110   Toronto

50A0

                     5100               node   pointer to a
5090                                           next node
       5070          50F0
5080   50D0                                    pointer to
                            Rome
       0
                     50E0                      an element
5070   5110
                            Seattle
       5080          50D0
5060   50E0

                            Baltimore
       5060          50C0
       50C0
5050
              head
50B0

                     5110   Toronto

50A0

                     5100
5090
                                        node   pointer to a
       5070          50F0
5080   50D0                                    next node
                     50E0   Rome
       0
5070   5110                                    pointer to
                            Seattle
       5080          50D0                      an element
5060   50E0

                            Baltimore
       5060          50C0
       50C0
5050
              head
50B0

                     5110   Toronto

50A0

                     5100
                                        node   pointer to a
5090
                                               next node
       5070          50F0
5080   50D0

                     50E0   Rome               pointer to
       0
5070   5110                                    an element
                            Seattle
       5080          50D0
5060   50E0

                            Baltimore
       5060          50C0
       50C0
5050
              head
50B0

                     5110   Toronto

50A0

                     5100
5090

       5070          50F0
5080   50D0

                     50E0   Rome
       0
5070   5110
                            Seattle
       5080          50D0
5060   50E0

                            Baltimore
       5060          50C0
       50C0
5050
              head
Singly Linked Lists and Arrays
       Singly linked list                   Array
Elements are stored in linear   Elements are stored in linear
order, accessible with links.   order, accessible with an
                                index.

Do not have a fixed size.       Have a fixed size.

Cannot access the previous      Can access the previous
element directly.               element easily.

No binary search.               Binary search.
                    Class Node
Here is an implementation of nodes in Java:

public class Node {
   private Object element;
   private Node next;

   public Node() {
      this( null, null );
   }

   public Node( Object e, Node n ) {
      element = e;
      next = n;
   }
    Object getElement() {
       return element
    }

    Node getNext() {
       return next;
    }

    void setElement( Object newElem ) {
       element = newElem;
    }

    void setNext( Node newNext ) {
       next = newNext;
    }
}
Insertion of an Element at the
             Head
Before the insertion:

     head


                next              next             next


  element       element           element
         Rome           Seattle          Toronto
Have a new node:

                    head

               next           next             next             next


  element       element       element          element
        Baltimore      Rome          Seattle          Toronto




  Node x = new Node();
  x.setElement(new String(“Baltimore”));
  The following statement is not correct:
  x.element = new String(“Baltimore”));
After the insertion:

     head

                 next          next             next             next


  element        element       element          element
         Baltimore      Rome          Seattle          Toronto




     x.setNext(head);
     head = x;
  Deleting an Element at the Head
Before the deletion:

     head

                next          next             next             next


  element        element      element          element
         Baltimore     Rome          Seattle          Toronto
Remove the node from the list:

                     head

                next           next             next             next


  element        element       element          element
         Baltimore      Rome          Seattle          Toronto



     head = head.getNext();
After the deletion:

     head

                next             next             next


  element       element          element
         Rome          Seattle          Toronto
    Insertion of an Element at the
                  Tail
Before the insertion:

     head                                tail


                next              next             next


  element       element           element
         Rome           Seattle          Toronto
How to keep “head” and “tail”?

 public class Head_and_Tail {
      Node head;
      Node tail;

        Head_and_Tail(Node x, Node y)
          {
             head = x;
             tail = y;
        }
 }
How to keep “head” and “tail”?

public class GeneratingList {
   Node head = null;
   Node tail = null;

Public Head_and_Tail linked_list () {
   Node x = null;
   for (int i = 0; i < 10; i++)
        {x = new Node(); x.setElement(new Integer(i));
           if (i == 0 ) {x.setNext(null); tail = x;}
           else x.setNext(head);
           head = x;
   }
      return new Head_and_Tail(head, tail);}
}
Have a new node:

    head                              tail


              next             next             next           next


  element     element          element            element
       Rome          Seattle          Toronto           Baltimore



    Node x = new Node( );
    x.setElement(new String(“Baltimore”));
    x.setNext(null);
    tail.setNext(x);
    tail = x;
After the insertion:

     head                                          tail

                next             next             next           next


   element      element          element      element
         Rome          Seattle          Toronto           Baltimore
   Deleting an Element at the Tail
Deletion of an element at the tail of a singly linked list takes
more effort.

The difficulty is related with the fact that the last node does not
have a link to the previous node which will become the new
tail of the list.
                       Wiley          Gary



        Bill

                           McFee


Scott          Scott: Who is McFee?
               Gary: I don’t know.
Before the deletion:

     head                                          tail

                next             next             next           next


  element       element          element      element
         Rome          Seattle          Toronto           Baltimore
Remove the node: How can we find the new tail?

    head                                                tail ?

               next             next             next              next


  element      element          element            element
        Rome          Seattle          Toronto              Baltimore




                                                    should be removed
How to insert a new node in the middle of a singly linked
list?

How to remove a node which in the middle of a singly
linked list?
   Data Structure Exercises 5.1
Write a Java program to create a linked list as shown below.

 head                                        tail

                          ……                         

  0          1                           9

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:20
posted:10/2/2011
language:English
pages:30