Data Structure Lecture 12,13

download Data Structure Lecture 12,13

of 6

Transcript of Data Structure Lecture 12,13

  • 8/4/2019 Data Structure Lecture 12,13

    1/6

    DATA STRUCTUREDATA STRUCTURELinked ListLinked List

    Lecture 12,13Lecture 12,13

  • 8/4/2019 Data Structure Lecture 12,13

    2/6

    F inding the location for insertionF inding the location for insertion

    FIND ( INFO , LINK , START, I TEM, LOC)FIND ( INFO , LINK , START, I TEM, LOC)1.1. If START = NU LL thenIf START = NU LL then

    SET LOC := NU LL and ReturnSET LOC := NU LL and Return2.2. If I TEM < INFO [ START] thenIf I TEM < INFO [ START] then

    SET LOC := NU LL and returnSET LOC := NU LL and return3.3. Set SAVE := START and PTR = L INK [ START]Set SAVE := START and PTR = L INK [ START]4.4. Repeat step 5 and 6 while PTR != NU LLRepeat step 5 and 6 while PTR != NU LL5.5. If I TEM < INFO [ PTR] thenIf I TEM < INFO [ PTR] then

    SET LOC := SAVE and return;SET LOC := SAVE and return;[end If ][end If ]

    6.6. SET SAVE := PTR and PTR := L INK [PTR]SET SAVE := PTR and PTR := L INK [PTR][ end of step 4][ end of step 4]

    7.7. SET LOC := SAVESET LOC := SAVE

    8.8. RETUR NRETUR N

  • 8/4/2019 Data Structure Lecture 12,13

    3/6

    I nsertion on given locationI nsertion on given locationIN SSRT ( INFO , LINK , START, AVA IL. I TEM ) IN SSRT ( INFO , LINK , START, AVA IL. I TEM )

    This algorithm inserts I TEM into a sorted linked list. This algorithm inserts I TEM into a sorted linked list.1.1. [ Use previous Procedure to f ind the location o f the node[ Use previous Procedure to f ind the location o f the node

    preceding I TEM.]preceding I TEM.]Call FIND A( INFO , LINK , START, I TEM, L OC) .Call FIND A( INFO , LINK , START, I TEM, L OC) .

    2.2. [ Use Algorithm to insert I TEM a f ter the node with[ Use Algorithm to insert I TEM a f ter the node withlocation LOC .]location LOC .]Call IN SLOC ( INFO , LINK , START, AVA IL, LOC ,Call IN SLOC ( INFO , LINK , START, AVA IL, LOC ,I TEM ).I TEM ).

    3.3. Exit.Exit.

  • 8/4/2019 Data Structure Lecture 12,13

    4/6

    Deletion in a linked listDeletion in a linked listD EL ( INFO , START, AVA IL, LOC , LOC P )D EL ( INFO , START, AVA IL, LOC , LOC P )1.1. IF LOC P = NU LL thenIF LOC P = NU LL then

    set START := L INK [START]set START := L INK [START]ElseElse

    set LINK [LOC P] := LINK [LOC ]set LINK [LOC P] := LINK [LOC ]

    2.2. Set LINK [ LOC ] := AVA IL and AVA IL := LOCSet LINK [ LOC ] := AVA IL and AVA IL := LOC3.3. Return.Return.

  • 8/4/2019 Data Structure Lecture 12,13

    5/6

    Deleting a node with given itemDeleting a node with given item

    (searching)(searching)FINDB ( INFO , LINK . START, I TEM, LOC LOC P ) FINDB ( INFO , LINK . START, I TEM, LOC LOC P ) This procedure f inds the location LOC of the f irst node N which contains I TEM and This procedure f inds the location LOC of the f irst node N which contains I TEM andthe location LOC P of the node preceding N . If I TEM does not appear in the list, thenthe location LOC P of the node preceding N . If I TEM does not appear in the list, thenthe procedure sets L OC NU LJ...; and if I TEM appears in the f irstthe procedure sets L OC NU LJ...; and if I TEM appears in the f irst/ node, then it sets L OC P NU LL./ node, then it sets L OC P NU LL.

    1.1. [List empty?]If START= NU LL, then:[List empty?]If START= NU LL, then:

    Set LOC :=NU LL and LOC P:NU LL, and Return.Set LOC :=NU LL and LOC P:NU LL, and Return.[End If structure.][End If structure.]2.2. [ I TEM in f irst node?] If INFO [START] = I TEM, then:[ I TEM in f irst node?] If INFO [START] = I TEM, then:

    Set LOC := STA1T and L OC P = NU LL, and Return.Set LOC := STA1T and L OC P = NU LL, and Return.[End o f If structure.][End o f If structure.]

    3.3. Set SAVE := START and PTR := L INK [START]. [ Initializes pointers.]Set SAVE := START and PTR := L INK [START]. [ Initializes pointers.]4.4. Repeat StepsRepeat Steps 5 5 and 6 while PTR NU LL.and 6 while PTR NU LL.5.5. If INFO [PTRJ = I TEM, then:If INFO [PTRJ = I TEM, then:

    Set LOC := PTR, and L OC P := SAVE, and Return.Set LOC := PTR, and L OC P := SAVE, and Return.[End o f If structure.][End o f If structure.]

    6.6. Set SAVESet SAVE := PTR. and PTR := L INK [PTR]. [ Updates pointers.] [End o f Step 4 loop.]:= PTR. and PTR := L INK [PTR]. [ Updates pointers.] [End o f Step 4 loop.]7.7. Set LOC := NU LL [Search unsuccessf ul.]Set LOC := NU LL [Search unsuccessf ul.]8.8.

    Return.Return.

  • 8/4/2019 Data Structure Lecture 12,13

    6/6

    Deletion of node after searchingDeletion of node after searchingD ELETE( INFO , 1 INK , START, AVA IL, I TEM ) D ELETE( INFO , 1 INK , START, AVA IL, I TEM )

    This algorithm deletes f rom a linked list the. f irst node N which This algorithm deletes f rom a linked list the. f irst node N whichcontains the given I TEM o f inf orntion.contains the given I TEM o f inf orntion.

    1.1. [ Use Procedure 5.9 to lind the location o f N and its preceding node.][ Use Procedure 5.9 to lind the location o f N and its preceding node.]

    Call F TNDB ( INFO , LINK , START, I TEM, LOC , LOC P ) Call F TNDB ( INFO , LINK , START, I TEM, LOC , LOC P ) 2.2. If 1.If 1.--OC NU LL, then: Write: I TEM not in list, and Exit.OC NU LL, then: Write: I TEM not in list, and Exit.3.3. [delete nock.] If LOC P NU LL, then:[delete nock.] If LOC P NU LL, then:

    Set START : LINK (START]. ( D eletes f irst node.1Set START : LINK (START]. ( D eletes f irst node.1Else:Else:

    Set LIN ,[LOC P] := L INK [LOC ].Set LIN ,[LOC P] := L INK [LOC ].[End.if If ][End.if If ]

    4.4. [Return deleted nude to the AVA IL list.][Return deleted nude to the AVA IL list.]Set LINK [LOC ] = AVA IL and AVA IL : = LOC .Set LINK [LOC ] = AVA IL and AVA IL : = LOC .

    5.5.Exit.Exit.