100 likes | 181 Views
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.
E N D
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
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.
On the agenda: More Practice With Linked Lists • Getting Elements in Linked Lists • Removing Elements in Linked Lists • The DNA Assignment • Madness
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
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
What do you suppose the Big-O of the following Linked List Operations Are? • Work with those nearby to fill out the worksheet
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
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