Linked List: Traversal Insertion Deletion. Linked List Traversal LB

  • Published on
    30-Mar-2015

  • View
    219

  • Download
    5

Embed Size (px)

Transcript

<ul><li>Slide 1</li></ul> <p>Linked List: Traversal Insertion Deletion Slide 2 Linked List Traversal LB Slide 3 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 Slide 4 The Node Code Node definesa record data isoftype Num next isoftype Ptr toa Node endrecord LB Slide 5 The Big Picture 42 heap stack head 98126 LB Slide 6 The Little Picture 12 LB Slide 7 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(currentNIL) then print(current^.data) Traverse(current^.next) endif endprocedure // Traverse LB Slide 8 The Big Picture 42 heap stack head 98126 LB Slide 9 Questions? LB Slide 10 Insertion Into Linked Lists Slide 11 Node Definition node definesa record data isoftype Num next isoftype ptr toa node endrecord Slide 12 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 4817142 head // Slide 13 Adding an Element to a Linked List Involves two steps: Finding the correct location Doing the work to add the node Slide 14 Finding the Correct Location Three possible positions: The front The end Somewhere in the middle Slide 15 head Inserting to the Front There is no work to find the correct location Empty or not, head will point to the right location 4817142 head 93 Slide 16 Inserting to the End Find the end of the list (when at NIL) Recursion or iteration 4817142 head // 93 // Dont Worry! Slide 17 Inserting to the Middle Used when order is important Go to the node that should follow the one to add Recursion or iteration 1748142 head // 93 // 142 Slide 18 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 . Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then cur . Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then cur . Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then cur . Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur . Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur . Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur . Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur . Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur . Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur . Delete(head, 4). 17 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur . Delete(head, 4). 17 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 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(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 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(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 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(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 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(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 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(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 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(curNIL) then if(cur^.data = target) then curNIL) then if(cur^.data = target) then cur 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(curNIL) then if(cur^.data = target) then cur</p>