Linked list traversal insertion deletion
Download
1 / 89

Linked List: Traversal Insertion Deletion - PowerPoint PPT Presentation


  • 398 Views
  • Updated On :

Lecture 9. Linked List: Traversal Insertion Deletion. LB. 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)

Related searches for Linked List: Traversal Insertion Deletion

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Linked List: Traversal Insertion Deletion' - tocho


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Linked list traversal insertion deletion l.jpg

Lecture 9

Linked List: Traversal InsertionDeletion


Linked list traversal l.jpg

LB

Linked List Traversal


Linked list traversal3 l.jpg

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?


The node code l.jpg

LB

The Node Code

Node definesa record

data isoftype Num

next isoftype Ptr toa Node

endrecord


The big picture l.jpg

LB

The Big Picture

42

98

12

6

heap

stack

head


The little picture l.jpg

LB

The Little Picture

12


The classic code l.jpg

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


The big picture8 l.jpg

LB

The Big Picture

42

98

12

6

heap

stack

head


Questions l.jpg

LB

Questions?



Node definition l.jpg
Node Definition

node definesa record

data isoftype Num

next isoftype ptr toa node

endrecord


The scenario l.jpg

//

head

48

17

142

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


Adding an element to a linked list l.jpg
Adding an Element to a Linked List

Involves two steps:

  • Finding the correct location

  • Doing the work to add the node


Finding the correct location l.jpg
Finding the Correct Location

  • Three possible positions:

    • The front

    • The end

    • Somewhere in the middle


Inserting to the front l.jpg

head

93

Inserting to the Front

48

17

142

head

  • There is no work to find the correct location

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


Inserting to the end l.jpg

93

//

Don’t Worry!

Inserting to the End

//

head

48

17

142

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

    • Recursion or iteration


Inserting to the middle l.jpg

93

//

142

Inserting to the Middle

//

head

17

48

142

  • Used when order is important

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

    • Recursion or iteration


The work to add the node l.jpg
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


Three types of insertion l.jpg
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



Inserting to the front of a linked list l.jpg
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


Animated insert to front of linked list l.jpg

42

Current new_data temp

2

R

Animated Insert to Front of Linked List

head

4

17

2

(current iot in/out ptr toa Node,

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

temp <- new(Node)

temp^.data <- new_data

temp^.next <- current

current <- temp



Inserting to end of a linked list l.jpg
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


Inserting to the end of a linked list l.jpg
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


Inserting at the end of a linked list26 l.jpg
Inserting at the End of a Linked List

current new_data 53

current new_data 53

current new_data 53

current new_data 53

R

R

R

R

head

48

17

142

53



Inserting in order into a linked list28 l.jpg
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


Inserting in order into a linked list29 l.jpg
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)

endif

endprocedure //Insert_In_Order


Inserting in order into a linked list30 l.jpg

R

R

R

current new_data temp

current new_data temp

current new_data temp

19

19

19

Inserting In Order into a Linked List

Head

13

18

23

19


Summary l.jpg
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)




The node definition l.jpg
The Node Definition

Node definesa record

data isoftype num

next isoftype ptr toa Node

endrecord


The scenario35 l.jpg

6

42

The Scenario

head

  • Begin with an existing linked list

    • Could be empty or not

    • Could be ordered or not

4

17


The scenario36 l.jpg

6

42

The Scenario

head

  • Begin with an existing linked list

    • Could be empty or not

    • Could be ordered or not

4

17


The scenario37 l.jpg

42

The Scenario

head

  • Begin with an existing linked list

    • Could be empty or not

    • Could be ordered or not

4

17


The scenario38 l.jpg

42

The Scenario

head

  • Begin with an existing linked list

    • Could be empty or not

    • Could be ordered or not

4

17


Finding the match l.jpg
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


Deleting the first element l.jpg
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

    endif

    endprocedure


Deleting from a linked list l.jpg
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.


Slide42 l.jpg

6

42

head

4

17

.

.

Delete(head, 4)

.

.


Slide43 l.jpg

6

42

head

4

17

.

.

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

endif

endprocedure

Target = 4


Slide44 l.jpg

6

42

head

4

17

.

.

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

endif

endprocedure

Target = 4


Slide45 l.jpg

6

42

head

4

17

.

.

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

endif

endprocedure

Target = 4


Slide46 l.jpg

6

42

head

4

17

.

.

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

endif

endprocedure

Target = 4


Slide47 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

42

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide48 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

42

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide49 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

42

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide50 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

42

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide51 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

42

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide52 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

42

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide53 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

42

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide54 l.jpg

6

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

endif

endprocedure

42

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide55 l.jpg

6

42

head

17

.

.

Delete(head, 4)

.

.


Linked list deletion all occurrences l.jpg

Linked List Deletion(All Occurrences)


Deleting all occurrences l.jpg
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


Slide58 l.jpg

6

4

head

4

17

.

.

Delete(head, 4)

.

.


Slide59 l.jpg

6

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

endif

endprocedure

4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide60 l.jpg

6

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

endif

endprocedure

4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide61 l.jpg

6

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

endif

endprocedure

4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide62 l.jpg

6

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

endif

endprocedure

4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide63 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide64 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide65 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide66 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide67 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide68 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide69 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide70 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide71 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide72 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide73 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide74 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide75 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide76 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

4

Target = 4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide77 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

Target = 4

Target = 4

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide78 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

Target = 4

Target = 4

Target = 4

Target = 4

head

4

17

.

.

Delete(head, 4)

.

.


Slide79 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

Target = 4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide80 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

Target = 4

Target = 4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide81 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

Target = 4

Target = 4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide82 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

Target = 4

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide83 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

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

endif

endprocedure

Target = 4

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide84 l.jpg

6

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

endif

endprocedure

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

endif

endprocedure

Target = 4

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide85 l.jpg

6

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

endif

endprocedure

Target = 4

head

17

.

.

Delete(head, 4)

.

.


Slide86 l.jpg

6

head

17

.

.

Delete(head, 4)

.

.


Summary87 l.jpg
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



ad