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

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Linked Lists Geletaw S..
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Chapter 7. Binary Search Trees
Singly linked lists Doubly linked lists
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
Queues and Linked Lists
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linear collections.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
John Hurley Cal State LA
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-5
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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.
Lists Lists as an abstract data type (ADT)
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Chapter 5 Linked Lists II
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Java linked list.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Linked Lists and Generics Written by J.J. Shepherd.
CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Data Structure By Amee Trivedi.
Linked Lists Chapter 5 (continued)
Priority Queue.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Collections Framework
Header and Trailer Sentinels
Linked Lists Chapter 5 (continued)
Data Structures & Algorithms
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists Chapter 5 (continued)
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

 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

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; }