Transcript
Page 1: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Linked List: Traversal InsertionDeletion

Page 2: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Linked List Traversal

LB

Page 3: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Linked List Traversal

• Traversal means “visiting” or examining each node.

• Simple linked list– Start at the beginning– Go one node at a time until the end

• Recursive procedure (or function)– Given a head pointer– Looks at just one node– What procedure will look at the rest?

LB

Page 4: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Node Code

Node definesa record

data isoftype Num

next isoftype Ptr toa Node

endrecord

LB

Page 5: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Big Picture

42

heap

stackhead

98 12 6

LB

Page 6: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Little Picture

12

LB

Page 7: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Classic Code

Procedure Traverse

(current isoftype in Ptr toa Node)

// Precon: Pass in pointer to a list

// Purpose: Print each data item

// Postcon: No changes to list

if(current <> NIL) then

print(current^.data)

Traverse(current^.next)

endif

endprocedure // Traverse

LB

Page 8: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Big Picture

42

heap

stackhead

98 12 6

LB

Page 9: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Questions?

LB

Page 10: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Insertion Into Linked Lists

Page 11: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Node Definition

node definesa record

data isoftype Num

next isoftype ptr toa node

endrecord

Page 12: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Scenario

• You have a linked list– Perhaps empty, perhaps not– Perhaps ordered, perhaps not

• You want to add an element into the linked list

48 17 142head //

Page 13: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Adding an Element to a Linked List

Involves two steps:

• Finding the correct location

• Doing the work to add the node

Page 14: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Finding the Correct Location

• Three possible positions:– The front– The end– Somewhere in the middle

Page 15: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

head

Inserting to the Front

• There is no work to find the correct location

• Empty or not, head will point to the right location

48 17 142head 93

Page 16: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting to the End

• Find the end of the list(when at NIL)

– Recursion or iteration

48 17 142head //93 //

Don’t Worry!

Page 17: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting to the Middle

• Used when order is important

• Go to the node that should follow the one to add

– Recursion or iteration

17 48 142head //93 //142

Page 18: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Work to Add the Node

• Create the new node• Fill in the data field• Deal with the next field

– Point to nil (if inserting to end)– Point to current (front or middle)

temp <- new(Node) temp^.data <- new_data temp^.next <- current current <- temp

Page 19: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Three Types of Insertion

• To front– No recursion needed

• To end– Get to the end, then add node

• In order (in middle)– To maintain sorted property

Page 20: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting at the Front of a Linked List

Page 21: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting to the Front of a Linked List

• Need an in/out pointer parameter

• Create new node• Fill in data• Make new node’s next pointer point to

current• Update current to point to new node

Page 22: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

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 <- tempendprocedure

4 17

head

42

Current new_data temp2R

2

Animated Insert to Front of Linked List

(current iot in/out ptr toa Node,

temp <- new(Node)temp^.data <- new_datatemp^.next <- currentcurrent <- temp

Page 23: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting at the End of a Linked List

Page 24: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting to End of a Linked List

• Recursively traverse the list until at end• Then:

– Create new node at current– Fill in data– Terminate the new node’s

next pointer to point to NIL

Page 25: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting to the End of a Linked List

procedure Add_To_End( current isoftype in/out Ptr toa Node, new_data isoftype in Num )

// Purpose: Add new node to end of list

// Pre: current points to NIL-terminated list

// Post: new list has added element at end

if( current = NIL ) then

current <- new( Node )

current^.data <- new_data

current^.next <- NIL

else

Add_To_End( current^.next, new_data)

endif

endprocedure //Add_To_End

Page 26: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

48 17 142head 53

current new_data 53R

current new_data 53R

current new_data 53R

current new_data 53R

Inserting at the End of a Linked List

Page 27: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting in Order into a Linked List

Page 28: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting In Order into a Linked List

• Recursively traverse until you find the correct place to insert– Compare to see if you need to insert before

current– If adding largest value, then insert at the end

• Perform the commands to do the insertion– Create new node– Add data– Update the next pointer of the new node– Update current to point to new node

Page 29: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Inserting in Order into a Linked List

procedure Insert_In_Order(current isoftype in/out Ptr toa Node, new_data isoftype in Num )

// comments here temp isoftype Ptr toa Node if ((current = NIL) OR (current^.data > new_data)) then temp <- new( Node ) temp^.data <- new_data temp^.next <- current current <- temp else Insert_In_Order(current^.next,new_data) endifendprocedure //Insert_In_Order

Page 30: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

13 18 23Head

Inserting In Order into a Linked List

19

current new_data tempR 19

current new_data tempR 19

current new_data tempR 19

Page 31: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

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)

