Professional Documents
Culture Documents
Cs 1311 Lecture 09 WDL
Cs 1311 Lecture 09 WDL
Cs 1311 Lecture 09 WDL
LB
LB
LB
Node definesa record data isoftype Num next isoftype Ptr toa Node endrecord
LB
LB
LB
LB
LB
Questions?
Node Definition
node definesa record data isoftype Num next isoftype ptr toa node endrecord
The Scenario
You have a linked list Perhaps empty, perhaps not Perhaps ordered, perhaps not
head
48 17 142
//
93 head
48
17
142
There is no work to find the correct location Empty or not, head will point to the right location
//93
//
142 //
//
Used when order is important Go to the node that should follow the one to add Recursion or iteration
head
Animated Insert to Front of Linked List
4 2
17
42
Current
new_data
temp
procedure Insert (current iot in/out ptr toa Node, new_data isoftype in Num) temp isoftype ptr toa Node temp <- new(Node) temp^.data <- new_data temp^.next <- current current <- temp endprocedure
new_data 19 temp
new_data 19 temp
new_data 19 temp
Summary
Inserting into a linked list involves two steps: Find the correct location Do the work to insert the new value We can insert into any position Front End Somewhere in the middle (to preserve order)
Questions?
The Scenario
head 6 4 17 42
Begin with an existing linked list Could be empty or not Could be ordered or not
The Scenario
head 6 4 17 42
Begin with an existing linked list Could be empty or not Could be ordered or not
The Scenario
head 4 17
42
Begin with an existing linked list Could be empty or not Could be ordered or not
The Scenario
head 4 17
42
Begin with an existing linked list Could be empty or not Could be ordered or not
Well examine three situations: Delete the first element Delete the first occurrence of an element Delete all occurrences of a particular element
This removes the element, since nothing will point to the node.
head
6
. . Delete(head, 4) . .
17
42
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. . if(cur <> NIL) then . if(cur^.data = target) then
Target = 4
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. . if(cur <> NIL) then . if(cur^.data = target) then
Target = 4
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. . if(cur <> NIL) then . if(cur^.data = target) then
Target = 4
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. . if(cur <> NIL) then . if(cur^.data = target) then
Target = 4
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, ptr toa . procedure Delete(cur iot in/out in num) Node, target isoftype target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. occurrence of a node. . // Delete singlethen if(cur <> NIL) if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. occurrence of a node. . // Delete singlethen if(cur <> NIL) if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. occurrence of a node. . // Delete singlethen if(cur <> NIL) if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. occurrence of a node. . // Delete singlethen if(cur <> NIL) if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. occurrence of a node. . // Delete singlethen if(cur <> NIL) if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
42
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. occurrence of a node. . // Delete singlethen if(cur <> NIL) if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 17
42
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. occurrence of a node. . // Delete singlethen if(cur <> NIL) if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 17
42
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete single occurrence of a node. . if(cur <> NIL) then . if(cur^.data = target) then
Target = 4
head
6
. . Delete(head, 4) . .
17
42
head
6
. . Delete(head, 4) . .
17
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . if(cur <> NIL) then . if(cur^.data = target) then
cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endif endprocedure
Target = 4
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . if(cur <> NIL) then . if(cur^.data = target) then
cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endif endprocedure
Target = 4
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . if(cur <> NIL) then . if(cur^.data = target) then
cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endif endprocedure
Target = 4
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . if(cur <> NIL) then . if(cur^.data = target) then
cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endif endprocedure
Target = 4
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, ptr toa . procedure Delete(cur iot in/out in num) Node, target isoftype target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . // Delete all occurrences of a node. if(cur <> NIL) then if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) Delete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . // Delete all occurrences of a node. if(cur <> NIL) then if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) Delete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . // Delete all occurrences of a node. if(cur <> NIL) then if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) Delete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . // Delete all occurrences of a node. if(cur <> NIL) then if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) Delete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 4 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . // Delete all occurrences of a node. if(cur <> NIL) then if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) Delete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . // Delete all occurrences of a node. if(cur <> NIL) then if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) Delete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . // Delete all occurrences of a node. if(cur <> NIL) then if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) Delete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. . if(cur <> NIL)occurrences of a node. // Delete all then if(cur <> NIL) then . if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) elseDelete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 4 endprocedure endif Target = endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. . if(cur <> NIL)occurrences of a node. // Delete all then if(cur <> NIL) then . if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) elseDelete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 4 endprocedure endif Target = endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. . if(cur <> NIL)occurrences of a node. // Delete all then if(cur <> NIL) then . if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) elseDelete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 4 endprocedure endif Target = endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. . if(cur <> NIL)occurrences of a node. // Delete all then if(cur <> NIL) then . if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) elseDelete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 4 endprocedure endif Target = endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in // Delete all then of a node. // Delete all then if(cur <> NIL)occurrences of a node. . if(cur <> NIL) then if(cur^.data = target) then if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) else Delete(cur^.next, target) else Delete(cur^.next, target) endif Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Target == 4 endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in // Delete all then of a node. // Delete all then if(cur <> NIL)occurrences of a node. . if(cur <> NIL) then if(cur^.data = target) then if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) else Delete(cur^.next, target) else Delete(cur^.next, target) endif Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Target == 4 endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in // Delete all then of a node. // Delete all then if(cur <> NIL)occurrences of a node. . if(cur <> NIL) then if(cur^.data = target) then if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) else Delete(cur^.next, target) else Delete(cur^.next, target) endif Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Target == 4 endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in // Delete all then of a node. // Delete all then if(cur <> NIL)occurrences of a node. . if(cur <> NIL) then if(cur^.data = target) then if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) else Delete(cur^.next, target) else Delete(cur^.next, target) endif Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Target == 4 endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in // Delete all then of a node. // Delete all then if(cur <> NIL)occurrences of a node. . if(cur <> NIL) then if(cur^.data = target) then if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) else Delete(cur^.next, target) else Delete(cur^.next, target) endif Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Target == 4 endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in // Delete all then of a node. // Delete all then if(cur <> NIL)occurrences of a node. . if(cur <> NIL) then if(cur^.data = target) then if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) else Delete(cur^.next, target) else Delete(cur^.next, target) endif Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Target == 4 endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrencesiot a node.ptr toa Node, procedure Delete(cur of in/outin num) target isoftype // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in num) // Delete all thentarget of a node. isoftype in // Delete all then if(cur <> NIL)occurrences of a node. . // Delete all then if(cur <> NIL) target) then if(cur^.data =occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then if(cur cur^.next if(cur^.data = then cur <- <> NIL) target) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) Delete(cur^.next, target) else Delete(cur^.next, target) endif else Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Delete(cur^.next, target) Target = 4 endif endif Target == 4 endprocedure endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrencesiot a node.ptr toa Node, procedure Delete(cur of in/outin num) target isoftype // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in num) // Delete all thentarget of a node. isoftype in // Delete all then if(cur <> NIL)occurrences of a node. . // Delete all then if(cur <> NIL) target) then if(cur^.data =occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then if(cur cur^.next if(cur^.data = then cur <- <> NIL) target) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) Delete(cur^.next, target) else Delete(cur^.next, target) endif else Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Delete(cur^.next, target) Target = 4 endif endif Target == 4 endprocedure endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, procedure Delete(cur iot in/outin num) Node, target isoftype ptr toa Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. num) target . if(cur <> NIL)occurrencesisoftype in // Delete all then of a node. // Delete all then if(cur <> NIL)occurrences of a node. . if(cur <> NIL) then if(cur^.data = target) then if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next if(cur^.data = target) then cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) cur <- cur^.next elseDelete(cur, target) elseDelete(cur, target) else Delete(cur^.next, target) else Delete(cur^.next, target) endif Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif Target == 4 endif endif Target 4 4 endprocedure endif Target == 4 endprocedure endif Target endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, procedure Delete(cur iot in/outin target isoftype ptr toa Node, target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. num) target isoftype in // Delete all occurrences of a node. . if(cur <> NIL)occurrences of a node. // Delete all then if(cur <> NIL) then . if(cur <> NIL) then if(cur^.data = target) then if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) cur <- cur^.next Delete(cur, target) elseDelete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 4 endprocedure endif Target = endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, toa . procedure Delete(cur iot in/out ptrnum) Node, target isoftype in target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . // Delete all occurrences of a node. if(cur <> NIL) then if(cur <> NIL) then . if(cur^.data = target) then if(cur^.data = target) then cur <- cur^.next cur <- cur^.next Delete(cur, target) Delete(cur, target) else else Delete(cur^.next, target) Delete(cur^.next, target) endif endif endif Target == 4 endif Target 4 endprocedure endprocedure
head
6 17
. procedure Delete(cur iot in/out ptr toa Node, . target isoftype in num) Delete(head, 4) // Delete all occurrences of a node. . if(cur <> NIL) then . if(cur^.data = target) then
cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endif endprocedure
Target = 4
head
6
. . Delete(head, 4) . .
17
Summary
Deletion involves: Getting to the correct position Moving a pointer so nothing points to the element to be deleted Can delete from any location Front First occurrence All occurrences
Questions?