投影片 1 by dandanhuanghuang

VIEWS: 22 PAGES: 30

									資料結構 (Data Structure)
Ch 04b: Linked List


        資訊工程學系副教授
        http://www.twcrypt.org
        wcyang@isu.edu.tw
        2007.09~2008.01
           CSIE: Data Structure, Introduction


    Outline
    1.   Singly Linked Lists and Chains
    2.   Representing Chains in C
    3.   Linked Stacks and Queues
    4.   Polynomials
    5.   Additional List Operations    Example
    6.   Equivalence Classes
    7.   Sparse Matrices
    8.   Doubly Linked Lists




Sunday, March 04, 2012                           Wu-Chuan Yang, 2/30
            CSIE: Data Structure, Introduction


    3. Linked Stacks and Queues
      Top

                                                   …

                              Linked stack              NULL
                                                 Rear
       Front

                                                   …

                                                        NULL
                                Linked queue

Sunday, March 04, 2012                                  Wu-Chuan Yang, 3/30
            CSIE: Data Structure, Introduction


    Example: Represent a linked stack

      top

                                                      …

                              Linked stack                        NULL

                                          typedef struct stack *stack_pointer;
   typedef struct {                       typedef struct stack {
           int key;                               element item;
           /* other fields */                     stack_pointer link;
           } element;                             };
                                          stack_pointer top;
Sunday, March 04, 2012                                            Wu-Chuan Yang, 4/30
            CSIE: Data Structure, Introduction


    Example: Push and Pop in a linked stack

      top

            10                20                 30   …   70


                              Linked stack                    NULL

                                          Push:
        temp                              temp->link = top;
                                          top = temp;
             XXX                          Pop:
                                          temp = top;
                                          top = top->link;

Sunday, March 04, 2012                                        Wu-Chuan Yang, 5/30
           CSIE: Data Structure, Introduction


    4. Polynomials
       Polynomial Representation
        typedef struct poly_node *poly_pointer;
        typedef struct poly_node {
           int coef;
           int expon;
           poly_pointer link;
        };
        poly_pointer a, b, c;


       A( x)  am 1 x            em1
                                          am  2 x    em2
                                                                  a0 x    e0



                         coef        expon      link

Sunday, March 04, 2012                                             Wu-Chuan Yang, 6/30
           CSIE: Data Structure, Introduction


    Example: Polynomial Representations
          a  3x14  2 x8  1
          b  8x14  3x10  10 x6

           3 14               2    8            1   0


     a                                                  NULL


            8 14               -3 10            10 6


      b                                                 NULL



Sunday, March 04, 2012                                         Wu-Chuan Yang, 7/30
             CSIE: Data Structure, Introduction


    Adding Polynomials
     a->expon == b->expon
            d->coef = a->coef + b->coef;
            a = a->link;
            b = b->link;
            d = d->link;              a  3x14  2 x8  1
     a->expon < b->expon                         b  8 x14  3x10  10 x 6
          d->coef = b->coef;                     d  11x14  3x10  2 x8  10 x 6  1
          b = b->link;
          d = d->link;
     a->expon > b->expon
          d->coef = a->coef;
          a = a->link;
          d = d->link;

Sunday, March 04, 2012                                                 Wu-Chuan Yang, 8/30
           CSIE: Data Structure, Introduction


    5. Additional List Operations
     Invert (Reverse) a list
     Concatenate two list
     Operations for Circular linked list




Sunday, March 04, 2012                          Wu-Chuan Yang, 9/30
           CSIE: Data Structure, Introduction


    Invert single linked lists

                                                           NULL
        lead             lead

             10                 20              30   …    70




   middle
                          temp = middle;
                          middle = lead;
                          lead = lead->link;
                          middle->link = temp;


