Linked List (Cont)

Embed Size (px)

Citation preview

  • 8/8/2019 Linked List (Cont)

    1/26

    Chapter 3: Linked List (cont.)

    DIT 2014

    Shazana Md Zin, FTMM (2010)

  • 8/8/2019 Linked List (Cont)

    2/26

    Types of Linked List

    o Singly Linked Listo Doubly Linked List

    o Circularly Linked List

  • 8/8/2019 Linked List (Cont)

    3/26

    Singly Linked List

    Each node points to the next one

    There may or may not be head and tail nodes There will always be a head pointer

    A variable pointer (curr) that points to the

    element being accessed

  • 8/8/2019 Linked List (Cont)

    4/26

    Declaration and Creation the Head

    and Curr

  • 8/8/2019 Linked List (Cont)

    5/26

    Singly Linked Listcont.

    A B C

    Head

    NULL

  • 8/8/2019 Linked List (Cont)

    6/26

    Singly Linked Listcont.

    head

    curr

    Abdullah

    070001

    90

    A

    Badrul

    070002

    80

    B

    Hazimi

    070003

    70

    C

    Name

    StudID

    Mark

    Grade

    *next

    members

  • 8/8/2019 Linked List (Cont)

    7/26

    Insert Node at Beginning

    A B C

    HEAD

    NULL

  • 8/8/2019 Linked List (Cont)

    8/26

    Insert Node in Middle

    A

    B

    C

    HEAD

    NULL

    prev

    curr

    newNode = new Node(value);

    newNode->next = curr;

    prev->next = newNode;

  • 8/8/2019 Linked List (Cont)

    9/26

    Insert Node at End

    A

    HEAD

    NULL

    B C

  • 8/8/2019 Linked List (Cont)

    10/26

    Delete First Node

    A B C

    HEAD

    NULL

  • 8/8/2019 Linked List (Cont)

    11/26

    Delete Node in Middle

    A B C

    HEAD

    NULL

    prevcurr

  • 8/8/2019 Linked List (Cont)

    12/26

    Delete Node at End

    A B C

    HEAD

    NULL

  • 8/8/2019 Linked List (Cont)

    13/26

    Doubly Linked List

    The unique feature is each node has twopositions reserved for pointers

    The first position is used to point the nextnode

    The second is used to point the previous node

    This allows the list to be traversed in twodirections

    There is no need for a fixed head pointer

  • 8/8/2019 Linked List (Cont)

    14/26

    Doubly Linked Listcont.

    One of the powerful implementations

    Each node has a pointer to both its successorand its predecessor

  • 8/8/2019 Linked List (Cont)

    15/26

    Doubly Linked Listcont.

    B CA

    NULL

    HEAD

    prev succcurr

  • 8/8/2019 Linked List (Cont)

    16/26

    Doubly Linked Listcont.

    Abdullah

    070001

    90

    A

    Badrul

    070002

    80

    B

    Hazimi

    070003

    70

    C

    Name

    StudID

    Mark

    Grade

    *next

    members

    *prev

    curr

  • 8/8/2019 Linked List (Cont)

    17/26

    Create Double Linked List

  • 8/8/2019 Linked List (Cont)

    18/26

    Insert Node

    HEAD

    NULL

    prevcurr

    newNode = new Node(value);

    newNode.next = curr;

    prev.next = newNode;

    C

    B

    A

    newNode.prev = prevNode;Curr.prev = newNode;

  • 8/8/2019 Linked List (Cont)

    19/26

    Delete Node

    HEAD

    NULL

    CBA

    prev

    currsucc

    succNode = curr.next;

    prevNode.next = succNode;

  • 8/8/2019 Linked List (Cont)

    20/26

    Circularly Linked Lists

    The last nodes link points to the first node ofthe list

    Allow access to nodes in the middle of the listwithout starting at the beginning

    Insertion into and deletion from a circularly

    follow the same logic patterns used in a singly,except the last node points to the first node

  • 8/8/2019 Linked List (Cont)

    21/26

    Circularly Linked Listscont.

    It only requires a single variable pointer, whichcan move to different nodes

    The last element points back to the first

  • 8/8/2019 Linked List (Cont)

    22/26

    Circularly Linked Listscont.

    Abdullah

    070001

    90

    A

    Badrul

    070002

    80

    B

    Hazimi

    070003

    70

    C

    Name

    StudID

    Mark

    Grade

    *next

    members

    curr

  • 8/8/2019 Linked List (Cont)

    23/26

    Circularly Linked Listscont.

    HEAD

    CBA

    TAIL

  • 8/8/2019 Linked List (Cont)

    24/26

    Problem

    1

    2

    3

    4

    Amia

    Juana Siti

    Roslan

    Student List

  • 8/8/2019 Linked List (Cont)

    25/26

    Using Linked List

    Juana

    Amia

    Siti

    Roslan

    1

    2

    3

    4

    Student List

  • 8/8/2019 Linked List (Cont)

    26/26

    Thank You