Docstoc

l6

Document Sample
l6 Powered By Docstoc
					                             ECE 242
                          Data Structures
                              Lecture 6

                            Linked Lists




ECE242 L6: Linked Lists                     September 17, 2012
       Administration

     °HW 1 Submission
     °HW return with solutions on Thursday
     °Lab Submission – details coming
     °TA email
     °TA Office Hours - for best service, come early!
     °Readings!




ECE242 L6: Linked Lists                           September 17, 2012
       Overview

  °Problem: Can we implement data structures using
   something other than arrays?
          • Individual objects can be more flexible
          • Use references to find “neighbors”


  °Iterators will be important tools for linked lists
          • Search through the list


  °Easier to implement than arrays. A little harder to
   visualize conceptually




ECE242 L6: Linked Lists                               September 17, 2012
      ArrayList Implementation - Delete An Item


                              Operation: delete(2)

       maxsize-1                                        maxsize-1

  Count=5
                                                                          Count=4
                 4        E            Remove an item

                 3        D             at position 2          3    E     pos=2
 pos
                 2        C                                    2    D
                 1        B                                    1    B
                 0        A                                    0    A
                              Implementation is slow and costly

                              Need to know max size of array
ECE242 L6: Linked Lists                                                 September 17, 2012
       Another Implementation of List


     °Linked List



                          a           b                c   null

     °Take as much memory as needed
             • no more and no less
             • allocate the memory dynamically if needed

     °No need to move items around




ECE242 L6: Linked Lists                                           September 17, 2012
     Advantage of Linked List



  °Nodes can be located anywhere in the memory.
  °Getting from one node to another by storing the reference of the
   next node
  °Insertion/removal does not require moving other items

    Object

                          a                  b

                                         c


                                             d
                              e   null

ECE242 L6: Linked Lists                                    September 17, 2012
    Simple Linked List


    °Singly Linked List (SLList)
            • A sequence of nodes

    °Each node contains two fields
            • Item
            • Reference to next node



                          a            b               c null
           head

                              Node:
                              its value is “a”, this node
                              also has a link which points
                              to its succeeding node
ECE242 L6: Linked Lists                                         September 17, 2012
     Node Definition


     °Define an abstract object Node:
             private class Node {
                    private Object item;
                    Node     next;
             }

     °Example for Employee Node:
             private class Node {
                    Employee   e;
                    Node     next;
             }




ECE242 L6: Linked Lists                    September 17, 2012
       Singly Linked List --- head/tail




     °Important variables in Singly Linked List
             • head        Indicates the beginning of the List
             • tail        Indicates the end of the List




                                                     tail
             head

                          node1           node2             node3



ECE242 L6: Linked Lists                                             September 17, 2012
     Search() Method In Linked List


°          For an empty list, return empty list


°          For a nonempty list
       •        Search for the wantedNode from head, print the record if matched




           head                                  tail
                              wantedNode

                          node1        node2            node3




ECE242 L6: Linked Lists                                              September 17, 2012
     Size() Method In Listed Link


°          For an empty list, return size 0


°          For a nonempty list
       •        count the total number of nodes in the Linked List




           head                                     tail

                          node1          node2             node3




ECE242 L6: Linked Lists                                              September 17, 2012
     Add New Node

  °          Case 1: for an empty list
         •       set head and tail to newNode


                          head
                                          newNode
                          tail
  ° Case 2: for a non-empty list, find where
    newNode should be inserted
         •       (a) insert in front of head
         •       (b) insert in the middle of list
         •       (c) insert after tail
                                                        tail
                head

                                 node1          node2          node3

ECE242 L6: Linked Lists                                                September 17, 2012
     Add New Node --- Case 2 (a)




    °(a) Insert newNode in front of head



                                                 tail
 head                     head

            newNode              node1   node2          node3




ECE242 L6: Linked Lists                                 September 17, 2012
    Add New Node --- Case 2 (b)




    • (b) Insert newNode in the middle of list
       – Suppose newNode needs to be inserted between node 2
           and node 3

       head                                      tail

                   node1   node2        node3           node4




ECE242 L6: Linked Lists                                 September 17, 2012
    Add New Node --- Case 2 (b)




    • (b) Insert newNode in the middle of list
       – Suppose newNode needs to be inserted between node 2
           and node 3

       head                                      tail

                   node1   node2        node3           node4

                            newNode


ECE242 L6: Linked Lists                                 September 17, 2012
    Add New Node --- Case 2 (b)




    • (b) Insert newNode in the middle of list
       – Suppose newNode needs to be inserted between node 2
           and node 3

       head                                      tail

                   node1   node2        node3           node4

                            newNode


