Chapter 5 Linked List

download Chapter 5 Linked List

of 27

Transcript of Chapter 5 Linked List

  • 8/12/2019 Chapter 5 Linked List

    1/27

    MODULE

    5Linked list

    5.1 Linked List

    In computer science, a linked list is one of the fundamental data

    structures used in computer programming. It consists of a sequence of

    nodes, each containing arbitrar data fields and one or t!o references

    "#links#$ pointing to the ne%t and&or pre'ious nodes. ( linked list is a

    self)referential data tpe because it contains a link to another data of the

    same tpe. Linked lists permit insertion and remo'al of nodes at an

    point in the list in constant time, but do not allo! random access

  • 8/12/2019 Chapter 5 Linked List

    2/27

    Advantages of Linked List:

    ( linked list is a dnamic data structure and therefore the si*e of the

    linked list can gro! or shrink in si*e during e%ecution of the program. (

    linked list does not require an e%tra space therefore it does not !astee%tra memor. It pro'ides fle%ibilit in rearranging the items efficientl.

    +he limitation of linked list is that it consumes e%tra space !hen

    compared to a arra since each node must also contain the address of the

    ne%t item in the list to search for a single item in a linked list is

    cumbersome and time consuming.

    5.2 Operations on linked list

    +here are se'eral operations that !e can think of performing on linked

    lists. +he follo!ing program sho!s ho! to build a linked list b adding

    ne! nodes at the beginning, at the end or in the middle of the linked list.

    It also contains a function displa"$ !hich displas all the nodes present

    in the linked list and a function delete"$ !hich can delete an node in the

    linked list.

    5. Program to maintain a linked list

    -include stdio.h/

    -include conio.h/

    -include alloc.h/

    &0 structure containing a data part and link part 0&

    struct node1

    int data 2

    struct node 0 link 23 2

    'oid append " struct node 00, int $ 2

    'oid addatbeg " struct node 00, int $ 2'oid addafter " struct node 0, int, int $ 2

    'oid displa " struct node 0 $ 2

    int count " struct node 0 $ 2

  • 8/12/2019 Chapter 5 Linked List

    3/27

    'oid delete " struct node 00, int $ 2

    'oid main" $

    1

    struct node 0p 2p 4 ULL 2 &0 empt linked list 0&

    printf " #6no. of elements in the Linked List 4 7d#, count " p $ $ 2

    append " 8p, 9: $ 2append " 8p, ; $ 2

    append " 8p, >> $ 2

    addatbeg " 8p, ??? $ 2

    addatbeg " 8p, === $ 2displa " p $ 2

    addafter " p, =, ; $ 2addafter " p,

  • 8/12/2019 Chapter 5 Linked List

    4/27

    !hile " temp )/ link @4 ULL $

    temp 4 temp )/ link 2

    &0 add node at the end 0&r 4 malloc " si*eof " struct node $ $ 2

    r )/ data 4 num 2

    r )/ link 4 ULL 2temp )/ link 4 r 2

    3

    3&0 adds a ne! node at the beginning of the linked list 0&

    'oid addatbeg " struct node 00q, int num $

    1

    struct node 0temp 2&0 add ne! node 0&

    temp 4 malloc " si*eof " struct node $ $ 2

    temp )/ data 4 num 2

    temp )/ link 4 0q 20q 4 temp 2

    3&0 adds a ne! node after the specified number of nodes 0&

    'oid addafter " struct node 0q, int loc, int num $

    1struct node 0temp, 0r 2

    int i 2

    temp 4 q 2&0 skip to desired portion 0&

    for " i 4 ; 2 i loc 2 iAA $

    1temp 4 temp )/ link 2

    /* if end of linked list is encountered */

    if ( temp !"LL #

    $

    printf ( %&n'ere are less tan )d elements in list% loc # +

    return +

    ,

    ,

    /* insert ne- node */

    r malloc ( sieof ( struct node # # +

    r 0 data num +

    r 0 link temp 0 link +

    temp 0 link r +

    ,

    /* displas te contents of te linked list */

  • 8/12/2019 Chapter 5 Linked List

    5/27

    void displa ( struct node * #

    $

    printf ( %&n% # +

    /* traverse te entire linked list */

    -ile ( 3 !"LL #

    $ printf ( %)d % 0 data # +

    0 link +

    ,

    ,

    /* counts te num4er of nodes present in te linked list */

    int count ( struct node * #

    $

    int c +

    /* traverse te entire linked list */

    -ile ( 3 !"LL #$

    0 link +

    c66 +

    ,

    return c +

    ,

    /* deletes te specified node from te linked list */

    void delete ( struct node ** int num #

    $

    struct node *old *temp +

    temp * +

    -ile ( temp 3 !"LL #

    $

    if ( temp 0 data num #

    $

    /* if node to 4e deleted is te first node in te linked list */

    if ( temp * #

    * temp 0 link +

    /* deletes te intermediate nodes in te linked list */

    else

    old 0 link temp 0 link +

    /* free te memor occupied 4 te node */

    free ( temp # +

    return +

    ,

    /* traverse te linked list till te last node is reaced */

  • 8/12/2019 Chapter 5 Linked List

    6/27

    else

    $

    old temp + /* old points to te previous node */

    temp temp 0 link + /* go to te ne7t node */

    ,

    ,printf ( %&n8lement )d not found% num # +

    ,

    5.9 'pes of Link List

    9$ Linearl)linked List

  • 8/12/2019 Chapter 5 Linked List

    7/27

    Buppose !e !ant to store a list of ints, then the linear linked list can be

    declared as

    tpedefstructnodetpe

    1

    intinfo2 structnodetpe 0ne%t2

    3 node2

    node 0head2

    +he abo'e declaration defines a ne! data tpe, struct nodetpeF, !ith a

    tpedef of nodeF.

    +o insert an element in the list, the first task is to create a ne! node,

    assign the element to be inserted to the info field of the node, and thenplace the ne! node at the appropriate position b adGusting the

    appropriate pointers. Insertion in the list can take place at the follo!ing

    positions

    (t the beginning of the list

    (t the end of the list

    (fter a gi'en element

    a) Insertion at the Beginning of the List

    Hirst, test !hether the linked list is initiall empt, if es, then the

    element is inserted as the first and onl one element b performing the

    follo!ing steps

    (ssign ULL to the ne%t pointer field of the ne! node.

    (ssign the address of the ne! node to head.

    If the list is not empt, then the element is inserted as the first element ofthe list b performing the follo!ing steps

    (ssign the 'alue of head to the ne%t pointer field of the ne! node.

    (ssign the address of the ne! node to head.

  • 8/12/2019 Chapter 5 Linked List

    8/27

    rogrammaticall, both cases are equi'alent. +his is because in both the

    cases the first step is to assign the 'alue of head "ULL or other!ise$ to

    the ne%t pointer field of the ne! node.

    'oidinsertatbeginning"node 00head, intitem$1

    node 0ne!ode2

    &0 allocate memor for the ne! node and initiali*e the data in it0&

    ne!ode4 malloc"si*eof"node$$2

    ne!ode)/info4item2

    &0 assign the 'alue of head to the ne%tF of ne!ode0&

    ne!ode)/ne%t40head2

    &0 assign the address of ne!ode to head 0&

    0head4ne!ode2

    3

    b) Inserting at the End of the List

    Hirst test !hether the linked list is initiall empt, if es, then the

    element is inserted as the first and onl one element b performing the

    follo!ing steps

    )(ssign ULL to the ne%t pointer field of the ne! node.

    )(ssign the address of the ne! node to head.

    If the list is not empt, !e tra'erse it to reach the last element, and then

    the ne! node is inserted as the last element of the list b performing the

    follo!ing steps

    )(ssign ULL to the ne%t pointer field of the ne! node.

    )(ssign the address of the ne! node to the ne%t pointer field of the

    last node.

    'oid insertatend"node 00head, int item$

    1

    node 0ne!ode2

    ne!ode4malloc"si*eof"node$$2

    ne!ode)/info4item2

  • 8/12/2019 Chapter 5 Linked List

    9/27

    ne!ode)/ne%t4ULL2

    if"0head44ULL$

    0head4ne!ode2

    else

    1

    node 0 pre'40head2

    !hile "pre')/ne%t@4ULL$

    pre'4pre')/ne%t2

    pre')/ne%t4ne!ode2

    3

    3

    c) Inserting after Given Element

    +o insert a ne! element after the gi'en element, first !e find thelocation, sa loc, of the gi'en element in the list, and then the element is

    inserted in the list b performing the follo!ing steps

    (ssign the ne%t pointer field of the node pointed to b loc to the

    ne%t pointer field of the ne! node.

    (ssign the address of the ne! node to the ne%t pointer field of the

    node pointed to b loc.

    'oid insertafterelement"node 0head, int item,int after$1

    node 0ne!ode, 0loc2

    loc4searchunsortedlist"head,after$2

    if"loc44ULL$ &0element after not found0&

    return2

    ne!ode4malloc"si*eof"node$$2

    ne!ode)/info4item2

    ne!ode)/ne%t4loc)/ne%t2

    loc)/ne%t4ne!ode23

    5.9.2

  • 8/12/2019 Chapter 5 Linked List

    10/27

    a pointer pointing to the first node in the list and then tra'ersing the

    entire list using this pointer.

    ( circular linked list does not ha'e a first or last node.

    5.9.2 (a#

  • 8/12/2019 Chapter 5 Linked List

    11/27

    +his representation significantl simplifies adding and remo'ing nodes

    !ith a non)empt list, but empt lists are then a special case.

    5.9.2 (4#

  • 8/12/2019 Chapter 5 Linked List

    12/27

    for"i4;2in2iAA$

    1

    printf"#er the element#$2

    scanf"#7d#,8m$2

    create"m$23break2

    case

  • 8/12/2019 Chapter 5 Linked List

    13/27

    default

    printf"#!rong choice#$2

    3

    3

    3create"int data$

    1

    struct node 0q,0tmp2

    tmp4"struct node 0$malloc"si*eof"struct node$$2

    tmp)/info4data2

    tmp)/link4null2

    if"last44null$

    1last4tmp2

    tmp)/link4last2

    3

    else

    1

    tmp)/link4last)/link2

    last)/link4tmp2

    last4tmp233

    addat"int data$

    1

    struct node 0q,0tmp2

    tmp4"struct node 0$malloc"si*eof"struct node$$2

    tmp)/info4data2

    tmp)/link4last)/link2

    last)/link4tmp23

    addbt"int data,int pos$

    1

    struct node 0tmp,0q2

    int i2

  • 8/12/2019 Chapter 5 Linked List

    14/27

    q4last)/link22

    for"i4;2ipos)92iAA$

    1

    q4q)/link2

    if"q44last)/link$1

    printf"#there r lessthan 7d elements#,pos$2

    return2

    3

    3

    tmp4"struct node 0$malloc"si*eof"struct node$$2

    tmp)/link4q)/link2

    tmp)/info4data2q)/link4tmp2

    if"q44last$

    last4tmp2

    3

    del"int data$

    1

    struct node 0tmp,0q2

    if"last)/link44last88last)/info44data$ 1

    tmp4last2

    last4null2

    free"tmp$2

    return2

    3

    q4last)/link2

    if"q)/info44data$ 1

    tmp4q2

    last)/link4q)/link2

    free"tmp$2

    return2

  • 8/12/2019 Chapter 5 Linked List

    15/27

    3

    !hile"q)/link@4last$

    1

    if"q)/link)/info44data$ 1

    tmp4q)/link2

    q)/link4tmp)/link2

    free"tmp$2

    printf"#element 7d is deleted#,data$2

    3

    if"q)/link)/info4data$ 1

    tmp4q)/link2

    q)/link4last)/link2

    free"tmp$2

    last4q2

    return23

    printf"#element7d is not found#,data$2

    3 disp"$

    1

    struct node 0q2

    if"last44null$

    1

    printf"#list isdempt#$2

    return2

    3q4last)/link2

    !hile"q@4last$ 1

    printf"#7d#,q)/info$2

    q4q)/link2

    3

    printf"#7d#,last)/info$2

  • 8/12/2019 Chapter 5 Linked List

    16/27

    3

    5.9.> ;ou4l Linked List

    In a doubl linked list, also called a t!o)!a list, each node is di'idedinto three parts

    9$ +he first part, called the pre'ious pointer field, contains the address of

    the preceding element in the list.

  • 8/12/2019 Chapter 5 Linked List

    17/27

    tpedefstructnodetpe

    1

    structnodetpe 0pre'2

    intinfo2

    structnodetpe 0ne%t23node2

    node 0head,0tail2

    +he abo'e declaration defines a ne! data tpe called struct nodetpeF

    !ith a tpedef of nodeF. +!o node pointers are also declared head and

    tail.

    5.9.> (4#=nserting an 8lement in dou4l Linked List

    +o insert an element in the list, the first task is to allocate memor for a

    ne! node, assign the element to be inserted to the info field of the node,

    and then the ne! node is placed at the appropriate position b adGusting

    appropriate pointers. Insertion in the list can take place at the follo!ing

    positions

    (t the beginning of the list

    (t the end of the list (fter a gi'en element

    Jefore a gi'en element

    a) Insertion at the Beginning of the List

    Hirst, test !hether the linked list is empt, if es, then the element is

    inserted as the first and onl one element b performing the follo!ing

    steps

    (ssign ULL to the ne%t pointer and pre' pointer fields of the ne!

    node

    (ssign the address of the ne! node to head and tail pointer

    'ariables.

  • 8/12/2019 Chapter 5 Linked List

    18/27

    If the list is not empt, then the element is inserted as the first element of

    the list b performing the follo!ing steps

    (ssign ULL to the pre' pointer field of the ne! node.

    (ssign the 'alue of the head 'ariable "the address of the firstelement of the e%isting list$ to the ne%t pointer field of the ne!

    node.

    (ssign the address of the ne! node to pre' pointer field of the

    node currentl pointed b head 'ariable, i.e. first element of the

    e%isting list.

    Hinall assign the address of the ne! node to the head 'ariable

    b)Inserting at the End of the List

    Hirst test !hether the linked list is initiall empt, if es, then the

    element is inserted as the first and onl one element b performing the

    follo!ing steps

    (ssign ULL 'alue to the ne%t pointer and pre' pointer field of

    the ne! node

    (ssign address of ne! node to head and tail pointer 'ariable.

    If the list is not empt, then element is inserted as the last element of the

    list b performing the follo!ing steps

    (ssign ULL 'alue to the ne%t pointer field of the ne! node.

    (ssign 'alue of the tail 'ariable "the address of the last element of

    the e%isting list$ to the pre' pointer field of the ne! node.

    (ssign address of the ne! node to the ne%t pointer field of the

    node currentl pointed b tail 'ariable i.e. last element of the

    e%isting list. Hinall assign the address of the ne! node to tail 'ariable.

    5.9.9 <

  • 8/12/2019 Chapter 5 Linked List

    19/27

    1

    node 0ptr2

    ptr 4 malloc"si*eof"node$$2

    ptr)/info 4 item2

    if"0head 44 ULL$1

    ptr)/ne%t 4 ptr)/pre'4ULL2

    0head 4 0tail 4 ptr2

    3

    else

    1

    ptr)/ne%t4ULL2

    ptr)/pre'40tail2 "0tail$)/ne%t4ptr2

    0tail4ptr2

    3

    3

    =nserting 4efore a ?iven 8lement

    'oid insertbeforeelement "node 00head, int item, int before$1

    node 0ptr, 0loc2

    ptr40head2

    loc4search"ptr,before$2

    if"loc44ULL$

    return2

    ptr4malloc"si*eof"node$$2

    ptr)/info4item2

    if"loc)/pre'44ULL$ 1

    ptr)/pre'4ULL2

    loc)/pre'4ptr2

    ptr)/ne%t40head2

    0head4ptr2

  • 8/12/2019 Chapter 5 Linked List

    20/27

    3

    else

    1

    ptr)/pre'4loc)/pre'2

    ptr)/ne%t4loc2 "loc)/pre'$)/ne%t4ptr2

    loc)/pre'4ptr2

    3

    3

    =nserting after a ?iven 8lement

    'oid insertafterelement "node 00head, node 00tail, int item, int after$

    1

    node 0ptr, 0loc2

    ptr 4 0head2

    loc 4 search"ptr,after$2

    if"loc 44 ULL$

    return2

    ptr4malloc"si*eof"node$$2 ptr)/info 4 item2

    if"loc)/ne%t 44 ULL$

    1

    ptr)/ne%t 4 ULL2

    loc)/ne%t 4 ptr2

    ptr)/pre' 4 0tail2

    0tail 4 ptr2

    3

    else 1

    ptr)/pre' 4 loc2

    ptr)/ne%t 4 loc)/ne%t2

    "loc)/ne%t$)/pre' 4 ptr2

    loc)/ne%t 4 ptr2

  • 8/12/2019 Chapter 5 Linked List

    21/27

    3

    3

    5.5 ;eleting an element from a dou4l Linked list.

    +o delete an element from the list, first the pointers are set properl and

    then the memor occupied b the node to be deleted is deallocated

    "freed$.

    Deletion in the list can take place at the follo!ing positions.

    a$ (t the beginning of the list

    b$ (t the end of the list

    c$ (fter a gi'en element

    d$ Jefore a gi'en element

    a) Deleting from the Beginning of the List

    (n element from the beginning of the list can be deleted b performing

    the follo!ing steps

    (ssign the 'alue of head "address of the first element of the list$ to a

    temporar 'ariable "sa temp$+here are t!o further cases

    )If there is onl one element in the e%isting list, both head and tail are set

    to ULL.

    )If there is more than one element in the list then

    )(ssign ULL to the pre' pointer field of the second node.

    )(ssign the address of the second node to head.

    )Deallocate the memor occupied b the node pointed to b temp.

    <

  • 8/12/2019 Chapter 5 Linked List

    22/27

    if"0head44ULL$

    return2

    temp40head2

    if"0head440tail$ &0one element onl0&

    0head40tail4ULL2

    else

    1

    "temp)/ne%t$)/pre'4ULL2

    0head4temp)/ne%t2

    3

    free"temp$2

    3

    b) Deleting from the End of the List

    (n element from the end of the list can be deleted b performing the

    follo!ing steps

    (ssign the 'alue of tail "address of the last element of the list$ to a

    temporar 'ariable "sa temp$

    Hurther there are t!o cases

    ) If there is onl one element in the e%isting list, set both head and tail to

    ULL.

    )If there is more than one element in the list then)(ssign ULL to the ne%t pointer field of the second last node.

    )(ssign the address of the second last node to tail.

    )Deallocate the memor occupied b the node pointed to b temp.

    <

  • 8/12/2019 Chapter 5 Linked List

    23/27

    temp40tail2

    if"0head440tail$ &0one element onl0&

    0head40tail4ULL2

    else

    1

    temp)/pre')/ne%t4ULL2

    0tail4temp)/pre'2

    3

    free"temp$2

    3

    c) Deleting after a Given Element

    'oid deleteafterelement "node 00head, node 00tail, int after$

    1 node 0temp, 0loc2

    temp 4 0head2

    loc 4 search"temp,after$2

    if "loc 44 ULL$ &0search item not found0&

    return2

    temp 4 loc)/ne%t2

    loc)/ne%t 4 temp)/ne%t2

    if"temp)/ne%t 44 ULL$

    0tail 4 loc2

    else

    "temp)/ne%t$)/pre' 4 loc2

    free"temp$2

    3

    d) Deleting before a Given Element

    'oid deletebeforeelement "node 00head, int before$

    1

    node 0temp, 0loc2 temp40head2

    loc4search"temp,before$2

    if"loc44ULL$

    return2

    temp4loc)/pre'2

  • 8/12/2019 Chapter 5 Linked List

    24/27

  • 8/12/2019 Chapter 5 Linked List

    25/27

    customers in a queue can increase or decrease during the process at an

    time. Nhen the list gro!s !e need to allocate more memor space to

    accommodate additional data items. Buch situations can be handled

    mo'e easil b using dnamic techniques. Dnamic data items at run

    time, thus optimi*ing file usage of storage space.

    5.B (a# ;namic memor allocation:

    +he process of allocating memor at run time is kno!n as dnamic

    memor allocation. (lthough c does not inherentl ha'e this facilit

    there are four librar routines !hich allo! this function.

    Man languages permit a programmer to specif an arra si*e at run

    time. Buch languages ha'e the abilit to calculate and assign duringe%ecutions, the memor space required b the 'ariables in the program.

    Jut c inherentl does not ha'e this facilit but supports !ith memor

    management functions, !hich can be used to allocate and free memor

    during the program e%ecution. +he follo!ing functions are used in c for

    purpose of memor management.

    unction 'ask

    malloc (llocates memor requests si*e of btes and returns a

    pointer to the Ist bte of allocated space

    calloc (llocates space for an arra of elements initiali*es them to

    *ero and returns a pointer to the memor

    free Hrees pre'iousl allocated space

    realloc Modifies the si*e of pre'iousl allocated space.

  • 8/12/2019 Chapter 5 Linked List

    26/27

    5.B (4#Demor allocations process:

    (ccording to the conceptual 'ie! the program instructions and global

    and static 'ariable in a permanent storage area and local area 'ariables

    are stored in stacks. +he memor space that is located bet!een these t!oregions in a'ailable for dnamic allocation during the e%ecution of the

    program. +he free memor region is called the heap. +he si*e of heap

    keeps changing !hen program is e%ecuted due to creation and death of

    'ariables that are local for functions and blocks. +herefore it is possible

    to encounter memor o'erflo! during dnamic allocation process. In

    such situations, the memor allocation functions mentioned abo'e !ill

    return a null pointer.

    5.B (

  • 8/12/2019 Chapter 5 Linked List

    27/27

    Allocating multiple 4locks of memor:

    Calloc is another memor allocation function that is normall used to

    request multiple blocks of storage each of the same si*e and then sets all

    btes to *ero. +he general form of calloc is

    ptr4"cast)tpe0$ calloc"n,elem)si*e$2

    +he abo'e statement allocates contiguous space for n blocks each si*e of

    elements si*e btes. (ll btes are initiali*ed to *ero and a pointer to the

    first bte of the allocated region is returned. If there is not enough space

    a null pointer is returned.

    5.B (d# Eeleasing te used space:

    Compile time storage of a 'ariable is allocated and released b the

    sstem in accordance !ith its storage class. Nith the dnamic runtime

    allocation, it is our responsibilit to release the space !hen it is not

    required. +he release of storage space becomes important !hen the

    storage is limited. Nhen !e no longer need the data !e stored in a block

    of memor and !e do not intend to use that block for storing an other

    information, !e ma release that block of memor for future use, using

    the free function.

    free"ptr$2

    ptr is a pointer that has been created b using malloc or calloc.

    'o alter te sie of allocated memor:

    +he memor allocated b using calloc or malloc might be insufficient or

    e%cess sometimes in both the situations !e can change the memor si*ealread allocated !ith the help of the function realloc. +his process is

    called reallocation of memor.