Linked Lists (continued). Maintaining Order Creating an ordered list 3 Baltimore head Rome Seattle...

download Linked Lists (continued). Maintaining Order Creating an ordered list 3 Baltimore head Rome Seattle Toronto  (1) Insert node containing (reference to.

If you can't read please download the document

description

Creating an ordered list 3 Baltimore head Rome Seattle Toronto  (1) Insert node containing (reference to ) the string “Paris” Starting from head proceed until reach a node with string > ”Paris”. Then insert node containing “Paris” before this node. Need to insert node containing “Paris” between nodes containing “Baltimore” and “Rome” ADS2 Lecture 4

Transcript of Linked Lists (continued). Maintaining Order Creating an ordered list 3 Baltimore head Rome Seattle...

Linked Lists (continued) Maintaining Order Creating an ordered list 3 Baltimore head Rome Seattle Toronto (1) Insert node containing (reference to ) the string Paris Starting from head proceed until reach a node with string > Paris. Then insert node containing Paris before this node. Need to insert node containing Paris between nodes containing Baltimore and Rome ADS2 Lecture 4 Creating an ordered list contd. 4 Baltimore head Rome Seattle Toronto Paris Create node containing string Paris pointing to the Rome node. Baltimore head RomeSeattleToronto Paris Make node Baltimore node point to Paris node. As we progress along the list, Need to keep a record of the previous node visited. ADS2 Lecture 4 5 (2) Go back to the original list. Insert node containing (reference to ) the string Aberdeen Starting from head proceed until reach a node with string>Aberdeen. Then insert node containing Aberdeen before this node. Need to insert node containing Aberdeen at the head of the list. No previous node in this case. Add to head as before. Creating an ordered list contd. (3) Go back to the original list. Insert node containing (reference to ) the string Verona Starting from head proceed until reach a node with string>Verona. But this doesnt happen we reach null. We can add to the end of the list without needing a last instance variable (see Lecture 3) as we are keeping copy of previous node. Baltimore head Rome Seattle Toronto ADS2 Lecture 4 6 Baltimore head Rome Seattle Toronto Verona Allocate new node containing Verona, pointing to null Creating an ordered list contd. Make previous node point to Verona node Baltimore head Rome Seattle Toronto Verona So in general to insert a node containing string myString, starting from head proceed until reach null or a node with string>myString. Remember: Do not try to check the contents of a node if it is null. ADS2 Lecture 4 Creating an ordered list contd. What do you think of this as a way of documenting code? Know that we have order, how can isPresent exploit it? Check it out remove/delete Removing all nodes containing a given value Baltimore head Rome Seattle Toronto To remove all nodes containing string Rome: Start at head and proceed along list. If a node temp contains the String Rome - Redirect prev to point to the node pointed to by temp continue pattern/template Found what we are looking for? The actual removal of a node Removing head of list The actual removal of a node Removing an arbitrary node otherwise, move on advance the cursor dog cat owl doghen dog head s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog dog cat owl doghen dog head cursor prev s = dog Know that we have order, how can isPresent exploit it? Below, linear search on a sorted array of strings. Note early stopping condition. Can we incorporate this into isPresent above for linked lists?