Download presentation
Presentation is loading. Please wait.
Published byHortense Crawford Modified over 9 years ago
1
3/19/2016 5:40 PMLinked list1 Array-based List v.s. Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009
2
Linked List v.s. Array-based list Array-based list Elements are stored in contiguous array positions Support Random Access Requires an estimate of the maximum size of the list the size of a C++ array is fixed waste space Linked list Ensure that the list is not stored contiguously use a linked list a series of structures that are not necessarily adjacent in memory The pointer implementation uses only as much space as is needed for the elements currently on the list Dynamic: a linked list can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. Requires space for the pointers in each cell 3/19/2016 5:40 PMLinked list2
3
3/19/2016 5:40 PMLinked list3 Linked List v.s. Array-based list: Access the kth element Linked List: Need to visit k nodes O(k) Array: Access the element with index k-1 directly O(1) ABCD first Singly Linked List ABCD S[10] S[3] Array
4
3/19/2016 5:40 PMLinked list4 Linked List v.s. Array-based list: Search a key Linked List: Worst case: need to visit n nodes Average: need to visit n/2 nodes O(n) Array: Worst case: need to visit n nodes Average: need to visit n/2 nodes O(n)
5
3/19/2016 5:40 PMLinked list5 Linked List v.s. Array-based list: Insert an element to 1 st position Linked List: Visit one node O(1) Array: requires first pushing the entire array down one spot to make room Visit n node O(n)
6
3/19/2016 5:40 PMLinked list6 Linked List v.s. Array-based list: Delete the 1 st element Linked List: Visit one node O(1) Array: Requires shifting all the elements in the list up one Visit n node O(n)
7
3/19/2016 5:40 PMLinked list7 Linked List v.s. Array-based list: Insert an element to k th position Linked List: In order to find k-1 th element, k-1 nodes will be visited O(k) Array: requires first pushing the elements after k-1 th element down one spot to make room shifting n-(k-1) node Average: n/2 nodes need to be visited.
8
3/19/2016 5:40 PMLinked list8 Linked List v.s. Array-based list: Delete the k st element Linked List: In order to find k-1 th element, k-1 nodes will be visited O(k) Array: Requires shifting all the elements in the list up one shifting n-k node Average: n/2 nodes need to be visited.
9
Linked List v.s. Array-based list The Most Efficient Operations on Linked List are: Inserting at the head position ----- O(1) Removing the head node ----O(1) If the above two operations need to be done more frequently, then linked list is the best. The Most Efficient Operations on Array-based List are: Find the kth element ----O(1) If finding the kth element needs to be done more frequently, then Array-based list is the best. 3/19/2016 5:40 PMLinked list9
10
3/19/2016 5:40 PMLinked list10 Thank you!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.