1 / 8

Comparison of ArrayList Efficiency to LinkedList

Comparison of ArrayList Efficiency to LinkedList. Computer Science 4 Mr. Gerb. Reference: Objective: Understand the differences between the efficiencies of LinkedLists and ArrayLists. Add, Size, Clear. Add Adds to end of list In ArrayList might need to resize list

torn
Télécharger la présentation

Comparison of ArrayList Efficiency to LinkedList

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. Comparison of ArrayList Efficiency to LinkedList Computer Science 4 Mr. Gerb Reference: Objective: Understand the differences between the efficiencies of LinkedLists and ArrayLists.

  2. Add, Size, Clear • Add • Adds to end of list • In ArrayList might need to resize list • In LinkedList, simply add to the back of the list (LinkedList keeps a reference to the last node) • Size: Same for ArrayLists as LinkedLists. Both keep track of the size of the list • Clear • ArrayList sets the size to 0 • LinkedList sets the front and back pointers to null. Nodes in the list are garbage-collected • Both O(1)

  3. Get and Set • Get(int n) - Get the nth item in the list • O(1) in ArrayList. Simply retrieve desired Object • Much slower in LinkedList (O(N)) • Pointers are not kept to each element in the list • Method must “count” nodes to the Nth • Set(int n, Object ob) - Set the nth item in the list • Also O(1) in ArrayList. Simply set the desired element. • Also much slower in LikedList (O(N)). Must count nodes.

  4. Remove Remove(int n) - Remove and return nth element: • O(N) in ArrayList. Must move all subsequent elements down one position. Takes an average of N/2 moves. • O(N) in Linked list: • The removal is easy (O(1), need only to change the pointers on the next and previous nodes. • But must count nodes to reach the node to be removed. • Thus remove is O(N) for both ArrayLists and LinkedLists.

  5. Comparison of efficiency of ArrayList and LinkedList

  6. Additional operations provided by LinkedList • getFirst – O(1) in both ArrayList (as get(1)) and LinkedList • getLast – O(1) in both ArrayList (as get(list.size())) and LinkedList • removeFirst – O(N) in ArrayList (as remove(1)) but O(1) in LinkedList. • removeLast – O(1) in both ArrayList (as remove(list.size())) and LinkedList • addFirst - O(1) in Linked List. Unavailable in ArrayList.

  7. Compare LinkedList’s additional operations with ArrayList

  8. Summary • Compared to ArrayLists: • LinkedList is more efficient for adding (no resizing) • LinkedList is less efficient for getting and setting a specified element. (LinkedList must count nodes). • LinkedList’s additional capabilities • All O(1) • removeFirst is more efficient than O(N) ArrayList • addFirst is unavailable in ArrayList

More Related