LinkedList Many slides from Horstmann modified by Dr V.

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

Linked Lists Linear collections.
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++)
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.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
1 Collections Working with More than One Number or Data Type or Object.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
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…?
(c) University of Washington14-1 CSC 143 Java Collections.
The Java Collections Framework Chapters 7.5. Outline Introduction to the Java Collections Framework Iterators Interfaces, Abstract Classes and Classes.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures.
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.
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
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.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Chapter 22 Generic Programming. Chapter Goals To understand the objective of generic programming To be able to implement generic classes and methods To.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
1 CS162: Introduction to Computer Science II Abstract Data Types.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Slides by Donald W. Smith
The List ADT Reading: Textbook Sections 3.1 – 3.5
Marcus Biel, Software Craftsman
Unit 1 Hadoop and big data
Chapter 20 An Introduction to Data Structures
Chapter 16 – Basic Data Structures
Introduction to Data Structures
Linked Lists.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Chapter 15 – An Introduction to Data Structures
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Computer Science and Engineering
Presentation transcript:

LinkedList Many slides from Horstmann modified by Dr V

Linked List A linked list consists of a number of links, each of which has a reference to the next link. 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 a linked list is efficient. Adding and removing elements in the middle of a linked list is efficient. Visiting the elements of a linked list in sequential order is efficient Visiting the elements of a linked list in sequential order is efficient Random access is not efficient Random access is not efficient

Inserting an Element into a Linked List is done by using a ListIterator object for the LinkedList object

Java's LinkedList class Easy access to first and last elements with methods Easy access to first and last elements with methods o void addFirst(Object obj) o void addLast(Object obj) o Object getFirst() o Object getLast() o Object removeFirst() o Object removeLast()

ListIterator ListIterator object gives access to elements inside a LinkedList object ListIterator object gives access to elements inside a LinkedList object ListIterator protects the linked list while giving access ListIterator protects the linked list while giving access ListIterator encapsulates a position anywhere in the linked list ListIterator encapsulates a position anywhere in the linked list

A ListIterator object in a LinkedList List Iterator position

Conceptual View of the ListIterator

List Iterator Think of an iterator as pointing between two links Think of an iterator as pointing between two links

List Iterator The listIterator method of the LinkedList class gets a list iterator The listIterator method of the LinkedList class gets a list iterator LinkedList list = new LinkedList(); LinkedList list = new LinkedList(); ListIterator iterator = list.listIterator(); ListIterator iterator = list.listIterator();

List Iterator The next method moves the iterator iterator.next(); The next method moves the iterator iterator.next(); next throws a NoSuchElementException if next throws a NoSuchElementException if you are already past the end of the list you are already past the end of the list

List Iterator hasNext returns true if there is a next element hasNext returns true if there is a next element if (iterator.hasNext()) iterator.next();

