Linked List Presentation

Document Sample
Linked List Presentation Powered By Docstoc
					Linked List Data Structure
 Definition
       A collection of nodes that together form a
       linear ordering

head                next              next              next
                           Ø                 Ø                 Ø
   element            element           element

             Bird               Cat               Dog
Arrays vs. Linked List
  The size must be     Does not have a
 defined              predetermined fixed
 There maybe unused   size
 cells                 Uses space
                      proportional to the
                      number of elements
Singly Linked List


                           next




                 element
Array Declaration in Java
                     int X[] = new int [5]



                        X[4]
                        X[3]
                        X[2]          30
                        X[1]          25
                        X[0]          10

 Arrays vs. Linked
Basic operations of SLL
                     head

                                                      Ø

            Octopus         Shark   Dolphin   Whale



  Inserting            a new element at the head

 Arrays vs. Linked
Basic operations of SLL
 head


                                                Ø

        Octopus    Shark      Dolphin   Whale



                  After   Insertion
Basic operations of SLL
        head

                                         Ø

   Octopus     Shark   Dolphin   Whale


   Deleting the element at the head
Basic operations of SLL
      head

                                       Ø

             Shark   Dolphin   Whale


             After deletion
Basic operations of SLL
 head                     tail

                                         Ø                Ø
        Shark      Dolphin       Whale          Octopus

            Inserting   a new element at the tail


   head                                  tail

                                                          Ø
           Shark      Dolphin       Whale       Octopus
Basic operations of SLL
   head                      tail

                                                        Ø
          Shark     Dolphin         Whale     Octopus

          Deleting       the node at the tail

          head                      tail

                                                   Ø
                  Shark      Dolphin       Whale
Sample programs of linked list

          Linked List in Java



           Linked List in C++
Class ListNode()
     /**
      * --------------------------------------------------
      * Basic node stored in a linked list.
       * --------------------------------------------------
      */
     class ListNode {
        Object element;
        ListNode next;
        ListNode(Object theElement) {
           this(theElement, null);
        }
        ListNode(Object theElement, ListNode n) {
           element = theElement;
           next = n;
        }
     }
Class LinkedList
public class LinkedList {            public void insert(Object x,
  private ListNode header;           LinkedListItr p) {
  public LinkedList() {                    if (p != null && p.current !=
     header = new ListNode(null);    null) {
  }                                           p.current.next = new
   public boolean isEmpty() {        ListNode( x, p.current.next );
     return header.next == null;           }
  }                                     }
  public void makeEmpty() {          public LinkedListItr find(Object x)
     header.next = null;             {
  }                                        ListNode itr = header.next;
   public LinkedListItr zeroth() {         while (itr != null &&
     return new                      !itr.element.equals(x)) {
LinkedListItr(header);                        itr = itr.next;
  }                                        }
   public LinkedListItr first() {          return new LinkedListItr(itr);
     return new LinkedListItr           }
(header.next);
  }
Class LinkedList
public LinkedListItr find(Object x)    public void remove(Object x) {
{
      ListNode itr = header.next;           LinkedListItr p =
      while (itr != null &&            findPrevious( x );
!itr.element.equals(x)) {
         itr = itr.next;                    if (p.current.next != null) {
      }                                        p.current.next =
      return new LinkedListItr(itr);   p.current.next.next;
   }                                        }
 public LinkedListItr
findPrevious(Object x) {                   }
      ListNode itr = header;
      while (itr.next != null &&       }
!itr.next.element.equals(x)) {
         itr = itr.next;
      }
      return new LinkedListItr(itr);
   }
Class LinkedListItr()
public class LinkedListItr {    public Object retrieve() {
                                     return isPastEnd() ? null :
  ListNode current;
                                current.element;
                                  }
    LinkedListItr(ListNode
theNode) {                          public void advance() {
      current = theNode;
                                        if (!isPastEnd()) {
   }
                                           current = current.next;
   public boolean                       }
isPastEnd() {
      return current == null;       }
   }
                                }
Class LinkedListApp()
public class LinkedListApp {               public static void main(String[]
    private static void prtln() {         args) {
       System.out.println();
    }                                         LinkedList theList = new
    private static void prt(String s) {   LinkedList();
       System.out.print(s);                   LinkedListItr theItr;
    }
    private static void                        theItr = theList.zeroth();
printList(LinkedList theList) {                printList(theList);
       if (theList.isEmpty()) {
          prt("[Empty list]");                 for (int i=0; i < 10; i++) {
       } else {                                  theList.insert( new
          LinkedListItr itr =             Integer(i), theItr);
theList.first();                                 printList(theList);
          for( ; !itr.isPastEnd();               theItr.advance();
itr.advance() ) {                              }
             prt(itr.retrieve() + " ");
          }                               }
       }                                  }
       prtln();
    }

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:9
posted:12/11/2011
language:
pages:17