ECE242 L6: Linked Lists                                 September 17, 2012
    Add New Node --- Case 2 (b)




    • (b) Insert newNode in the middle of list
       – Suppose newNode needs to be inserted between node 2
           and node 3

       head                                      tail

                   node1   node2        node3           node4

                            newNode


ECE242 L6: Linked Lists                                 September 17, 2012
     Add New Node --- Case 2 (c)




    °(c) Insert newNode after tail



   head                           tail

               node1      node2          node3




ECE242 L6: Linked Lists                          September 17, 2012
     Add New Node --- Case 2 (c)




    °(c) Insert newNode after tail



   head                           tail

               node1      node2          node3   newNode




ECE242 L6: Linked Lists                           September 17, 2012
     Add New Node --- Case 2 (c)




    °(c) Insert newNode after tail



   head                           tail

               node1      node2          node3   newNode




ECE242 L6: Linked Lists                           September 17, 2012
     Add New Node --- Case 2 (c)




    °(c) Insert newNode after tail



   head                           tail           tail

               node1      node2          node3          newNode




ECE242 L6: Linked Lists                                  September 17, 2012
     Delete Node --- Case 2 (a)



    °(a): wantedNode is both head and tail



             head                                   head=null
                                   after deletion
                          node 1

             tail                                   tail=null




ECE242 L6: Linked Lists                                         September 17, 2012
     Delete Method --- Case 2(b)


    °(b): wantedNode is head, but not tail
            • Suppose node 1 needs to be deleted




                                            tail
          head

                      node1       node2            node3




ECE242 L6: Linked Lists                                    September 17, 2012
     Delete Method --- Case 2(b)


    °(b): wantedNode is head, but not tail
            • Suppose node 1 needs to be deleted




                                             tail
                              head

                      node1          node2          node3




ECE242 L6: Linked Lists                                     September 17, 2012
     Delete Method --- Case 2(b)


    °(b): wantedNode is head, but not tail




                                         tail
                          head

                                 node2          node3




ECE242 L6: Linked Lists                                 September 17, 2012
     Delete Method --- Case 2(c)


    °(c): wantedNode is not head, not tail
            • Suppose node2 needs to be deleted




                                                  tail
        head

                          node1      node2               node3



                                  To be deleted


ECE242 L6: Linked Lists                                          September 17, 2012
     Delete Method --- Case 2(c)


    °(c): wantedNode is not head, not tail
            • Suppose node2 needs to be deleted




                                                  tail
        head

                          node1      node2               node3



                                  To be deleted


ECE242 L6: Linked Lists                                          September 17, 2012
     Delete Method --- Case 2(c)


    °(c): wantedNode is not head, not tail
            • Suppose node2 needs to be deleted




                                                  tail
        head

                          node1                          node3




ECE242 L6: Linked Lists                                          September 17, 2012
     Delete Method --- Case 2(d)




    °(d): wantedNode is not head, but tail


                                          tail
               head

                          node1   node2          node3




ECE242 L6: Linked Lists                                  September 17, 2012
     Delete Method --- Case 2(d)




    °(d): wantedNode is not head, but tail



                                  tail
               head

                          node1          node2   node3




ECE242 L6: Linked Lists                                  September 17, 2012
     Delete Method --- Case 2(d)




    °(d): wantedNode is not head, but tail



                                  tail
               head

                          node1          node2




ECE242 L6: Linked Lists                          September 17, 2012
       Code Example from Book

     °ListNode.java




ECE242 L6: Linked Lists         September 17, 2012
       History

     °Linked lists were developed in 1955-56 by Allen
      Newell, Cliff Shaw and Herbert A. Simon at RAND
      Corporation
     °The problem of machine translation for natural
      language processing led Victor Yngve at
      Massachusetts Institute of Technology (MIT) to use
      linked lists as data structures in 1958.
     °The LISP language, developed in the early 1960’s
      used linked lists as a primary data structure.
     °Several operating systems used singly linked lists
      as file structures




ECE242 L6: Linked Lists                           September 17, 2012
ECE242 L6: Linked Lists   September 17, 2012
       Summary

     °Size grows as needed
     °Data manipulation is efficient
             • Add, remove, etc.

     °Linked List implementation is more efficient than
      array
     °We will learn lots more implementations and uses
      of linked lists in subsequent lectures




ECE242 L6: Linked Lists                           September 17, 2012

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:5/26/2013
language:Unknown
pages:35
lanyuehua lanyuehua http://
About