linked lists - cs.miami.edu · linked list i doubly linked list i a different way of storing a...

120
Linked Lists Victor Milenkovic Department of Computer Science University of Miami CSC220 Programming II – Spring 2019

Upload: others

Post on 03-Sep-2019

16 views

Category:

Documents


0 download

TRANSCRIPT

Linked Lists

Victor Milenkovic

Department of Computer ScienceUniversity of Miami

CSC220 Programming II – Spring 2019

Outline

Arrays are slow

I Speed of SortedPD addOrChangeEntry:I The find method for SortedPD is O(log n).I But add must also insert a new entry at the index i returned by find.I Move all the entries from i to size − 1.I That takes O(n), which dominates the running time.I No hope of a fast addOrChange method for large n.

Arrays are slow

I Speed of SortedPD addOrChangeEntry:

I The find method for SortedPD is O(log n).I But add must also insert a new entry at the index i returned by find.I Move all the entries from i to size − 1.I That takes O(n), which dominates the running time.I No hope of a fast addOrChange method for large n.

Arrays are slow

I Speed of SortedPD addOrChangeEntry:I The find method for SortedPD is O(log n).

I But add must also insert a new entry at the index i returned by find.I Move all the entries from i to size − 1.I That takes O(n), which dominates the running time.I No hope of a fast addOrChange method for large n.

Arrays are slow

I Speed of SortedPD addOrChangeEntry:I The find method for SortedPD is O(log n).I But add must also insert a new entry at the index i returned by find.

I Move all the entries from i to size − 1.I That takes O(n), which dominates the running time.I No hope of a fast addOrChange method for large n.

Arrays are slow

I Speed of SortedPD addOrChangeEntry:I The find method for SortedPD is O(log n).I But add must also insert a new entry at the index i returned by find.I Move all the entries from i to size − 1.

I That takes O(n), which dominates the running time.I No hope of a fast addOrChange method for large n.

Arrays are slow

I Speed of SortedPD addOrChangeEntry:I The find method for SortedPD is O(log n).I But add must also insert a new entry at the index i returned by find.I Move all the entries from i to size − 1.I That takes O(n), which dominates the running time.

I No hope of a fast addOrChange method for large n.

Arrays are slow

I Speed of SortedPD addOrChangeEntry:I The find method for SortedPD is O(log n).I But add must also insert a new entry at the index i returned by find.I Move all the entries from i to size − 1.I That takes O(n), which dominates the running time.I No hope of a fast addOrChange method for large n.

Linked List

I Doubly Linked ListI A different way of storing a list.I Allows us to add or remove an entry in O(1) time.

I The DLLEntry class.I Extends prog02.DirectoryEntry.I Adds next and previous fieldI with get and set methods.I References to the next and previous entries in the list.

Linked List

I Doubly Linked List

I A different way of storing a list.I Allows us to add or remove an entry in O(1) time.

I The DLLEntry class.I Extends prog02.DirectoryEntry.I Adds next and previous fieldI with get and set methods.I References to the next and previous entries in the list.

Linked List

I Doubly Linked ListI A different way of storing a list.

I Allows us to add or remove an entry in O(1) time.I The DLLEntry class.

I Extends prog02.DirectoryEntry.I Adds next and previous fieldI with get and set methods.I References to the next and previous entries in the list.

Linked List

I Doubly Linked ListI A different way of storing a list.I Allows us to add or remove an entry in O(1) time.

I The DLLEntry class.I Extends prog02.DirectoryEntry.I Adds next and previous fieldI with get and set methods.I References to the next and previous entries in the list.

Linked List

I Doubly Linked ListI A different way of storing a list.I Allows us to add or remove an entry in O(1) time.

I The DLLEntry class.

I Extends prog02.DirectoryEntry.I Adds next and previous fieldI with get and set methods.I References to the next and previous entries in the list.

Linked List

I Doubly Linked ListI A different way of storing a list.I Allows us to add or remove an entry in O(1) time.

I The DLLEntry class.I Extends prog02.DirectoryEntry.

I Adds next and previous fieldI with get and set methods.I References to the next and previous entries in the list.

Linked List

I Doubly Linked ListI A different way of storing a list.I Allows us to add or remove an entry in O(1) time.

