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.

Slides:



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

1
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (Parallel Algorithms) Robin Pomplun.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 38.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
Introduction to Computation and Problem
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Photo Slideshow Instructions (delete before presenting or this page will show when slideshow loops) 1.Set PowerPoint to work in Outline. View/Normal click.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Break Time Remaining 10:00.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Linked Lists Chapter 4.
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
ITEC200 Week04 Lists and the Collection Interface.
CHAPTER 19 An Introduction to Data Structures. CHAPTER GOALS To learn how to use linked lists provided in the standard library To be able to use iterators.
Double-Linked Lists and Circular Lists
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
15. Oktober Oktober Oktober 2012.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
The List ADT Textbook Sections
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
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.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Clock will move after 1 minute
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
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.
LinkedList Many slides from Horstmann modified by Dr V.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
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.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Chapter 20 An Introduction to Data Structures
Chapter 15 – An Introduction to Data Structures
Presentation transcript:

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 a linked list is efficient. Visiting the elements of a linked list in sequential order is efficient Random access is not efficient

linked list

Java's LinkedList class Some of the simpler methods: o void addFirst(Object obj) o void addLast(Object obj) o Object getFirst() o Object getSecond() o Object removeFirst() o Object removeLast()

listIterator provides a way to access all links: ListIterator listIterator(int index) listIterator method provides an object of type ListIterator ListIterator is an interface, so the return object from this method provides all the methods in this interface ListIterator Interface: void add(Object o) boolean hasNext(), boolean hasPrev() Object next(), int nextIndex() Object previous(), int prevIndex() void remove(), void set(Object) * provide a way to move forward thru list elements

A ListIterator object

hasNext() method.. hasNext returns true if there is a next element LinkedList list = new LinkedList(); // other calls ListIterator iterator = list.listIterator(0); if (iterator.hasNext()) ………

next() method.. The next method moves the iterator ListIterator iterator = list.listIterator(0); if (iterator.hasNext()) iterator.next(); next throws a NoSuchElementException if you are already past the end of the list The next method returns the object of the link that it is passing while (iterator.hasNext()) { Object obj = iterator.next(); Dog mydog = (Dog)obj; mydog.draw(); }

other iterator methods.. To move the list position backwards, use: ohasPrevious oPrevious The add method: o Adds an object after the iterator o Moves the iterator position past the new element iterator.add("Juliet"); 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 while (iterator.hasNext()) { Object obj = iterator.next(); if (obj fulfills condition) iterator.remove(); }

File ListTest.java 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: }

Implementing Linked Lists LinkedList class has a private inner class Link class LinkedList { private class Link { public Object data; public Link next; }

Implementing Linked Lists LinkedList class oHolds a reference first to the first link oHas a method to get the first element

Implementing Linked Lists class LinkedList { public LinkedList() { first = null; } public Object getFirst() { if (first == null) throw new NoSuchElementException(); return first.data; }... private Link first; }

Adding a New First Element When a new link is added to the list o It becomes the head of the list o The old first link becomes the next link

Adding a New First Element The addFirst method class LinkedList {... public void addFirst(Object obj) { Link newLink = new Link(); newLink.data = obj; newLink.next = first; first = newLink; }... }

Adding a Link to the Head of a Linked List

Removing the First Element When the first element is removed oThe data of the first link are saved and later returned as the method result oThe successor of the first link becomes the first link of the shorter list oThe link will be garbage collected when there are no further references to it

Removing the First Element The removeFirst method class LinkedList {... public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj; }... }

Removing the First Link from a Linked List

LinkedListIterator Private inner class of LinkedList Implements a simplified ListIterator interface Has access to the first field and private Link class

LinkedListIterator The LinkListIterator class class LinkedList {... public ListIterator listIterator() { return new LinkedListIterator(); } private class LinkedListIterator implements ListIterator { public LinkedListIterator() { position = null; previous = null; }... private Link position; private Link previous; }... }

LinkListIterator's next Method position reference is advances to position.next Old position is remembered as previous If the iterator points before the first element of the list, then the old position is null and position must be set to first

LinkListIterator's next Method private class LinkedListIterator implements ListIterator {... public Object next() { if (!hasNext()) throw new NoSuchElementException(); previous = position; // remember for remove if (position == null) position = first; else position = position.next; return position.data; }... }

LinkListIterator's hasnext Method The next method should only be called when the iterator is not at the end of the list The iterator is at the end o if the list is empty (first == null) o if there is no element after the current position (position.next == null)

LinkListIterator's hasnext Method private class LinkedListIterator implements ListIterator {... public boolean hasNext() { if (position == null) return first != null; else return position.next != null; }

LinkListIterator's remove Method If the element to be removed is the first element, call removeFirst Otherwise, the link proceeding the element to be removed needs to have its next reference updated to skip the removed element If the previous reference is null : o this call does not immediately follow a call to next o throw an IllegalArgumentException Set previous reference to null

LinkListIterator's remove Method private class LinkedListIterator implements ListIterator {... public void remove() { if (position == first) { removeFirst(); position = first; } else { if (previous == null) throw new IllegalStateException(); previous.next = position.next; position = previous; } previous = null; }... }

Removing a Link From the Middle of a Linked List

LinkListIterator's set Method Changes the data stored in the previously visited element The set method private class LinkedListIterator implements ListIterator {... public void set(Object obj) { if (position == null) throw new NoSuchElementException(); position.data = obj; }... }

LinkListIterator's add Method Inserts the new link after the current position Sets the successor of the new link to the successor of the current position

LinkListIterator's add Method private class LinkedListIterator implements ListIterator {... public void add(Object obj) { if (position == null) { addFirst(obj); position = first; } else { Link newLink = new Link(); newLink.data = obj; newLink.next = position.next; position.next = newLink; position = newLink; } previous = null; }... }

Adding a Link to the Middle of a Linked List