Sunday, March 04, 2012                                   Wu-Chuan Yang, 10/30
           CSIE: Data Structure, Introduction


    Invert single linked lists


                         lead              lead        3)


            4)


                 10              20               30




                                                1) temp = middle;
                                                2) middle = lead;
    middle            middle                    3) lead = lead->link;
   1) temp                       2)             4) middle->link = temp;

Sunday, March 04, 2012                                           Wu-Chuan Yang, 11/30
             CSIE: Data Structure, Introduction


    Circular Linked List
     Example: In compute e(x) =a(x)*b(x)+d(x)
          Circular linked list may be used.
            • Chain: A list with NULL ending (not a circular list).
          The operations in circular linked list should be modified.


          b          a

                         10                20          …       70




         a

                 -                10              20       …          70

Sunday, March 04, 2012                                              Wu-Chuan Yang, 12/30
             CSIE: Data Structure, Introduction


    Operations for Circular Linked List
     All operations in linked list should be modified for
      circular linked list.
            create
            insert (insert_front)
            delete
            length
            …




Sunday, March 04, 2012                             Wu-Chuan Yang, 13/30
           CSIE: Data Structure, Introduction


    6. Equivalence Classes
     Equivalence relations
          A relation over a set S, is to be an equivalence relation over S,
           iff it is symmetric, reflexive, and transitive over S.
              • Symmetric  x = x
              • Reflexive  if x = y, then y = x.
              • Transitive  if x = y and y = z, then x = z.
          Example
              • 0=4, 3=1, 6=10, 8=9, 7=4, 6=8, 3=5, 2=11, 11=0
              • three equivalent classes {0,2,4,7,11}, {1,3,5}, {6,8,9,10}


            What kind of data structure should be adopted?




Sunday, March 04, 2012                                           Wu-Chuan Yang, 14/30
           CSIE: Data Structure, Introduction


    Example of equivalence classes
              • 0=4, 3=1, 6=10, 8=9, 7=4, 6=8, 3=5, 2=11, 11=0
              • three equivalent classes {0,2,4,7,11}, {1,3,5}, {6,8,9,10}

                     [0] [1] [2]      [3] [4] [5] [6] [7]    [8] [9] [10] [11]




           data      11      3   11   5    7    3   8    4   6    8   6     0
           link              0   0              0        0        0   0


           data          4            1    0        10       9              2
           link          0            0    0        0        0              0




Sunday, March 04, 2012                                                    Wu-Chuan Yang, 15/30
           CSIE: Data Structure, Introduction


    7. Sparse Matrices
     Sparse matrices
          # of non-zeros is very less than # of zeros.
          Circular linked list can be used.



                         0  0 11 0 
                         12 0 0  0 
                                    
                         0 4 0  0 
                                    
                          0 0 0  15




Sunday, March 04, 2012                                    Wu-Chuan Yang, 16/30
           CSIE: Data Structure, Introduction


    Data structure of sparse matrix


                                   down     head        right           First row
         First column                         next



                              down entry        row      col    right

                                                value



                                      entry       i        j

                                                 aij


Sunday, March 04, 2012                                                    Wu-Chuan Yang, 17/30
            CSIE: Data Structure, Introduction


    Example of sparse matrices

   Matrix                            H0          H1    H2          H3
   head              4 4



                                                       0 2
               H0
                                                       11


                                     1 0
               H1
                                     12


                                                 2 1
               H2
                                                 -4


                                                                   3 3
               H3
                                                                   -15




Sunday, March 04, 2012                                       Wu-Chuan Yang, 18/30
           CSIE: Data Structure, Introduction


    8. Doubly Linked Lists
     Doubly linked list
          Move in forward and backward direction.
            • Singly linked list is in forward direction only.
          Every node should use 2 links (left and right).
          The operations of list should be modified.




                                       data




Sunday, March 04, 2012                                           Wu-Chuan Yang, 19/30
           CSIE: Data Structure, Introduction


    Data Structure of doubly linked list

         typedef struct node *node_pointer;
         typedef struct node {
             node_pointer llink;
             element item;
             node_pointer rlink;
         }




                empty