I The DLLEntry class.I Extends prog02.DirectoryEntry.I Adds next and previous field

I with get and set methods.I References to the next and previous entries in the list.

Linked List

I Doubly Linked ListI A different way of storing a list.I Allows us to add or remove an entry in O(1) time.

I The DLLEntry class.I Extends prog02.DirectoryEntry.I Adds next and previous fieldI with get and set methods.

I References to the next and previous entries in the list.

Linked List

I Doubly Linked ListI A different way of storing a list.I Allows us to add or remove an entry in O(1) time.

I The DLLEntry class.I Extends prog02.DirectoryEntry.I Adds next and previous fieldI with get and set methods.I References to the next and previous entries in the list.

No array needed

I So we don’t need an array anymore.I All we need is a reference to any element of the list

I Call getNext() or getPrevious() repeatedly.I Get to any other element.

I For convenience, it is customary to store references toI head, the first entry in the listI tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

No array needed

I So we don’t need an array anymore.

I All we need is a reference to any element of the listI Call getNext() or getPrevious() repeatedly.I Get to any other element.

I For convenience, it is customary to store references toI head, the first entry in the listI tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

No array needed

I So we don’t need an array anymore.I All we need is a reference to any element of the list

I Call getNext() or getPrevious() repeatedly.I Get to any other element.

I For convenience, it is customary to store references toI head, the first entry in the listI tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

No array needed

I So we don’t need an array anymore.I All we need is a reference to any element of the list

I Call getNext() or getPrevious() repeatedly.

I Get to any other element.I For convenience, it is customary to store references to

I head, the first entry in the listI tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

No array needed

I So we don’t need an array anymore.I All we need is a reference to any element of the list

I Call getNext() or getPrevious() repeatedly.I Get to any other element.

I For convenience, it is customary to store references toI head, the first entry in the listI tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

No array needed

I So we don’t need an array anymore.I All we need is a reference to any element of the list

I Call getNext() or getPrevious() repeatedly.I Get to any other element.

I For convenience, it is customary to store references to

I head, the first entry in the listI tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

No array needed

I So we don’t need an array anymore.I All we need is a reference to any element of the list

I Call getNext() or getPrevious() repeatedly.I Get to any other element.

I For convenience, it is customary to store references toI head, the first entry in the list

I tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

No array needed

I So we don’t need an array anymore.I All we need is a reference to any element of the list

I Call getNext() or getPrevious() repeatedly.I Get to any other element.

I For convenience, it is customary to store references toI head, the first entry in the listI tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

No array needed

I So we don’t need an array anymore.I All we need is a reference to any element of the list

I Call getNext() or getPrevious() repeatedly.I Get to any other element.

I For convenience, it is customary to store references toI head, the first entry in the listI tail, the last entry in the list

I The slides show how to use this structure to implement a phonedirectory.

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.

I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.

I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?

I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.

I How do we get the name at entry? (Jay)I That’s not the one we want,

I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,

I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.

I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignment

I entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =

I What do we set entry equal to?I The loop should return when it finds Ian,

I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,

I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?

I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?

I What value of entry tells us that we have seen everything in the list?

Finding a name

I To find Ian, we need to look at each entry.I Set the variable entry to the first one.I How do we do that?I Compare the name at entry to the name we are looking for.I How do we get the name at entry? (Jay)

I That’s not the one we want,I so we need to move entry forward one.I The only way to change the value of entry is an assignmentI entry =I What do we set entry equal to?

I The loop should return when it finds Ian,I but what if Ian is not there?I What will stop the loop?I What value of entry tells us that we have seen everything in the list?

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntry

I calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.

I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.

I How does it set them?I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its next

entry.I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?

I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?

I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?

I Similarly Ann’s must point back to Zoe’s.I At this point Ian thinks he is still in the list,

I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,

I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.

I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!

I Similar to entries in array with index bigger than size.

Removing an entry

I removeEntryI calls find("Ian") to find its entry in the list.I Then it sets variables next and previous.I How does it set them?

I Next removeEntry must tell Zoe’s entry to use Ann’s entry as its nextentry.

I What method sets Zoe’s next entry pointer?I How do we invoke it?I What value do we give it?I Similarly Ann’s must point back to Zoe’s.

