linked lists notes

Upload: shresth

Post on 16-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/23/2019 Linked Lists Notes

    1/24

    Data St r uct ur es ( 9067)

    Linked Lists

    Introduction

    We have seen repr esent at i on of l i near dat a st r uct ur es by usi ngsequent i al al l ocat i on met hod of st or age, as i n, ar r ays. But t hi s i sunaccept abl e i n cases l i ke:

    a.

    Unpr edi ct abl e st or age requi r ement s:

    The exact amount of dat a st or age r equi r ed by t he program var i eswi t h the amount of data bei ng pr ocessed. Thi s may not be avai l abl e att he t i me we wr i t e pr ogr ams but are t o be determi ned l ater .

    For exampl e, l i nked al l ocat i ons ar e ver y benef i ci al i n case ofpol ynomi al s. When we add t wo pol ynomi al s, and none of t hei r degreesmat ch, t he r esul t i ng pol ynomi al has t he si ze equal t o t he sum of t he t wopol ynomi al s t o be added. I n such cases we can generat e nodes ( al l ocat ememory t o t he dat a member ) whenever r equi r ed, i f we use l i nkedr epr esent at i on ( dynami c memor y al l ocat i on) .

    b. Extensi ve dat a mani pul at i on t akes pl ace.

    Frequent l y many oper at i ons l i ke i nser t i on, del et i on et c, ar e t o beper f or med on t he l i nked l i st .

    Poi nt er s are used f or t he dynami c memory al l ocat i on. These poi nt er s areal ways of same l engt h r egar dl ess of whi ch dat a el ement i t i s poi nt i ng t o(i nt , f l oat , st r uct etc, ) . Thi s enabl es t he mani pul at i on of poi nt er s t o beper f ormed i n a uni f orm manner usi ng si mpl e t echni ques. These make us capabl eof r epr esent i ng a much more compl ex rel at i onshi p bet ween t he el ement s of a

    dat a st r uct ur e t han a l i near or der met hod.

    The use of poi nt er s or l i nks t o r ef er t o el ement s of a data st r uct urei mpl i es t hat el ement s, whi ch ar e l ogi cal l y adj acent , need not be physi cal l yadj acent i n t he memor y. J ust l i ke f ami l y member s di sper sed, but st i l l boundt oget her .

    Singly Linked List [or] One way chain

    Thi s i s a l i st , whi ch may consi st of an or dered set of el ement s t hat mayvar y i n number . Each el ement i n t hi s l i nked l i st i s cal l ed as node. A node i na si ngl y l i nked l i st consi st s of t wo par t s, a information part where t he

    act ual dat a i s st or ed and a link part, whi ch st or es t he addr ess of t hesuccessor ( next ) node i n t he l i st . The or der of t he el ement s i s mai nt ai ned byt hi s expl i ci t l i nk bet ween t hem. The typi cal node i s as shown :

    NODE

    Li nkI nf o

    Fig 1. Structure of a Node

    - 1 -

  • 7/23/2019 Linked Lists Notes

    2/24

    Data St r uct ur es ( 9067)

    Consi der an exampl e wher e the marks obt ai ned by t he st udent s ar e st oredi n a l i nked l i st as shown i n t he f i gur e :

    | dat a | Next |

    - - - > - - - > - - - >70 65 NULL6245

    | |

    fig 2. Singly Linked List

    I n f i gur e 2, t he ar r ows r epr esent t he l i nks. The data par t of each nodeconsi st s of t he marks obt ai ned by a st udent and the next par t i s a poi nt er t ot he next node. The NULL i n t he l ast node i ndi cat es t hat t hi s node i s t he l astnode i n t he l i st and has no successor s at pr esent . I n t he above the exampl et he dat a par t has a si ngl e el ement marks but you can have as many el ement s asyou r equi r e, l i ke hi s name, cl ass et c.

    There ar e several oper at i ons t hat we can per f or m on l i nked l i st s. We cansee some of t hem now. To begi n wi t h we must def i ne a st r uct ur e f or t he nodecont ai ni ng a dat a par t and a l i nk par t . We wi l l wr i t e a pr ogr am t o show howt o bui l d a l i nked l i st by addi ng new nodes i n the begi nni ng, at t he end or i nt he mi ddl e of t he l i nked l i st . A f uncti on di spl ay( ) i s used t o di spl ay thecont ent s of t he nodes pr esent i n t he l i nked l i st and a f unct i on del et e( ) ,whi ch can del et e any node i n t he l i nked l i st .

    t ypedef st r uct node{ i nt dat a;

    st r uct node *l i nk;}NODE;

    # i ncl ude < st di o. h ># i ncl ude < al l oc. h > / * r equi r ed f or dynami c memory */

    / * al l ocat i on * /

    mai n( ){NODE *p;

    P = NULL; / * empt y l i nked l i st */

    pr i nt f ( \ n No of el ement s i n t he l i nked l i st = %d,count ( p) ) ;

    append( &p, 1) ; / * adds node at t he end of t he l i st */append( &p, 2) ;append( &p, 3) ;append( &p, 4) ;append(&p, 17) ;

    cl r s cr ( ) ;di spl ay( p) ;

    - 2 -

  • 7/23/2019 Linked Lists Notes

    3/24

    Data St r uct ur es ( 9067)

    add_beg( &p, 999) ; / * adds node at t he begi nni ng of t hel i st * /

    add_beg( &p, 888) ;add_beg( &p, 777) ;

    di spl ay( p) ;

    add_af t er ( p, 7, 0) ; / * adds node af t er speci f i ed node */add_af t er ( p, 2, 1) ;add_af t er ( p, 1, 99) ;

    di spl y(p) ;pr i nt f ( \ n No of el ement s i n t he l i nked l i st = %d,

    count ( p) ) ;

    del et e( &p, 888) ; / * del et es the node speci f i ed */del et e( &p, 1) ;del et e( &p, 10) ;

    di spl y(p) ;pr i nt f ( \ n No of el ement s i n t he l i nked l i st = %d,

    }

    To begi n wi t h t he var i abl ep has been decl ared as poi nt er t o a node.Thi s poi nt er i s a poi nt er t o t he f i r st node i n t he l i st . No mat t er how manynodes get added t o t he l i st , p wi l l al ways be t he f i r st node i n t he l i nkedl i st . When no node exi st s i n t he l i nked l i st , p wi l l be set t o NULL t oi ndi cat e t hat t he l i nked l i st i s empt y. Now we wi l l wr i t e and di scuss each oft hese f unct i ons.

    Function to add a node at the end of the linked list

    append( NODE **q, i nt num){

    NODE *t emp, *r ;

    t emp = *q;

    i f ( *q == NULL) / *l i st empt y, creat e t he f i r st node */{

    t emp = mal l oc( si zeof ( NODE) ) ;t emp- >dat a = num;t emp- >l i nk = NULL;*q = t emp;

    }el se{

    t emp = *q;

    whi l e( t emp- >l i nk ! = NULL ) / * got o t he end of */t emp = t emp- >l i nk; / * l i st */

    - 3 -

  • 7/23/2019 Linked Lists Notes

    4/24

    Data St r uct ur es ( 9067)

    r = mal l oc( si zeof ( NODE) ) ;r - >dat a = num; / * add node at t he */r - >l i nk = NULL; / * end of t he l i st */t emp- >l i nk = r ;

    }}

    The append( ) f unct i on has t o deal wi t h t wo si t uat i ons:

    a. The node i s bei ng added t o an empt y l i st .b. The node i s bei ng added t o t he end of t he l i nked l i st .

    I n t he f i r st case, t he condi t i on

    i f ( *q == NULL )

    get s sat i sf i ed. Hence space i s al l ocat ed f or t he node usi ng mal l oc( ) .Dat a and t he l i nk par t of t hi s node ar e set up usi ng t he st at ement s :

    t emp- >dat a = num;t emp- >l i nk = NULL;

    Last l y p i s made t o poi nt t o t hi s node, si nce t he f i r st node has beenadded t o t he l i nked l i st and p must al ways poi nt t o t he f i r st node. Not e t hat*q i s not hi ng but equal t o p.

    I n t he ot her case, when t he l i nked l i st i s not empt y, t he condi t i on :

    i f ( *q == NULL)

    woul d f ai l , si nce *q ( i . e. p i s non- NULL) . Now t emp i s made t o poi nt t o t hef i r st node i n t he l i nked l i st t hr ough t he st at ement ,

    t emp = *q;

    Then usi ng t emp we have t r aver sed t hrough t he ent i r e l i nked l i st usi ng t hest at ement s:

    whi l e( t emp- >l i nk ! = NULL)t emp=t emp- >l i nk;

    The posi t i on of t he poi nt er bef or e and af t er t r aver si ng t he l i nked l i st i sshown bel ow:

    p t emp

    - - - > - - - > - - - >1 NULL432

    - 4 -

  • 7/23/2019 Linked Lists Notes

    5/24

    Data St r uct ur es ( 9067)

    p t emp r

    - - - > - - - > - - - >1 NULL432

    node

    bei ngadded.

    Fig 3. Node being added at the end of a SLL

    Each t i me t hrough t he l oop t he st at ement t emp= t emp- >l i nk makes t emppoi nt t o the next node i n t he l i st . When t emp r eaches t he l ast node t hecondi t i on t emp- >l i nk ! = NULL woul d f ai l . Once out si de t he l oop we al l ocat et he space f or t he new node t hrough t he st at ement

    r = mal l oc( si zeof ( NODE) ) ;

    Once the space has been al l ocat ed f or t he new node i t s dat a part i sf i l l ed wi t h num and t he l i nk par t wi t h NULL. Not e that t hi s node i s now goi ngt o be t he l ast node i n t he l i st .

    Al l t hat now r emai ns i s connect i ng t he pr evi ous l ast node to t hi s newl ast node. The pr evi ous node i s bei ng poi nt ed t o by t emp and the new l astnode i s by r . t hey ar e connected t hr ough t he st atement

    t emp- >l i nk = r ;

    There i s of t en a conf usi on amongst t he begi nner s as t o how t he st at ementt emp=t emp- >l i nk makes t emp poi nt t o t he next node i n the l i nked l i st . Let us

    under st and t hi s wi t h t he hel p of an exampl e. Suppose i n a l i nked l i stcont ai ni ng 4 nodes t emp i s poi nt i ng t o the f i r st node. Thi s i s shown i n t hef i gur e bel ow:

    t emp

    150 1 400 2 700 3 910 4 NULL

    150 400 700 910

    Fig 4. Actual representation of a SLL in memory

    I nst ead of showi ng t he l i nks t o t he next node t he above di agr am showst he addresses of t he next node i n t he l i nk par t of each node. When we executet he st at ement t emp = t emp- > l i nk, t he r i ght hand si de yi el ds 400. Thi saddr ess i s now st or ed i n t emp. As a resul t , t emp st ar t s posi t i oni ng nodespr esent at addr ess 400. I n ef f ect t he st at ement has shi f t ed t emp so t hat i thas st ar t ed posi t i oni ng t o t he next node i n t he l i nked l i st .

    Function to add a node at the beginning of the linked list

    - 5 -

  • 7/23/2019 Linked Lists Notes

    6/24

    Data St r uct ur es ( 9067)

    add_beg( NODE **q, i nt num){

    t emp = mal l oc( si zeof ( NODE) ) ;t emp- >dat a = num;t emp- >l i nk = *q;*q = t emp;

    }

    Suppose t her e ar e al r eady 5 nodes i n the l i st and we wi sh t o add a newnode at t he begi nni ng of t hi s exi st i ng l i nked l i st . Thi s si t uat i on i s showni n t he f i gur e bel ow.

    t emp p

    1 - - - > 2 - - - > 3 - - - > 4 - - - >17 NULL

    Before Addition

    t emp p

    999 - - - > 1 - - - > 2 - - - > 3 - - - > 4 - - - >17 NULL

    After Addition

    Fig 5. Addition of a node in the beginning of a SLL

    For addi ng a new node at t he begi nni ng, f i r st l y space i s al l ocat ed f or

    t hi s node and dat a i s st or ed i n i t t hr ough t he st at ementt emp- >dat a = num;

    Now we need t o make t he l i nk par t of t hi s node poi nt t o the exi st i ngf i r st node. Thi s has been achi eved t hr ough t he st atement

    t emp- >l i nk = *q;

    Last l y t hi s new node must be made t he f i r st node i n t he l i st . Thi s hasbeen at t ai ned t hrough t he st atement

    *q = t emp;

    Function to add a node after the specified node

    add_af t er ( NODE *q, i nt l oc, i nt num){NODE *t emp, *t ;i nt i ;

    t emp = q;

    - 6 -

  • 7/23/2019 Linked Lists Notes

    7/24

    Data St r uct ur es ( 9067)

    f or ( i =0 ; i l i nk;i f ( t emp == NULL){

    pr i nt f ( Ther e ar e l ess t han %d el ement s i nt he l i st , l oc) ;

    r et ur n;

    }}

    r = mal l oc( si zeof ( NODE) ) ;r - >dat a = num;r - >l i nk = t emp- >l i nk;t emp- >l i nk = r ;

    }

    The add_af t er ( ) f unct i on permi t s us t o add a new node af t er a speci f i ednumber of nodes i n t he l i nked l i st .

    To begi n wi t h, t hrough a l oop we ski p t he desi r ed number of nodes af t er

    whi ch a new node i s t o be added. Suppose we wi sh t o add a new node cont ai ni ngdat a as 99 af t er t he t hi r d node i n t he l i st . The posi t i on of poi nt er s oncet he cont r ol r eaches out si de t he for l oop i s shown bel ow:

    P t emp

    1 - - - > 2 - - - > 3 - - - > 4 - - - >17 NULL

    r

    99

    Before Insertion

    P t emp

    1 - - - > 2 - - - > 3 4 - - - >17 NULL

    r

    99

    After Insertion

    Fig 6. Insertion of a node in the specified position

    - 7 -

  • 7/23/2019 Linked Lists Notes

    8/24

    Data St r uct ur es ( 9067)

    The space i s al l ocat ed f or t he node t o be i nser t ed and 99 i s st or ed i nt he dat a par t of i t . Al l t hat r emai ns t o be done i s r eadj ust ment of l i nkssuch t hat 99 goes i n between 3 and 4. t hi s i s achi eved t r ough t he st atement s

    r - >l i nk = t emp- >l i nk;t emp- >l i nk = r ;

    The f i r st st at ement makes l i nk par t of node cont ai ni ng 99 t o poi nt t o

    t he node cont ai ng 4. t he second st atement ensures t hat t he l i nk part of t henode cont ai ni ng 3 poi nt s t o t he new node.

    Functions for display and count

    These f unct i ons ar e ver y si mpl e and st r ai ght f or war d. So no f ur t herexpl anat i on i s r equi r ed f or t hem.

    / * f unct i on t o count t he number of nodes i n t he l i nked l i st */

    count ( NODE *q){

    i nt c = 0;

    whi l e( q ! = NULL) / * tr aver se t he ent i r e l i st */{

    q = q- >l i nk;c++;

    }

    r et ur n ( c) ;}

    / * f uncti on t o di spl ay t he cont ent s of t he l i nked l i st */

    di spl ay( NODE *q){

    pr i nt f ( \ n ) ;

    whi l e( q ! = NULL) / * tr aver se t he ent i r e l i st */{

    pr i nt f ( %d, q- >dat a) ;q=q- >l i nk;

    }}

    Function to delete the specified node from the list

    del ete(NODE ** q, i nt num){

    NODE *ol d, *t emp;

    t emp = *q;

    - 8 -

  • 7/23/2019 Linked Lists Notes

    9/24

    Data St r uct ur es ( 9067)

    whi l e( t emp ! = NULL){

    i f ( t emp- >dat a == num){

    i f ( t emp == q) / * i f i t i s t he f i r st node */{

    *q = t emp- >l i nk;f r ee( t emp) ; / * r el ease t he memory */

    r et ur n;}

    el se{

    ol d- >l i nk == t emp- >l i nk;f r ee( t emp) ;r et ur n;

    }}

    el se{

    ol d = t emp;

    t emp=t emp- >l i nk;}

    }

    pr i nt f ( \ n El ement %d not f ound, num) ;}

    I n t hi s f unct i on t hr ough t he while l oop , we have t r aversed t hr ought he ent i r e l i nked l i st , checki ng at each node, whet her i t i s t he node t o bedel et ed. I f so, we have checked i f t he node i s t he f i r st node i n t he l i nkedl i st . I f i t i s so, we have s i mpl y shi f t edp t o t he next node and t hen del eted

    t he ear l i er node.

    I f t he node t o be del et ed i s an i nt er medi at e node, t hen t he posi t i on ofvar i ous poi nt er s and l i nks bef or e and af t er del et i on ar e shown bel ow.

    P ol d t emp

    1 - - - > 2 - - - > 3 - - - > 4 - - - >17 NULL

    node to be del et ed = 4

    Before deletion

    p ol d

    1 - - - > 2 - - - > 3 - - - > 17 NULL

    - 9 -

  • 7/23/2019 Linked Lists Notes

    10/24

    Data St r uct ur es ( 9067)

    t emp

    Thi s node get s del et ed.4

    After deletion

    Fig 7. Deletion of a node from SLL

    Though t he above l i nked l i st depi ct s a l i st of i nt eger s, a l i nked l i stcan be used f or st or i ng any si mi l ar dat a. For exampl e, we can have a l i nkedl i st of f l oat s, char act er ar r ay, st r uct ure et c.

    Doubly Linked Lists [or] Two-way chain

    I n a si ngl y l i nked l i st we can t r aver se i n onl y one di r ect i on ( f or war d) ,i . e. each node st or es t he addr ess of t he next node i n t he l i nked l i st . I t has

    no knowl edge about wher e t he pr evi ous node l i es i n t he memory. I f we ar e att he 12t h node( say) and i f we want t o reach 11t h node i n t he l i nked l i st , wehave t o t r aver se r i ght f r om t he f i r st node. Thi s i s a cumber some pr ocess.Some appl i cat i ons r equi r e us t o t r averse i n both f or ward and backwar ddi r ect i ons. Here we can st or e i n each node not onl y the addr ess of t he nextnode but al so t he addr ess of t he pr evi ous node i n t he l i nked l i st . Thi sarr angement i s of t en known as a Doubl y l i nked l i st . The node and t hear r angement i s shown bel ow:

    PREV NEXT

    NODE

    Fig 8. Node structure of a DLL

    NULL 20 15 70 60 NULL

    Fig 9. Doubly Linked List

    The l ef t poi nt er of t he l ef t most node and t he r i ght poi nt er of t her i ght most node ar e NULL i ndi cat i ng t he end i n each di r ect i on.

    The f ol l owi ng program i mpl ement s t he doubl y l i nked l i st .

    / * pr ogr am t o mai nt ai n a doubl y l i nked l i st */

    # i ncl ude < al l oc. h >

    - 10 -

  • 7/23/2019 Linked Lists Notes

    11/24

    Data St r uct ur es ( 9067)

    t ypedef st r uct node{ i nt dat a;

    st r uct node *pr ev, *next ;}NODE;

    mai n( )

    {NODE *p;

    p = NULL; / * empt y doubl y l i nked l i st */

    d_append(&p, 11) ;d_append(&p, 21) ;

    cl r s cr ( ) ;di spl ay( p) ;pr i nt f ( \ n No of el ement s i n t he doubl y l i nked l i st =

    %d, count ( p) ) ;

    d_add_beg( &p, 33) ;d_add_beg( &p, 55) ;

    di spl y(p) ;pr i nt f ( \ n No of el ement s i n t he doubl y l i nked l i st =

    %d, count ( p) ) ;

    d_add_af t er ( p, 1, 4000) ;d_add_af t er ( p, 2, 9000) ;

    di spl y(p) ;pr i nt f ( \ n No of el ement s i n t he l i nked l i st = %d,count ( p) ) ;

    d_del et e( &p, 51) ;d_del et e( &p, 21) ;

    di spl y(p) ;pr i nt f ( \ n No of el ement s i n t he l i nked l i st = %d,

    }

    / * adds a new node at t he begi nni ng of t he l i st */

    d_add_beg( NODE **s, i nt num){

    NODE *q;

    / * cr eat e a new node */q = mal l oc( si zeof ( NODE) ) ;

    - 11 -

  • 7/23/2019 Linked Lists Notes

    12/24

    Data St r uct ur es ( 9067)

    / * assi gn dat a and poi nt er s*/q- >prev = NULL;q- >dat a = num;q- >next = *s ;

    / * make t he new node as head node */( *s) - > pr ev = q;*s = q;

    }

    / * adds a new node at t he end of t he doubl y l i nked l i st */

    d_append( NODE **s, i nt num){

    NODE *r , *q =*s ;

    i f ( *s == NULL) / * l i st empt y, creat e t he f i r st node */{

    *s = mal l oc( si zeof ( NODE) ) ;( *s ) - >dat a = num;( *s ) - >next =( *s ) - >prev = NULL;

    }el se{

    whi l e(q- >next ! = NULL ) / * got o the end of */q = q- >next ; / * l i st */

    r = mal l oc( si zeof ( NODE) ) ;

    r - >dat a = num; / * add node at t he */r - >next = NULL; / * end of t he l i st */r - >prev = q;q- >next = r ;

    }}

    / * adds a new node af t er t he speci f i ed number of nodes */

    d_add_af t er ( NODE *q, i nt l oc, i nt num){NODE *t emp;i nt i ;

    / * ski p t o t he desi r ed posi t i on*/

    f or ( i =0 ; i next ;i f ( q == NULL){

    - 12 -

  • 7/23/2019 Linked Lists Notes

    13/24

    Data St r uct ur es ( 9067)

    pr i nt f ( Ther e ar e l ess t han %d el ement s i nt he l i st , l oc) ;

    r et ur n;}

    }

    / * i nser t a new node */

    q = q- >prev;t emp = mal l oc( si zeof ( NODE) ) ;t emp- >dat a = num;t emp- >prev = q;t emp- >next = q- >next ;t emp- >next - >prev = t emp;q- >next = t emp;

    }

    / * count s t he number of nodes pr esent i n t he l i nked l i st */

    count ( NODE *q)

    {i nt c = 0;

    whi l e( q ! = NULL) / * tr aver se t he ent i r e l i st */{

    q=q- >next ;c++;

    }r et ur n ( c) ;

    }

    / * Funct i on t o di spl ay t he cont ent s of t he doubl y l i nked l i st *// * i n l ef t t o r i ght order * /

    di spl ayLR( NODE *q){

    pr i nt f ( \ n ) ;

    whi l e( q ! = NULL) / * tr aver se t he ent i r e l i st */{

    pr i nt f ( %d, q- >dat a) ;q=q- >next ;

    }}

    / * Funct i on t o di spl ay t he cont ent s of t he doubl y l i nked l i st *// * i n r i ght t o l ef t order * /

    di spl ayRL( NODE *q){

    pr i nt f ( \ n ) ;

    - 13 -

  • 7/23/2019 Linked Lists Notes

    14/24

    Data St r uct ur es ( 9067)

    whi l e( q- >next ! = NULL) / * t r aver se t he ent i r e l i st */{

    q=q- >next ;}

    whi l e( q != NULL){

    pr i nt f ( %d, q- >dat a) ;q=q- >prev;

    }}

    / * t o del et e t he speci f i ed node f r om t he l i st . * /

    d_del ete(NODE ** s, i nt num){

    NODE *q= *s;

    / * t r averse t he ent i r e l i nked l i st * /

    whi l e( q != NULL){

    i f ( q- >dat a == num){

    i f (q == *s) / * i f i t i s the f i rs t node * /{

    *s = ( *s) - >next ;( *s ) - > prev = NULL;

    }el se{

    / * i f t he node i s l ast node */i f ( q- >next == NULL)q- >prev- >next = NULL;

    el se/ * node i s i nt er medi at e */

    {q- >prev- >next = q- >next ;q- >next - >prev = q- >prev;

    }f ree( q) ;

    }r et ur n; / * af t er del et i on * /

    }q = q- >next ; / * got o next node i f not f ound */

    }pr i nt f ( \ n El ement %d not f ound, num) ;

    }

    As you must have r eal i zed by now any operat i on on l i nked l i st i nvol vesadj ust ment s of l i nks. Si nce we have expl ai ned i n det ai l about al l t he

    - 14 -

  • 7/23/2019 Linked Lists Notes

    15/24

    Data St r uct ur es ( 9067)

    f unct i ons f or si ngl y l i nked l i st , i t i s not necessar y t o gi ve step- by- stepworki ng anymore. We can underst and the worki ng of doubl y l i nked l i st s wi t ht he hel p of di agr ams. We have shown al l t he possi bl e oper at i ons i n a doubl yl i nked l i st al ong wi t h t he f unct i ons used i n di agr ams bel ow:

    Addition of new node to an empty linked list

    Case 1: Addi t i on t o an empt y l i st

    Rel at ed f unct i on : d_append( )

    p = *s = NULL

    Before Addition

    P

    NULL 1 NULL

    New node

    After Addition

    Case 2: Addi t i on t o an exi st i ng l i nked l i stRel at ed f unct i on : d_append( )

    P q

    2 3

    4 NN 1

    r

    99 N

    Before Appending

    P q

    2 3

    4N 1

    99 Nr

    - 15 -

  • 7/23/2019 Linked Lists Notes

    16/24

    Data St r uct ur es ( 9067)

    After Appending

    Addition of new node at the beginning.

    Rel at ed Funct i on : d_add_beg( )

    p

    2 3 4 NN 1

    q

    2 3 4 N

    33N

    Before Addition

    q p

    33N

    1

    After Addition

    Fig 10. Addition of nodes at various positions in the DLL

    - 16 -

  • 7/23/2019 Linked Lists Notes

    17/24

    Data St r uct ur es ( 9067)

    Insertion of a new node after a specified node

    Rel at ed f unct i on : d_add_af t er ( )

    p

    q

    2 3 4 NN 1

    t emp

    66N

    New Node

    Before Insertion

    P q t emp

    42 3 66 N 1

    N

    After Insertion

    Fig 11. Insertion of node in the DLL

    Deletion Of a Node

    Case 1: Del et i on of f i r st nodeRel at ed f unct i on : d_del et e( )

    p q

    1 2 3 NN 55

    Node to be del et ed : 55

    Before Deletion

    - 17 -

  • 7/23/2019 Linked Lists Notes

    18/24

    Data St r uct ur es ( 9067)

    p q

    2 3 N 1

    After Deletion

    Case 2: Del et i on of t he l ast nodeRel at ed f unct i on : d_del et e( )

    p q

    2 3 N88 N 1

    Node to be del et ed : 88

    Before Deletion

    p q

    2 3 N 1

    After Deletion

    Case 3: Del et i on of t he i nt er medi at e nodeRel at ed f unct i on : d_del et e( )

    p q

    2 N4 N 1 377

    Node to be del et ed : 77

    Before Deletion

    p

    - 18 -

  • 7/23/2019 Linked Lists Notes

    19/24

    Data St r uct ur es ( 9067)

    2 3 4 NNN 1

    After Deletion

    Fig 11.Deletion of nodes from various positions in the DLL

    Applications of the linked lists

    I n comput er sci ence l i nked l i st s are extensi vel y used i n Dat a BaseManagement Syst ems Pr ocess Management , Oper at i ng Syst ems, Edi t ors et c.Ear l i er we saw t hat how si ngl y l i nked l i st and doubl y l i nked l i st can bei mpl ement ed usi ng t he poi nt ers. We al so saw t hat whi l e usi ng arr ays var yof t en t he l i st of i t ems t o be st or ed i n an ar r ay i s ei t her t oo shor t or t oobi g as compared t o t he decl ar ed si ze of t he ar r ay. Mor eover , dur i ng pr ogr amexecut i on the l i st cannot gr ow beyond the si ze of t he decl ar ed ar r ay. Al so,oper at i ons l i ke i nser t i ons and del et i ons at a speci f i ed l ocat i on i n a l i st

    r equi r e a l ot of movement of dat a, t her eby l eadi ng t o an i nef f i ci ent andt i me- consumi ng al gor i t hm.

    The pr i mar y advant age of l i nked l i st over an ar r ay i s t hat t he l i nkedl i st can gr ow or shr i nk i n si ze dur i ng i t s l i f et i me. I n par t i cul ar , t hel i nked l i st s maxi mum si ze need not be known i n advance. I n pr act i calappl i cat i ons t hi s of t en makes i t possi bl e t o have sever al dat a st r uct ur esshar e t he same space, wi t hout payi ng par t i cul ar at t ent i on t o t hei r r el at i vesi ze at any t i me.

    The second advant age of provi di ng f l exi bi l i t y i n al l owi ng t he i t ems t obe r ear r anged ef f i ci ent l y i s gai ned at t he expense of qui ck access t o any

    ar bi t r ar y i t em i n t he l i st . I n ar r ays we can access any i t em at t he same t i meas no t r aver si ng i s r equi r ed.

    We are not suggest i ng t hat you shoul d not use ar r ays at al l . Ther e ar esever al appl i cat i ons wher e usi ng ar r ays i s mor e benef i ci al t han usi ng l i nkedl i st s. We must sel ect a par t i cul ar dat a st r uct ur e dependi ng on t her equi r ement s.

    Let us now see some mor e appl i cat i ons of t he l i nked l i st s, l i ke mer gi ngt wo l i st s and how t he l i nked l i st s can be used f or pol ynomi alr epr esent at i ons.

    Function to Merge the two lists.

    Mer ge( NODE *p, NODE *q, NODE **s){NODE *z;

    / * I f bot h l i st s ar e empt y */i f ( p==NULL && q == NULL){

    - 19 -

  • 7/23/2019 Linked Lists Notes

    20/24

    Data St r uct ur es ( 9067)

    r et ur n;}

    / * t r aver se bot h l i nked l i st s t i l l t he end. I f end of any one l i nkedl i st i s encount er ed t hen t he l oop i s t er mi nat ed */

    whi l e( p ! = NULL && q ! = NULL){

    / * i f node bei ng added i n t he f i r st l i st * /

    i f ( *s == NULL){

    *s = mal l oc( si zeof ( NODE) ) ;z = *s;

    }el se{

    z- >l i nk = mal l oc( si zeof ( NODE) ) ;z = z- >l i nk;

    }

    i f ( p- >data < q- >data){

    z- >dat a = p- >dat a;p = p- >l i nk;

    }el se{

    i f ( p- >dat a > q- >data){

    z- >dat a = q- >dat a;q = q- >l i nk;

    }el se{i f ( p- >dat a == q- >dat a){

    z- >dat a = q- >dat a;q = q- >l i nk;p = p- >l i nk;

    }}

    }}

    / * i f end of f i r st l i st has not been r eached */

    whi l e( p ! = NULL){

    z- >l i nk = mal l oc( si zeof ( NODE) ) ;z = z- >l i nk;z- >dat a = p- >dat a;p = p- >l i nk;

    - 20 -

  • 7/23/2019 Linked Lists Notes

    21/24

    Data St r uct ur es ( 9067)

    }

    / * i f end of second l i st has not been r eached */

    whi l e( q ! = NULL){

    z- >l i nk = mal l oc( si zeof ( NODE) ) ;z = z- >l i nk;

    z- >dat a = q- >dat a;q = q- >l i nk;

    }

    z- >l i nk = NULL;}

    I n t hi s progr am, assume t hat st r uct ur e NODE wi t h data and l i nk i savai l abl e. Al so usi ng add( ) used f or si ngl y l i nked l i st ear l i er we have t wol i nked l i st s. Thr ee poi nt er s poi nt t o t hr ee l i nked l i st s. The mer ge f uncti oncan be cal l ed t o mer ge t he t wo l i nked l i st s. Thi s mer ged l i st i s poi nt ed t oby t he poi nt er t hi r d. Whi l e mer gi ng t wo l i st s i t i s assumed t hat t he l i st s

    t hemsel ves are i n ascendi ng order.

    Linked lists and Polynomials.

    Pol ynomi al s can be mai nt ai ned usi ng a l i nked l i st . To have a pol ynomi all i ke 5x4 _ 2x3 + 7x2 + 10x 8 , each node shoul d consi st of t hree el ement s,namel y coef f i ci ent , exponent and a l i nk t o t he next i t em. Whi l e mai nt ai ni ngt he pol ynomi al i t i s assumed t hat t he exponent of each successi ve t er m i sl ess t han t hat of t he pr evi ous t er m. I f t hi s i s not t he case you can al so usea f unct i on t o bui l d a l i st , whi ch mai nt ai ns t hi s or der . Once we bui l d al i nked l i st t o r epr esent t he pol ynomi al we can per f or m oper at i ons l i keaddi t i on and mul t i pl i cat i on. Consi der t he pr ogr am gi ven bel ow.

    / * progr am t o add t wo pol ynomi al s */

    t ypedef st r uct node{

    f l oat coef f ;i nt exp;st r uct node *l i nk;

    }PNODE;

    voi d p_append( PNODE **, f l oat , i nt ) ;voi d p_addi t i on( PNODE *, PNODE *, PNODE **) ;

    mai n( ){PNODE ( f i r st , *second, *t ot al ;i nt i = 0;

    f i r st = second = t ot al = NULL; / * empt y l i nked l i st s */

    - 21 -

  • 7/23/2019 Linked Lists Notes

    22/24

    Data St r uct ur es ( 9067)

    p_append( &f i r st , 1, 4, 5) ;p_append( &f i r st , 1, 5, 4) ;p_append( &f i r st , 1, 7, 2) ;p_append( &f i r st , 1, 8, 1) ;p_append( &f i r st , 1, 9, 0) ;

    cl r s cr ( ) ;di spl ay_p( f i r s t ) ;

    p_append( &second, 1, 5, 6) ;p_append( &second, 2, 5, 5) ;p_append( &second, - 3, 5, 4) ;p_append( &second, 4, 5, 3) ;p_append( &second, 6, 5, 1) ;

    di spl ay_p( second) ;

    p_addi t i on( f i r st , second, &t ot al )

    di spl ay_p( t ot al ) ;

    };

    The f unct i on t o append t he pol ynomi al p_append( ) and di spl ay_p( ) ar esi mi l ar t o our f unct i ons f or si ngl y l i nked l i st . So t hey ar e expect ed t o bewr i t t en by t he user. The f unct i on t o add t wo pol ynomi al s i s gi ven bel ow.

    voi d p_addi t i on( PNODE *x, PNODE *y, PNODE **s){PNODE *z;

    / * i f bot h l i st s ar e empt y */

    i f ( x == NULL && y == NULL )r et ur n;

    / * t r aver se t i l l one node ends */

    whi l e( x ! = NULL && y ! = NULL ){i f ( *s == NULL)

    {*s = mal l oc( si zeof ( PNODE) ) ;z = *s;

    }el se{

    z- >l i nk = mal l oc( si zeof ( PNODE) ) ;z = z- >l i nk;

    }

    / * st or e a t er m of l ar ger degr ee i f pol ynomi al */i f ( x- >exp < y- >exp)

    - 22 -

  • 7/23/2019 Linked Lists Notes

    23/24

    Data St r uct ur es ( 9067)

    {z- >coef f = y- >coef f ;z- >exp = y- >exp;y = y- >l i nk; / * got o t he next node */

    }el se{

    i f ( x- >exp > y- >exp)

    {z- >coef f = x- >coef f ;x- >exp = x- >exp;x = x- >l i nk; / * got o t he next node */

    }el se{

    i f ( x- >exp == y- >exp){

    z- >coef f = x- >coef f + y- >coef f ;x- >exp = x- >exp;x = x- >l i nk; / * got o t he next node */

    y = y- >l i nk; / * got o t he next node */}

    }}

    }

    / *assi gn r emai ni ng el ement s of t he f i r st pol ynomi al t o t heresul t * /

    whi l e( x ! = NULL){

    i f ( *s == NULL)

    { *s = mal l oc( si zeof ( PNODE) ) ;z = *s;

    }el se{

    z- >l i nk = mal l oc( si zeof ( PNODE) ) ;z = z- >l i nk;

    }z- >coef = x- >coef ;z- >exp = x- >exp;x = x- >l i nk;

    }

    / *assi gn r emai ni ng el ement s of t he second pol ynomi al t o theresul t * /

    whi l e( y ! = NULL){

    i f ( *s == NULL){

    - 23 -

  • 7/23/2019 Linked Lists Notes

    24/24

    *s = mal l oc( si zeof ( PNODE) ) ;z = *s;

    }el se{

    z- >l i nk = mal l oc( si zeof ( PNODE) ) ;z = z- >l i nk;

    }

    z- >coef = y- >coef ;z- >exp = y- >exp;y = y- >l i nk;

    }

    z- >l i nk = NULL; / * at t he end of l i st append NULL*/

    }

    I n t hi s pr ogr am t wo pol ynomi al s ar e bui l t and poi nt ed by the poi nt er sf i r st and second. Next t he f unct i on p_addi t i on( ) i s cal l ed t o car r y out t headdi t i on of t hese t wo pol ynomi al s. I n t hi s f uncti on t he l i nked l i st s

    r epr esent i ng t he t wo pol ynomi al s ar e t r aver sed t i l l t he end of one of t hem i sr eached. Whi l e doi ng t hi s t r aver sal t he pol ynomi al s are compared on t er m- by-t erm basi s. I f t he exponent s of t he t wo t er ms bei ng compared ar e equal t hent hei r coef f i ci ent s r e added and t he r esul t i s st or ed i n t he t hi r d pol ynomi al .I f t he exponent s are not equal t hen t he bi gger exponent i s added to the thi r dpol ynomi al . Dur i ng t he tr aver sal i f t he end of one l i st i s r eached t hecont r ol br eaks out of t he whi l e l oop. Now t he remai ni ng t er ms of t hatpol ynomi al ar e si mpl y appended t o t he resul t i ng pol ynomi al . Last l y t he resul ti s di spl ayed.

    Exercises:

    1.

    WAP f or addi ng and del et i ng nodes f r om an ascendi ng or der l i nked l i st .2.

    WAP t o r ever se a si ngl y l i nked l i st by adj ust i ng t he l i nks.3. Wr i t e pr ogr ams f or r ever si ng a doubl y l i nked l i st ( t hough i t does not

    ser ve any pur pose i t wi l l gi ve pr act i ce t o mani pul at e t he poi nt er s.4. WAP to del et e a node af t er t he speci f i ed node and bef ore a speci f i ed

    node usi ng bot h si ngl y and doubl y l i nked l i st s.5. WAP to br eak a l i nked l i st i nt o t wo l i nked l i st s usi ng bot h SLL and

    DLL.6. Wr i t e a pr ogr am t o add t wo pol ynomi al s usi ng a DLL.7. WAP to mul t i pl y t wo pol ynomi al s f or bot h SLL and DLL.8. WAP t o add two l ong i nt egers. Each i nt eger may cont ai n 15 to 20 di gi t s,

    whi ch can be st ored i n nodes, a di gi t each or more dependi ng on t heuser s choi ce. Add t hese l ong i nt eger s (f r om l east si gni f i cant di gi tbackwar ds) and di spl ay the r esul t ant l i st .