Sunday, March 04, 2012                          Wu-Chuan Yang, 20/30
           CSIE: Data Structure, Introduction


    Insertion of doubly linked list

                    a

                                                b

                                          3)
                   a                      4)

                             1)            b        2)


   1) b->llink = a;
   2) b->rlink = a->rlink;
   3) a->rlink->llink = b;
   4) a->rlink = b;
Sunday, March 04, 2012                                   Wu-Chuan Yang, 21/30
           CSIE: Data Structure, Introduction


    Deletion of doubly linked list

                         a                      b    c


                                                1)
                         a                           c
                                                2)

                                                 b

                1) b->rlink->llink = b->llink;
                2) b->llink->rlink = b->rlink;
                3) free(b)



Sunday, March 04, 2012                                   Wu-Chuan Yang, 22/30
           CSIE: Data Structure, Introduction


    Example: Recursive
     Definition
          functions in which the function being defined(called) is applied
           within its own definition.
             • Direct: A call A
             • Indirect: A call B, B call A
          Ending condition should be defined.
          Source code is very compress




                                                A Sierpinski triangle
Sunday, March 04, 2012                                              Wu-Chuan Yang, 23/30
           CSIE: Data Structure, Introduction


    Example: Tower of Hanoi
     move the entire stack to another peg, obeying the
      following rules:
          Only one disk may be moved at a time.
          Each move consists of taking the upper disk from one of the
           pegs and sliding it onto another peg, on top of the other disks
           that may already be present on that peg.
          No disk may be placed on top of a smaller disk.




                         The figure is from wikipedia
Sunday, March 04, 2012                                          Wu-Chuan Yang, 24/30
           CSIE: Data Structure, Introduction


    Solution of Tower of Hanoi (1/3)
     Top (n-1) disks are from A to B




              A                                 B   C


Sunday, March 04, 2012                              Wu-Chuan Yang, 25/30
           CSIE: Data Structure, Introduction


    Solution of Tower of Hanoi (2/3)
     Top (n-1) disks are from A to B
     The n-th disk is from A to C.




              A                                 B   C


Sunday, March 04, 2012                              Wu-Chuan Yang, 26/30
           CSIE: Data Structure, Introduction


    Solution of Tower of Hanoi (3/3)
     Top (n-1) disks are from A to B
     The n-th disk is from A to C.
     Top (n-1) disks are from B to C




              A                                 B   C


Sunday, March 04, 2012                              Wu-Chuan Yang, 27/30
           CSIE: Data Structure, Introduction


    Source code of Tower of Hanoi
         #include <stdio.h>

         void hanoi(int n, char a, char b, char c) {
           if(n==1)
             printf("Disk %d: %c -->%c\n",n,a,c);
           else {
             hanoi(n-1,a,c,b);
             printf("Disk %d: %c -->%c\n",n,a,c);
             hanoi(n-1,b,a,c);
           }
         }

         main() {
           hanoi(3,'A','B','C');
         }



Sunday, March 04, 2012                                 Wu-Chuan Yang, 28/30
           CSIE: Data Structure, Introduction


    Analysis of Tower of Hanoi
                                              Hanoi
                                             3, A B C

                                                 4

                     Hanoi                   3: AC              Hanoi
                    2, A C B                                    2, B A C

                          2                                          6

        Hanoi                      Hanoi              Hanoi                  Hanoi
                    2: AB                                      2: BC
       1, A B C                   1, C B A           1, B C A               1, A B C
             1                           3               5                         7

       1: AC                     1: CB             1: BA                  1: AC

Sunday, March 04, 2012                                                   Wu-Chuan Yang, 29/30
           CSIE: Data Structure, Introduction


    How many steps of Tower of Hanoi?




                A model set of the Towers of Hanoi (with 8 disks)


                          2n–1 steps


Sunday, March 04, 2012                                              Wu-Chuan Yang, 30/30

								
To top