Delete Node

  • Upload
    prabhu

  • View
    233

  • Download
    0

Embed Size (px)

Citation preview

  • 8/4/2019 Delete Node

    1/17

    2. Delete Node Algorithm

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    2/17

    The primary difference between insertion and deletion1. Insertion you put something in the list

    2. Deletion you remove something from the list

    So in insertion if you create a new node pNew to put into the then for deletion you must obviously know which node to remove

    so in our algorithm that node is given by pLoc

    So difference between insertion and deletion is that in deletion yopLoc and also in insertion you had datain which holds the data to b

    into the node but in delelte node we will have dataout :)

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    3/17

    Logic

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    4/17

    Condition 1: Linked list already has few elements

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    5/17

    Lets go back to our friends list :)

    Lets say we have to remove rachell from the list because she wants to go to beauty parlour :o

    So what you will do?

    We know that in this linked listpoints to phoebephoebe points to rachel

    rachel points to joeyjoey to monica

    and monica to chandler

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    6/17

    So now if we have to remove rachell from the list we have to mphoebe point to joeyand not to rachell :o

    So how do we do it?

    Lets say rachell is pLoc

    And phoebe is pPre

    pPre -> link = pLoc -> link

    thats all :) :)

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    7/17

    Pointing Pointing

    X

    So were going to make phoebe point to joey

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    8/17

    WAIT!!!!!!!!!!!!

    Something important i totally forgot!!!!whenever you see ->link

    it means that pointing to address of next node

    so x = pLoc -> link

    this meansx will have the value of the adress of pNext

    because pNext comes after pLoc

    **If this is confusing ask me again and again!

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    9/17

    Condition 2: Linked list has only one element

    Saturday 17 September 2011

    S f h l k d l l k h

  • 8/4/2019 Delete Node

    10/17

    So if we have a linked list like this

    With only head one node and a terminator

    Count Head

    0 002 X13 001

    And we want to delete that node... the logic we use islist.head = pLoc -> link

    so list.head will point to whatever pLoc was pointing towhich is the terminator

    Count Head

    0 001 X

    So now our linked list will look like this ^

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    11/17

    Break Down The algorithm :)

    Initialisation

    Variable decleration++

    Main Logic

    Finishing

    End*As i said before because were deleting count value is decremented

    * Here were not actually declaring a variable.. what doing is... since the node we want to remove has somwe have to delete that data and so just the data we

    another variable and recycle it in line 21

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    12/17

    1. Initialisation

    Again for this algorithm we need1. the linked list

    2. the node to be deleted3. the node before the node to be deleted

    4. the data in the node to be deleted

    So how do we write the linked list?

    ref list how do we write pLoc and pPre?

    val pLoc val pPre

    how do we write for data?

    ref dataOut *remember in insert node it was dataIn :P

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    13/17

    2. Variable Decleration

    dataOut = pLoc -> data

    this means that dataOut variablewill hold the value/data of pLoc

    we do this to delete off the data later in

    the algorithm in line 21

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    14/17

    3. Main Logic

    Hope this is also easy to understan

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    15/17

    4. Finishing

    1. Decrement count value by 1 because were removing a node

    2. Delete the value stored in dataOut which as you know holds the

    value/data that was stored in pLoc which is the node we want to delete :)

    Saturday 17 September 2011

    5 E d

  • 8/4/2019 Delete Node

    16/17

    5. End

    End is same old story :P

    Saturday 17 September 2011

  • 8/4/2019 Delete Node

    17/17

    Ok So that was delete node only 5 steps 1 step less than insertn

    Hope everything is clear so far princess :)if u have doubts keep asking me :)

    Saturday 17 September 2011