Linked lists 2
Download
1 / 10

Linked Lists 2 - PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on

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.

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

PowerPoint Slideshow about ' Linked Lists 2' - iliana


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.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
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


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
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
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
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
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 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?


Linked lists into the mouth of madness
Linked Lists: Into the Mouth of Madness Operations Are?

  • 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


ad