lecture 5 link list jesmin slide

Upload: jesmin-rahaman

Post on 30-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    1/27

    Lecture 5

    on

    Data Structure

    Linked Lists

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    2/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Why linked lists

    Disadvantages of arrays as storage data structures:

    slow searching in unordered array

    slow insertion in ordered array

    Fixed size

    Linked lists solve some of these problems Linked lists are general purpose storage data structures and are versatile.

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    3/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Linked lists

    A linked list, or one way list, is a linear collection of data elements,

    called nodes, where the linear order is given by means of pointers.Each node is divided into two parts: The first part contains the

    information of the element, and the second part, called the link field or

    next pointer field, contains the address of the next node in the list.

    The pointer of the last node contains a special value,called the null

    pointer.

    A list pointer variable called START which contains the address of

    the first node.A special case is the list that has no nodes, such a list is called the

    null list or empty list and is denoted by the null pointer in the variable

    START.

    Start

    Data Nextnode node

    Data

    Linked list with 3 nodes

    Data Nextnode

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    4/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    linked lists

    A linked list organizes a collection of data items (elements ) such that elements

    can easily be added to and deleted from any position in the list.

    Only references To next elements are updated in insertion and deletion

    operations.

    There is no need to copy or move large blocks of data to facilitate insertion anddeletion of elements.

    Lists grow and shrink dynamically.

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    5/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Representation of Linked lists in memory

    1

    2

    3

    4

    5

    6

    7

    8

    A

    M

    G

    N

    O

    35

    2

    7

    4

    0

    START START=3, INFO[3]=M

    LINK[3]=2, INFO[2]=A

    LINK[2]=5, INFO[5]=N

    LINK[5]=4, INFO[4]=G

    LINK[4]=7, INFO[7]=O

    LINK[7]=0, NULL value, So the list has ended

    INFO LINK

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    6/27

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    7/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Traversing a linked lists

    For printing the information at each node of a linked list, must traverse the list.

    Algorithm 5.1 : PRINT(INFO, LINK, START)

    Algorithm Prints the information at each node of the list.

    Set PTR : =START.

    Repeat steps 3 and 4 whilePTR : NULL:

    Write : INFO[PTR].

    Set PTR : =LINK[PTR].

    Exit.

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    8/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Traversing a linked lists

    For Finding the number NUM of elements in a linked list, must traverse the list.

    Algorithm 5.1 : COUNT(INFO, LINK, START, NUM)

    1. Set NUM: =0.

    2. . Set PTR : =START.

    3. Repeat steps 4 and 5 while PTR : NULL:

    4. Set NUM : =NUM+1.

    5. Set PTR : =LINK[PTR].

    6. Exit.

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    9/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Garbage

    (Deleted Space)

    Memory allocation: Garbage collection

    Memory space can be reused if a node is deleted from a list i.e deleted node can be made available for future use

    Any technique that use this technique called garbage collection

    Computer

    programs

    Periodic

    Collector

    Avail List (Free space)

    Sent to

    avail list

    Takes

    space

    avail list

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    10/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Overflow and Underflow

    Overflow:

    Sometimes data are inserted into a data structure but there is noavailable space.

    This situation is called overflow

    Example: in linked list overflow occurs when AVAIL= NULL and

    There is an insertion operation

    Underflow:

    Situation:

    Want to delete data from data structure that is empty. Example: in linked list overflow occurs when

    START = NULL and

    There is an deletion operation

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    11/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    12/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Solved problems

    5.1, 5.2, 5.3, 5.4,5.5, 5.10, 5.11

    Supplementary problems

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    13/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    List Overview

    Linked lists

    Abstract data type (ADT)

    Basic operations of linked lists

    Insert, find, delete, print, etc.

    Variations of linked lists

    Circular linked lists

    Doubly linked lists

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    14/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Linked Lists

    A linked listis a series of connected nodes

    Each node contains at least

    A piece of data (any type)

    Pointer to the next node in the list

    Head: pointer to the first node

    The last node points to NULL

    A

    Head

    B C

    A

    data pointer

    node

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    15/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Inserting a new node

    Possible cases ofInsertNode

    1. Insert into an empty list

    2. Insert in front

    3. Insert at back

    4. Insert in middle

    But, in fact, only need to handle two cases

    Insert as the first node (Case 1 and Case 2)

    Insert in the middle or at the end of the list (Case 3 and Case 4)

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    16/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Inserting a new node

    INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

    1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit

    2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]

    3. Set INFO[NEW]= ITEM

    4. IF LOC = NULL then [Insert as first Node]

    Set LINK[]= START and START=NEW.

    Else: [Insert after node with location LOC]

    Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW

    5. Exit.

    Check for available

    memory

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    17/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Inserting a new node

    INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

    1. [OVERFLOW?] If AVAIL=NULL,then print OVERFLOW andexit

    2. SetNEW= AVAIL and AVAIL=LINK[AVAIL]

    3. SetINFO[NEW]=ITEM

    4. IF LOC =NULLthen [Insert asfirstNode]

    SetLINK[]= START and START=NEW.

    Else: [Insert afternode with locationLOC]

    SetLINK[NEW]=LINK [LOC] andLINK[LOC]=NEW

    5. Exit.

    Create a new node

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    18/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Inserting a new node

    INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

    1. [OVERFLOW?] If AVAIL=NULL,then print OVERFLOW andexit

    2. SetNEW= AVAIL and AVAIL=LINK[AVAIL]

    3. SetINFO[NEW]=ITEM

    4. IF LOC =NULLthen [Insert asfirstNode]

    SetLINK[]= START and START=NEW.

    Else: [Insert afternode with locationLOC]SetLINK[NEW]=LINK [LOC] andLINK[LOC]=NEW

    5. Exit.

    Insert as first element

    head

    newNode

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    19/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Inserting a new node

    INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

    1. [OVERFLOW?] If AVAIL=NULL,then print OVERFLOW andexit

    2. SetNEW= AVAIL and AVAIL=LINK[AVAIL]

    3. SetINFO[NEW]=ITEM

    4. IF LOC =NULLthen [Insert asfirstNode]

    SetLINK[]= START and START=NEW.

    Else: [Insert afternode with locationLOC]SetLINK[NEW]=LINK [LOC] andLINK[LOC]=NEW

    5. Exit.

    Insert aftercurrNode

    LOC

    NEW

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    20/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Finding a Node

    FINDB(INFO, LINK, START, ITEM, LOC, LOCP)This procedure finds the location LOC of the first node N which contains ITEM

    and the location LOCP of the node preceding node N.

    If ITEM does not appear in the list, then sets LOC=NULL

    If ITEM appear in the first node, then sets LOCP=NULL

    1. [List Empty?] If START = NULL then:

    Set LOC = NULL and LOCP = NULL and return.

    2. [ITEM in the first node?] If INFO[] = ITEM, then:

    Set LOC = START and LOCP = NULL and return.

    3. Set SAVE=START and PTR = LINK[START] [initialize pointer]

    4. Repeat step 5 and 6 while PTR NULL.

    5. If INFO[] = ITEM then:

    Set LOC= PTR and LOCP = SAVE and Return.6. Set SAVE = PTR and PTR= LINK[PTR] [update pointer]

    7. Set LOC= NULL

    8. Exit.

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    21/27

    Prepared by, Jesmin Akhter,Lecturer, IIT, JU

    Deleting a node

    DELETE(INFO, LINK, START, AVAIL, ITEM)

    Delete a node with the value equal to INFO from the list.

    If such a node is found, return its position. Otherwise, return

    NULL.

    Steps

    Find the desirable node (similar to FINDB)

    Release the memory occupied by the found node

    Set the pointer of the predecessor of the found node to the

    successor of the found node

    Like INSLOC, there are two special cases

    Delete first node

    Delete the node in middle or at the end of the list

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    22/27

    P

    repared by, Jesmin Akhter,Lecturer, IIT, JU

    Deleting a node

    DELETE(INFO, LINK, START, AVAIL, ITEM)

    1. Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP)

    2. IF LOC = NULL then: ITEM not in the list and exit.

    3. [Delete node]

    IF LOCP = NULL then:Set START= = LINK[START] [Delete first node]

    Else:

    Set LINK[LOCP] = LINK [LOC]

    4. Return deleted node to the AVAIL list

    Set LINK[LOC] = AVAIL and AVAIL = LOC

    5. Exit.

    Try to find the node withits value equal toN

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    23/27

    P

    repared by, Jesmin Akhter,Lecturer, IIT, JU

    Deleting a node

    LOCSTART

    DELETE(INFO, LINK, START, AVAIL, ITEM)

    1. Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP)

    2. IF LOC = NULL then: ITEM not in the list and exit.

    3. [Delete node]

    IF LOCP = NULL then:Set START= = LINK[START] [Delete first node]

    Else:

    Set LINK[LOCP] = LINK [LOC]

    4. Return deleted node to the AVAIL list

    Set LINK[LOC] = AVAIL and AVAIL = LOC

    5. Exit.

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    24/27

    P

    repared by, Jesmin Akhter,Lecturer, IIT, JU

    Deleting a node

    LOC

    LOCP

    DELETE(INFO, LINK, START, AVAIL, ITEM)

    1. Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP)

    2. IF LOC = NULL then: ITEM not in the list and exit.

    3. [Delete node]

    IF LOCP = NULL then:Set START= = LINK[START] [Delete first node]

    Else:

    Set LINK[LOCP] = LINK [LOC]

    4. Return deleted node to the AVAIL list

    Set LINK[LOC] = AVAIL and AVAIL = LOC

    5. Exit.

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    25/27

    Prepared by, Jesmin Akhter,

    Lecturer, IIT, JU

    Variations of Linked Lists

    Circular linked lists

    The last node points to the first node of the list

    How do we know when we have finished traversing the list?(Tip: check if the pointer of the current node is equal to the

    head.)

    A

    Head

    B C

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    26/27

    Prepared by, Jesmin Akhter,

    Lecturer, IIT, JU

    Variations of Linked Lists

    Doubly linked lists

    Each node points to not only successor but the predecessor

    There are two NULL:at the first and last nodes in the list

    Advantage: given a node, it is easy to visit its predecessor.

    Convenient to traverse lists backwards

    A

    Head

    B

    C

  • 8/9/2019 Lecture 5 Link List Jesmin Slide

    27/27