# David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.

## Presentation on theme: "David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do."— Presentation transcript:

David Weinberg presents

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

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

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

Q1Q2Q3Q4Q5 Q1Q2Q3Q4Q5 Linked List: The Types TThere 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

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

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

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

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