FCS 02 LinkedList

download FCS 02 LinkedList

of 28

Transcript of FCS 02 LinkedList

  • 7/25/2019 FCS 02 LinkedList

    1/28

    Foundation of Computing Systems

    Lecture 2

    Linked Lists

  • 7/25/2019 FCS 02 LinkedList

    2/28

    29.07.09 IT 60101: Lecture #2 2

    Array vs. Linked List

    Array

    elements are stored in a contagious memorylocations

    static data structure

    Linked list

    adacency !et"een any t"o elements aremaintained !y means o linksorpointers

    dynamic data structures

  • 7/25/2019 FCS 02 LinkedList

    3/28

    29.07.09 IT 60101: Lecture #2 $

    Linked List

    % linked list is an orderedcollection o inite&

    'omogeneousdata elements called nodes "'ere

    t'e linear order is maintained !y means o links or

    (ointers

    )ingle linked list& circular linked list& and dou!le

    linked list

    *%T%

    LI+,

    . Link to t'e ne-t node

  • 7/25/2019 FCS 02 LinkedList

    4/28

    29.07.09 IT 60101: Lecture #2

    Linked List Representation: Static

    / % *

    9

    + 1$ 3+ 2

    6 + $

    1

    +

    7 2

    +

    3 0

    + 6

    $ 3

    4

    1

    9

    3 0

    4

    7 2

    6

    7

    $

    0

    1

    * % T % L I + ,

    1

    2

    $

    6

    7

    3

    9

    0

    / e a d e r

    .

    .

    .

    .

    .

    .

    .

    .

    .

    5 e m o r yL o c a t i o n

    % r r a y o # ( o i n t e r s

  • 7/25/2019 FCS 02 LinkedList

    5/28

    29.07.09 IT 60101: Lecture #2

    Linked List Representation: DynamicA V A I L

    N E W

    H E A D E RX

    X

    6 7

    6 7

  • 7/25/2019 FCS 02 LinkedList

    6/28

    29.07.09 IT 60101: Lecture #2 6

    Linked List Representation: Dynamic

    A V A I L

    H E A D E R

  • 7/25/2019 FCS 02 LinkedList

    7/28

    29.07.09 IT 60101: Lecture #2 7

    Operations on Single Linked List

    Traversinga list

    Searchingor an element in a list

    Insertiono a node into a list

    Deletiono a node rom a list

    Copya linked list to make a du(licate

    Mergingt"o linked lists into a larger list

  • 7/25/2019 FCS 02 LinkedList

    8/28

    29.07.09 IT 60101: Lecture #2 3

    Single Linked List: nsertion

    Insertion ste(s

    8et a ne" node rom memory !ank

    )tart rom t'e 'eader node

    5anage links to

    Insert at ront

    Insert at end

    Insert at any (osition

  • 7/25/2019 FCS 02 LinkedList

    9/28

    29.07.09 IT 60101: Lecture #2 9

    Single Linked List: nsert at Front

    / % *

    !

    n e "

    1

    2

    r o mm e m o r y! a n k

  • 7/25/2019 FCS 02 LinkedList

    10/28

    29.07.09 IT 60101: Lecture #2 10

    Single Linked List: nsert at "nd

    / % * !

    n e "

    ( t r

    1

  • 7/25/2019 FCS 02 LinkedList

    11/28

    29.07.09 IT 60101: Lecture #2 11

    Single Linked List: nsert at Any #lace

    / % *

    !

    n e "

    ( t r

    , 0 7

    12

    6

  • 7/25/2019 FCS 02 LinkedList

    12/28

    29.07.09 IT 60101: Lecture #2 12

    Single Linked List: Deletion

    *eletion ste(s

    )tart rom t'e 'eader node

    5anage links to *elete at ront

    *elete at end

    *elete at any (osition

    eturn t'e deleted node to memory !ank

  • 7/25/2019 FCS 02 LinkedList

    13/28

    29.07.09 IT 60101: Lecture #2 1$

    Single Linked List: Delete at Front

    / % *

    ,

    ( t r ( t r 1

    1

  • 7/25/2019 FCS 02 LinkedList

    14/28

    29.07.09 IT 60101: Lecture #2 1

    Single Linked List: Delete at "nd

    / % *

    ( t r 1 ( t r

    e t u r n t o t ' e m e m o r y ! a n k

  • 7/25/2019 FCS 02 LinkedList

    15/28

    29.07.09 IT 60101: Lecture #2 1

    Single Linked List: Delete at Any #lace

    / % *

    ,

    ( t r 1 ( t r

    1

    e t u r n t o t ' e m e m o r y ! a n k

  • 7/25/2019 FCS 02 LinkedList

    16/28

    29.07.09 IT 60101: Lecture #2 16

    Single Linked List: Copy

    . . .

    . . .

    / % * 1

    / % * 2

    L 1

    L 2

    T o m e m o r y ! a n k

  • 7/25/2019 FCS 02 LinkedList

    17/28

    29.07.09 IT 60101: Lecture #2 17

    Circular Linked List

    / 0 % * 0

  • 7/25/2019 FCS 02 LinkedList

    18/28

    29.07.09 IT 60101: Lecture #2 13

    $erging %&o Circular Linked Lists

    . . .

    . . .

    1

    2

    / % * 1

    / % * 2

    ( t r 1

    ( t r 2( t r 2

    e t u r n t o m e m o r y

  • 7/25/2019 FCS 02 LinkedList

    19/28

    29.07.09 IT 60101: Lecture #2 19

    Dou'le Linked List

    . . .

    * % T %

    L L I + , L I + ,

  • 7/25/2019 FCS 02 LinkedList

    20/28

    29.07.09 IT 60101: Lecture #2 20

    Dou'le Linked List: nsertion

    . . .

    + :

    / % * ( t r

    a ; I n s e r t i o n a t t ' e # r o n t

    . . .

    / % * ( t r

    + :

    ! ; I n s e r t i o n a t t ' e e n d

    ( t r

    + :

    c ; I n s e r t i o n a t a n y i n t e r m e d i a t e ( o s i t i o n

  • 7/25/2019 FCS 02 LinkedList

    21/28

    29.07.09 IT 60101: Lecture #2 21

    Dou'le Linked List: Deletion

    . . .

    / % *

    ( t r

    a ; * e l e t i o n a t t ' e # r o n t

    . . .

    / % * ( t r

    ! ; * e l e t i o n a t t ' e e n d

    c ; * e l e t i o n a t a n i n t e r m e d i a t e ( o s i t i o n

    ( t r

    e t u r n t o t ' e m e m o r y ! a n k

    1

    / % * ( t r ( t r 2

    e t u r n t o t ' e m e m o r y ! a n k

    ( t r 1

    ( t r 1

    ,

  • 7/25/2019 FCS 02 LinkedList

    22/28

    29.07.09 IT 60101: Lecture #2 22

    Applications of Linked Lists

    )(arse matri- mani(ulation

  • 7/25/2019 FCS 02 LinkedList

    23/28

    29.07.09 IT 60101: Lecture #2 2$

    Application of Linked List: Sparse $atri(

    i j

    * % T %

    = : L I + ,

    > = L L I + ,

  • 7/25/2019 FCS 02 LinkedList

    24/28

    29.07.09 IT 60101: Lecture #2 2

    Application of Linked List: Sparse $atri(

    1

    2

    $

    6

    1 2 $

    ? ?

    ? X ? ? M

    ? ? ? ? ?

    P

    ?

    K

    A ? ?

    ? ? ? O

    ? L ? ?

    C ? ? B

    o "

    > o l u m n

  • 7/25/2019 FCS 02 LinkedList

    25/28

    29.07.09 IT 60101: Lecture #2 2

    Application of Linked List: Sparse $atri(/ % *

    6 0 1 0 2> / 1 > / 2 > / $ > / > /

    / 1

    / 2

    / $

    /

    /

    / 6

    1 0

    2 0

    $ 0

    0

    0

    6 0

    1

    $

    2 22

    1 $

    6 1 6 2

    K C B

    L

    P

    A

    X M

    O

    6

    < o i n t s t o / % *

  • 7/25/2019 FCS 02 LinkedList

    26/28

    29.07.09 IT 60101: Lecture #2 26

    Application of Linked List: #olynomial

    P@; A anenB an1

    en1B C C C B a1e1

    >=099 06

  • 7/25/2019 FCS 02 LinkedList

    27/28

    29.07.09 IT 60101: Lecture #2 27

    Application of Linked List: #olynomial

    or (olynomial mani(ulation

    )ee t'e !ook

    Classic Data Structures

    C)apter *

  • 7/25/2019 FCS 02 LinkedList

    28/28

    29.07.09 IT 60101: Lecture #2 23

    Application of Linked List: $emory

    or memory management

    )ee t'e !ook

    Classic Data Structures

    C)apter *