Linked Lists.

Slides:



Advertisements
Similar presentations
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
Advertisements

Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Concrete collections in Java library
Double-Linked Lists and Circular Lists
The List ADT Textbook Sections
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
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++)
Data Structures: A Pseudocode Approach with C
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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 and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Linked Lists. RHS – SOC 2 Linked lists We can already store collec- tions of objects in arrays and array lists – why would we need other data structures…?
111 © 2002, Cisco Systems, Inc. All rights reserved.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
LinkedList Many slides from Horstmann modified by Dr V.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: From the Ground Up Chapter 17 The Java Collections Framework.
Chapter 15 An Introduction to Data Structures. Chapter Goals To learn how to use the linked lists provided in the standard library To be able to use iterators.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Slides by Donald W. Smith
Prefix notation in action
Iterators.
Week 4 - Monday CS221.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Marcus Biel, Software Craftsman
4. Linked Lists.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 20 An Introduction to Data Structures
ECET 370 ASSIST Something Great/ecet370assist.com
Announcements & Review
Linked Lists Chapter 6.5, 17 CSCI 3333 Data Structures.
Chapter 17 Object-Oriented Data Structures
Introduction to Data Structures
Arrays and Linked Lists
List Data Structure.
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Building Java Programs
Chapter 15 – An Introduction to Data Structures
Welcome to CSE 143!.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Dynamic Data Structures and Generics
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Computer Science and Engineering
Collections Framework
Dynamic Data Structures and Generics
Building Java Programs
Iterators Dan Fleck.
TCSS 143, Autumn 2004 Lecture Notes
Introduction to Java Collection
Presentation transcript:

Linked Lists

Topics Understanding Linked Lists Pros and cons of Linked Lists. Using the Java LinkedList Class. Implementing a LinkedList Class. Adding Linked Nodes Deleting Linked Nodes

Understanding Linked Lists What is a linked list and how is it similar to an array or an ArrayList? A Linked List is an ordered collection of elements. An array and an ArrayList are also ordered collection of elements.   How is a Linked List different from an array or an ArrayList? The difference between a Linked List and an array or ArrayList is efficiency. A linked list allows efficient addition and removal of elements in the middle of a sequence.

In-depth look at Efficiency of Ordered Elements To understand the inefficiency of an array of ArrayList, imagine a program that maintains a sequence of users. If a user wants to be removed from the list, a gap is created when an element is deleted. Once a gap appears, it needs to be closed up. Conversely, when a user needs to be added in the middle of an ordered sequence, all the users following the new user must be moved to the end of the ordered list.

Gaps created when Deleting and Adding Elements in an Array or ArrayList Moving a large number of elements in an Array or ArrayList can involve a substantial amount of processing time.

What is a Linked List? A linked list uses a sequence of nodes to create an ordered list. A node is an object that stores an element and references to the neighboring nodes in the sequence. Example : c2 through c8 are nodes in a Linked List

Benefit of a Linked List Linked Lists allow speedy insertion and removal. Before: After:

Cons of a Linked List Accessing elements in a Linked List can be slow because it is sequential. Example: Accessing c5 requires that we visit c2, c3, then c5. Or, in reverse we visit c8, c7, and then c5.

Using the Java LinkedList Class Creating a basic Linked List can be done by using the LinkedList class provided by the Java Library. This class is part of the java.util package. Creating a Linked List requires a class. Example: LinkedList<Integer> list = new LinkedList<Integer>();

LinkedList Class Methods addLast() addFirst() getFirst() getLast() removeFirst()

LinkedList Class Inherited Methods The LinkedList class also inherits the methods of the collection interface. size() add() toString() contains()

Traversing a LinkedList You may use the for each loop structure with any collection. Example for (Integer c:list) System.out.println(c);

Iterators for Visiting an element in a LinkedList Provide an iterator for visiting list elements. Example ListIterator<Integer> itr = list.listIterator(); ____________________________________________________ You can use methods of the Iterator and ListIterator Interfaces next() previous() hasNext() hasPrevious() set() add() remove()

Tips for LinkedLists Draw before and after diagrams showing relevant references. Use before and after diagrams to show elements that need to be altered. Be sure that no links are left undefined at the conclusion of your code. Dangling nodes will become garbage and collected by the garbage collector. Verify that your algorithm works correctly for an empty list and for a list with only one node.

Practice – Create the Linked List Below Additional Tasks: Create an iterator and position it after the third number in the list. Insert the number 4 at this position. Remove the number 5 from the list. You will need to reposition the iterator. Display the new contents of the linked list.

Implementing a LinkedList Class Create your own LinkedList Class. LinkedList will rely on a Node Class containing a generic object data. This first LinkedList will be a single linked list. A pointer will be used to point to the next node. No pointers will be used to point to a previous node. Member methods will include addNode() and deleteNode()