1 / 10

Linked Lists 2

Linked Lists 2. As you arrive… Snarf the code for today’s class Grab a worksheet Take a look at the constructor for the LinkedList class you snarfed that takes an int []. Try to understand how to works. Feel free to run and play with the code to understand it. The Big O.

iliana
Télécharger la présentation

Linked Lists 2

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. Linked Lists 2 As you arrive… Snarf the code for today’s class Grab a worksheet Take a look at the constructor for the LinkedList class you snarfed that takes an int[]. Try to understand how to works. Feel free to run and play with the code to understand it. The Big O

  2. Take a look at the constructor for the LinkedList class you snarfed that takes an int[]. Which of these statements best describes its operation? • It goes through each element of the input array in order, and adds them to the head of the linked list. As a result, the list is the reverse of the array. • It goes through each element of the input array in order, and adds them to the tail (end) of the linked list. As a result, the list is the and the array are in the same order. • It goes through each element of the input array in reverse and adds them to the head of the linked list. As a result, the list is the and the array are in the same order. • It goes through each element of the input array in reverse and adds them to the tail of the linked list. As a result, the list is the reverse of the array.

  3. On the agenda: More Practice With Linked Lists • Getting Elements in Linked Lists • Removing Elements in Linked Lists • The DNA Assignment • Madness

  4. Inner Class (Reprise)

  5. Getting elements in a linked list • Write a function getElement(inti) that takes an index and returns the element in a linked list at that index • If the element is not in the list, return -1 • This has a very cute recursive solution, BTW • If you finish that, write a function getMiddleElement() that returns the middle element of a linked list

  6. Removing Elements from a Linked List • Write a function removeFoursthat removes all the elements with value 4 • You can assume that the first element in the linked list will not be a four. • I’ve got 2 hints at the bottom of the source file, if you get stuck • If you finish, change your function so it works if the first element is a 4 (there is a cool recursive formation, but it’s a bit tricky to get) • If you finish that, write a new version of your function that is iterative (i.e. non-recursive) or if you did it iteratively first, do it recursively

  7. What do you suppose the Big-O of the following Linked List Operations Are? • Work with those nearby to fill out the worksheet

  8. Linked List Big O – Summing Up • Linked Lists are good when you need to add or remove a lot of elements • Linked Lists are not so good when you have to access a lot of specific indexes • Sometimes for optimal behavior you have to keep track of specific nodes rather than iterating across the list multiple times

  9. DNA

  10. Linked Lists: Into the Mouth of Madness • Write a function mergeListsthat takes two linked lists as parameters. It should insert the second list into the first list after the first “0” element in the first list • If there’s no 0 in the first list, both lists should be unmodified • If you finish, write the recursive version (you might need 2 recursive functions for this one) • If you finish, change your function so that rather than modifying the existing list in returns a new list which is the merge

More Related