1 / 10

# Linked Lists 2 - PowerPoint PPT Presentation

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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.

• Getting Elements in Linked Lists

• Removing Elements in Linked Lists

• The DNA Assignment

• 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

• 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 Operations Are?

• 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

DNA Operations Are?