I At this point Ian thinks he is still in the list,I but he really isn’t.I Everyone is ignoring him!I Similar to entries in array with index bigger than size.

Adding Ian

I How do we add Ian back again?I Let’s just put him at the end.I Three values have to change for this to happen.I Which values?I How do we set them?I To what?

Adding Ian

I How do we add Ian back again?

I Let’s just put him at the end.I Three values have to change for this to happen.I Which values?I How do we set them?I To what?

Adding Ian

I How do we add Ian back again?I Let’s just put him at the end.

I Three values have to change for this to happen.I Which values?I How do we set them?I To what?

Adding Ian

I How do we add Ian back again?I Let’s just put him at the end.I Three values have to change for this to happen.

I Which values?I How do we set them?I To what?

Adding Ian

I How do we add Ian back again?I Let’s just put him at the end.I Three values have to change for this to happen.I Which values?

I How do we set them?I To what?

Adding Ian

I How do we add Ian back again?I Let’s just put him at the end.I Three values have to change for this to happen.I Which values?I How do we set them?

I To what?

Adding Ian

I How do we add Ian back again?I Let’s just put him at the end.I Three values have to change for this to happen.I Which values?I How do we set them?I To what?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.

I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.

I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?

I What does it return if we were adding Zora?I SortedDLLPD add uses the output of find.

I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.

I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.

I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)

I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.

I How does it set previous? (A little harder.)I To insert new entry Ian between next and previous, add has to set four

pointers.I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?

I What method do we use?I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?

I For each of the four times, how do we call it and what is the value?

SortedDLLPD find and add

I SortedDLLPD find must tell us where to put Ian if he is not there.I In that case it returns the entry after Ian.I How does it know it has reached this entry?I What does it return if we were adding Zora?

I SortedDLLPD add uses the output of find.I It sets the variable next to the entry that should be next after Ian.I How does it set next? (Easy!!)I It sets the variable previous to the entry that should be before Ian.I How does it set previous? (A little harder.)

I To insert new entry Ian between next and previous, add has to set fourpointers.

I What are they?I What method do we use?I For each of the four times, how do we call it and what is the value?

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simple

I It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.

I For example:I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;

I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();

I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);

I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!

I And the three parts of a for-loop control should each be considered a“line”:

I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:

I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.

I Write the line that makes that change happen.

Keep it simple

I Keep each line of your program simpleI It should involve at most two variables.I For example:

I entry = head;I previous = next.getPrevious();I entry.setNext(next);I if (next == null)

I Of course you will use getNext, setPrevious, and other variable names!I And the three parts of a for-loop control should each be considered a

“line”:I for (line1; line2; line3) {

I Draw the diagram of what should happen.I Write the line that makes that change happen.

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):

I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)

I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reason

I but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call find

I so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)

I Similarly, addOrChangeEntry is still O(n)I For SortedDLLPD (sorted):

I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):

I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(

I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t help

I because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!

I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!

I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call find

I so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)

I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)

I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call find

I so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

speed

I For DLLBasedPD (unsorted):I find is still O(n)I removal is still O(1), but for a different reasonI but removeEntry must call findI so it is still O(n)I Similarly, addOrChangeEntry is still O(n)

I For SortedDLLPD (sorted):I find is still O(n) :-(I binary search doesn’t helpI because it takes O(n) to get to the middle element!I removal is now O(1)!I but removeEntry must call findI so it is still O(n)I add is now O(1)I but addOrChangeEntry must call findI so it is still O(n).

I One step forward, two steps back!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.

I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),

I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.

I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),

I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.

I We will have to keeping working on improving the running time.I When programming a linked list:

I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:

I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.

I Program each change as a lineI with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a line

I with only two variables.I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a lineI with only two variables.

I Keep each step simple!

Summary

I The (doubly) linked list is a new way to store a list.I Adding or removing an entry at a known location is O(1),I in contrast to O(n) for an array.I But getting to the i th element takes O(n),I in contrast to O(1) for an array.I We will have to keeping working on improving the running time.

I When programming a linked list:I Draw the diagram of each change.I Program each change as a lineI with only two variables.I Keep each step simple!