Page 32: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Questions?

Page 33: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Deleting an Element from a Linked List

Page 34: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Node Definition

Node definesa record

data isoftype num

next isoftype ptr toa Node

endrecord

Page 35: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Scenario

• Begin with an existing linked list– Could be empty or not– Could be ordered or not

4 17

head

426

Page 36: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Scenario

• Begin with an existing linked list– Could be empty or not– Could be ordered or not

4 17

head

426

Page 37: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Scenario

• Begin with an existing linked list– Could be empty or not– Could be ordered or not

4 17

head

42

Page 38: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

The Scenario

• Begin with an existing linked list– Could be empty or not– Could be ordered or not

4 17

head

42

Page 39: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Finding the Match

• We’ll examine three situations:

– Delete the first element

– Delete the first occurrence of an element

– Delete all occurrences of a particular element

Page 40: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Deleting the First Element

• This can be done without any traversal/searching• Requires an in/out pointer

procedure DeleteFront(current iot in/out ptr toa Node)

// deletes the first node in the list if (current <> nil) then current <- current^.next endifendprocedure

Page 41: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Deleting from a Linked List

• Deletion from a linked list involves two steps:– Find a match to the element to be deleted

(traverse until nil or found)– Perform the action to delete

• Performing the deletion is trivial:

current <- current^.next– This removes the element, since nothing will

point to the node.

Page 42: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

426

.

.Delete(head, 4)..

Page 43: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

4 17

head

426

Target = 4

Page 44: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

4 17

head

426

Target = 4

Page 45: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

4 17

head

426

Target = 4

Page 46: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

4 17

head

426

Target = 4

Page 47: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

4 17

head

426

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

Page 48: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

4 17

head

426

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

Page 49: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

4 17

head

426

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

Page 50: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

4 17

head

426

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

Page 51: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

4 17

head

426

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

Page 52: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

4 17

head

426

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

Page 53: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

17

head

426

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

Page 54: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

17

head

426

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then

cur <- cur^.next else

Delete(cur^.next, target) endif endifendprocedure

Target = 4

Page 55: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

.

.Delete(head, 4)..

17

head

426

Page 56: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Linked List Deletion(All Occurrences)

Page 57: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Deleting All Occurrences

• Deleting all occurrences is a little more difficult.

• Traverse the entire list and don’t stop until you reach nil.

• If you delete, recurse on current• If you don’t delete, recurse on

current^.next

Page 58: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

Page 59: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 60: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 61: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 62: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 63: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 64: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 65: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 66: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 67: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

4 17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 68: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 69: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 70: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 71: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 72: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 73: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 74: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 75: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 76: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 77: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 78: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

46

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 79: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

6

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 80: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

6

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 81: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

6

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 82: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

6

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 83: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

6

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 84: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

6

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 85: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

6

.

.Delete(head, 4)..

procedure Delete(cur iot in/out ptr toa Node, target isoftype in num)

// 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 endifendprocedure

Target = 4

Page 86: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

17

head

6

.

.Delete(head, 4)..

Page 87: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

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

Page 88: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Questions?

Page 89: Linked List: Traversal Insertion Deletion. Linked List Traversal LB

Recommended