1 / 30

Review

Review. Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion. Linked List Traversal. 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

Download Presentation

Review

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review • Linked List • Insertion Description • Deletion Description • Basic Node Implementation • Conclusion

  2. Linked List Traversal • 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)

  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?

  4. The Node Code Node definesa record data isoftype Num next isoftype Ptr toa Node endrecord

  5. LB The Big Picture 42 98 12 6 heap stack head

  6. The Little Picture 12

  7. The Classic Code Procedure Traverse (current isoftype in Ptrtoa 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

  8. The Big Picture 42 98 12 6 heap stack head

  9. Insertion Into Linked Lists

  10. Node Definition node definesa record data isoftype Num next isoftype ptr toa node endrecord

  11. // 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

  12. Adding an Element to a Linked List Involves two steps: • Finding the correct location • Doing the work to add the node

  13. Finding the Correct Location • Three possible positions: • The front • The end • Somewhere in the middle

  14. head 93 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 142 head

  15. 93 // Don’t Worry! Inserting to the End • Find the end of the list(when at NIL) • Recursion or iteration // head 48 17 142

  16. 93 // 142 Inserting to the Middle • Used when order is important • Go to the node that should follow the one to add • Recursion or iteration // head 17 48 142

  17. 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

  18. 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

  19. Inserting at the Front of a Linked List

  20. 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

  21. 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

  22. Inserting at the End of a Linked List

  23. 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

  24. 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

  25. 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

  26. Inserting in Order into a Linked List

  27. 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

  28. 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

  29. 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

  30. 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

More Related