List Iterator The next method returns the object of the link that it is passing The next method returns the object of the link that it is passing while iterator.hasNext() { Object obj = iterator.next(); //do something with the object }

List Iterator To move the list position backwards, use: To move the list position backwards, use: o hasPrevious o previous

Adding and Removing from a LinkedList The add method: The add method: o Adds an object after the iterator o Moves the iterator position past the new element iterator.add("Juliet");

Adding and Removing from a LinkedList The remove method: The remove method: o Removes and o Returns the object that was returned by the last call to next or previous

Adding and Removing from a LinkedList This loop removes all objects that fulfill a certain condition This loop removes all objects that fulfill a certain condition while (iterator.hasNext()) { Object obj = iterator.next(); if (obj fulfills condition) iterator.remove(); iterator.remove(); }

File ListTest.java ListTest is a sample program that ListTest is a sample program that o inserts elements into a list o iterates through the list, adding and removing elements o prints the list

File ListTest.java 01: import java.util.LinkedList; 02: import java.util.ListIterator; 03: 04: /** 05: A program that demonstrates the LinkedList class 06: */ 07: public class ListTest 08: { 09: public static void main(String[] args) 10: { 11: LinkedList staff = new LinkedList(); 12: staff.addLast("Dick"); 13: staff.addLast("Harry"); 14: staff.addLast("Romeo"); 15: staff.addLast("Tom"); 16: 17: // | in the comments indicates the iterator position

18: 19: ListIterator iterator = staff.listIterator(); // |DHRT 20: iterator.next(); // D|HRT 21: iterator.next(); // DH|RT 22: 23: // add more elements after second element 24: 25: iterator.add("Juliet"); // DHJ|RT 26: iterator.add("Nina"); // DHJN|RT 27: 28: iterator.next(); // DHJNR|T 29: 30: // remove last traversed element 31: 32: iterator.remove(); // DHJN|T 33: 34: // print all elements 35: 36: iterator = staff.listIterator(); 37: while (iterator.hasNext())

38: System.out.println(iterator.next()); 39: } 40: }

Abstract Data Types Abstract data type defines the fundamental operations on the data Abstract data type defines the fundamental operations on the data Abstract data type does not specify an implementation Abstract data type does not specify an implementation

Abstract Data Types Abstract list Abstract list o An ordered sequence of items that can be traversed sequentially o Allows for insertion and removal of elements at any position Abstract array Abstract array o An ordered sequence of items o Allows for random access by specifying an integer index

An Abstract View of a Linked List

A Concrete View of a Linked List

An Abstract View of an Array List

A Concrete View of an Array List

Fundamental Operations on Array List public class ArrayList { public Object get(int index) {... } public void set(int index, Object value) {... } }

Fundamental Operations on Linked List public class LinkedList { public ListIteratior listIterator() {... }... } public interface ListIteratior { Object next(); boolean hasNext(); void add(Object value); void remove(); void set(Object value);... }

Efficiency of Linked List Adding or removing an element Adding or removing an element o A fixed number of link references need to be modified to add or remove a link, regardless of the size of the list o Thus, an element can be added or moved in constant time o In big-Oh notations: O(1)

Efficiency of Linked List Random access Random access o On average n/2 elements need to be skipped o In big-Oh notation: O(n)

Efficiency of Array List Adding or moving an element Adding or moving an element o On average n/2 elements need to be moved o In big-Oh notations: O(n) Random access Random access o In big-Oh notation: O(1)

Efficiency of Operations for Arrays and List

Abstract Data Type Stack Allows insertion and removal of elements only at one end Allows insertion and removal of elements only at one end o Traditionally called the top of the stack New items are added to the top of the stack New items are added to the top of the stack Items are removed at the top of the stack Items are removed at the top of the stack Called last in, first out or LIFO order Called last in, first out or LIFO order Think of a stack of books Think of a stack of books

A Stack of Books A stack can be visualized as a stack of books. A stack can be visualized as a stack of books. You place books on top and remove from the top. You place books on top and remove from the top.

A Stack of Books

Abstract Data Type Stack The Stack class is a concrete implementation of a stack in the Java library The Stack class is a concrete implementation of a stack in the Java library The Stack class uses an Object[] to implement a stack The Stack class uses an Object[] to implement a stack…………………………………………… OR you can just use the LinkedList class and only use the addFirst() and removeFirst() methods. OR you can just use the LinkedList class and only use the addFirst() and removeFirst() methods.

Abstract Data Type Stack Sample code to use the Stack class Sample code to use the Stack class Stack s = new Stack(); s.push("A");s.push("B");s.push("C"); //the following loop prints C, B, A int i = s.size(); while (i > 0) {System.out.println(s.pop());i--; }

Abstract Data Type Queue Items added to one end of the queue (the tail) Items added to one end of the queue (the tail) Items removed from the other end (the head) Items removed from the other end (the head) Called first in, first out or FIFO order Called first in, first out or FIFO order Think of a queue of people Think of a queue of people

A Queue A Queue can be visualized as a queue of people. A Queue can be visualized as a queue of people. People join the tail of the queue and wait until they reach the head. People join the tail of the queue and wait until they reach the head.

A Queue

Abstract Data Type Queue No implementing class in the Java library No implementing class in the Java library Can be implemented using a linked list Can be implemented using a linked list

A Queue Implementation public class Queue { /** /** Constructs an empty queue Constructs an empty queue */ */ public Queue() public Queue() { list = new LinkedList(); list = new LinkedList(); } /** /** Adds an item to the tail of the queue Adds an item to the tail of the x the item to x the item to add */ */ public void add(Object x) public void add(Object x) { list.addLast(x); list.addLast(x);

} /** /** Removes an item from the head of the queue Removes an item from the head of the the removed the removed item */ */ public Object remove() public Object remove() { return list.removeFirst(); return list.removeFirst(); } /** /** Gets the number of items in the queue Gets the number of items in the the the size */ */ public int size() public int size() { return list.size() return list.size() } private LinkedList list; private LinkedList list;}