Download presentation

Presentation is loading. Please wait.

Published byImani Bares Modified over 3 years ago

2
David Weinberg presents

3
Linked Lists: The Background Linked Lists are similar to ArrayLists in their appearance and method of manipulation They do NOT NOT NOT NOT NOT have indices, if it does, you’re a noob. (Yes, you) They are composed of objects called nodes that are “linked” together through references to other nodes

4
Linked List: The Theory Data This is a Node. This represents the data a node holds Data 2 Each node has references to other nodes which are considered its “neighbors” Data 3Data 4 This is known as the Head of the Linked List This is known as the tail of the Linked List Think of this as an array without indexes… Each Node knows only its left and right neighbors but not all of the elements in the array

5
Q1Q2Q3Q4Q5 Linked List: An Example Q1Q2Q3Q4Q5 Suppose you were managing a test…One approach would be an array, lets discuss this: Arrays do not have dynamic length, but there is a bigger problem. The amount of operations required to remove a question or add a question or swap a question can be quite large (for very large arrays) Our Example array: Now suppose you wanted to remove Q2First you must access the index, which is O(1) efficiency Now you must remove the information at this index, which is O(1) After this you must shift all of the indices to the left, such as making Q3 at index 1, rather than index 2. Q3Q4Q5 Therefore, the efficiency of the remove operation for an array is O(n). Now suppose you used a Linked List: Now to remove a node, we first access this node, which is O(1)- assuming we have found it already (Best case)-. Our Example Linked List: Now we must remove the Node from the list by setting the reference from Q1 to Q2 as Q1 to Q3. Efficiency: O(1) The Node is now removed from the list and is returned or deleted, depending on the users preference. The total efficiency for this Linked List is O(1) which is must faster than an array, O(n).

6
Q1Q2Q3Q4Q5 Q1Q2Q3Q4Q5 Linked List: The Types TThere are 3 types of Linked Lists The first has already been introduced, Linear or Singular Linked Lists: Q1Q2Q3Q4Q5 Characteristics: Each Node points to its “right” neighbor only The Tail is considered NULL, which signifies the end You can only traverse rightwards starting at the Head The second is doubly Linked Lists: Characteristics: Each Node points to its left and right neighbor A Tail is commonly used and is the last node You only traverse both forwards and backwards starting at either end The final type is circularly Linked Lists: Characteristics: Circularly Linked Lists are either Linear or Doubly Linked Lists Their tail points to their head, and if its doubly linked, their head points to their tail. All circularly linked lists have both a head and a tail You can iterate through the Linked List both forwards, backwards and also through it multiple times with only using the getLeft() and getRight() methods

7
Linked List: The Efficiency Earlier, the remove method for the Linked List was discussed, here is a table of Linked List efficiency (Remember, its O(n) for all cases in an array type data structure): Add or Remove Operation for the Linked List TailType Location of Insertion Worst Case Efficiency NoAny FrontO(1) RandomO(n) EndO(n) YesAny FrontO(1) RandomO(n) EndO(1) Other Operations of LL vs. Arrays OperationArray EfficiencyLL EfficiencyComments SearchO(n)- Linear Each index or node might have to be searched Delete All Occurrences of An Object O(n) Depending on implementation, Array can equal basic LL Efficiency

8
Linked List: Implementation Implementing a Node: public class ListNode { public Object value; //Get+Sets needed if private public ListNode next;//Get+Sets needed if private //public ListNode prev; //If doubly linked. /*Constructors*/ } Value Next -This has its own value and Next node- ListNode visualization

9
Linked List: Implementation Implementing a Linear Linked List: public class LinearLinkedList: private ListNode head; public boolean isEmpty() {){/*Implemented Later*/)}; public void addFirst(Object info){/*Implemented Later*/}; public void addLast(Object info){/*Implemented Later*/}; public void removeFirst(){/*Implemented Later*/}; public void removeLast(){/*Implemented Later*/}; public void remove(Object info) {/*Implemented Later*/}; public void toString() {/*Implemented Later*/}; Q1Q2Q3Q4Q5

10
Linked List: isEmpty(); Linear: if(head == null) return true; return false; Doubly: if(head == null) return true; return false; Circular Doubly Linked List: if(head == null) return true; return false; Just checks to see if there is a starting node

11
Doubly if(isEmpty()) head = new ListNode(info, null, null); else { ListNode current = head; head = new ListNode(info, head, null); current.setPrev(head); } Circular Doubly if(isEmpty()) head = new ListNode(info, null, null); else { ListNode current = head; head = new ListNode(info, head, tail); current.setPrev(head); tail.setNext(head); } Linear if(isEmpty()) head = new ListNode(info, null); else head = new ListNode(info, head); Linked List: addFirst(Object info); Q1Q2Q3Q4Q5InfoQ1Q2Q3Q4Q5 Q1Q2Q3Q4Q5

12
Linked List: Parsing To manipulate a Linked List, you parse through it using the pointers with recursion: /*Start the method at the head for the first count if you want the LL’s length*/ public int count(ListNode current) { if(current != null) int total = 1; if(current.getRight() != null) total += count(current.getRight()); /* Used if its doubly Linked!*/ if(current.getLeft() != null) total += count(current.getLeft()); return total; }

Similar presentations

OK

1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.

1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google