Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
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.
Linked Lists Chapter 4.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Double-Linked Lists and Circular Lists
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.
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++)
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
12-Jul-15 Lists in Java Part of the Collections Framework.
Iterators CS 367 – Introduction to Data Structures.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
LinkedList Many slides from Horstmann modified by Dr V.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
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.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
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.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
List Interface and Linked List Mrs. Furman March 25, 2010.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Iteration Abstraction SWE Software Construction Fall 2009.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
CS 46B: Introduction to Data Structures July 23 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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
Linked Lists CS 367 – Introduction to Data Structures.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked lists.
LinkedList Class.
8-1.
8-1.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Data Structures ADT List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Computer Science and Engineering
Data Structures ADT List
CSC 143 Java Linked Lists.
Linked Lists.
Iterators Dan Fleck.
Linked lists.
Linked Lists Chapter 5 (continued)
Presentation transcript:

Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College

Linked List Elements Each node contains two parts: –The current element (of class E) –Reference to the next element (another Node) private static class node { private E data; private node next; }

Ending the list The last node in a list still has a next pointer The next pointer of the last node in the list will be null –Null is never a valid reference (causes exception) Graphical representations for null:

Inserting into a Linked List /* Assume aNode is a reference to the node that contains “Barry”*/ Node newNode = new Node (“Carol”); newNode.next = aNode.next; aNode.next = newNode;

Deleting from a Linked List Just the reverse of adding to the list! Make sure you start with the item before the one to be deleted. //Again, assume aNode references “Barry” toDelete = aNode.next; //reference to “Carol” aNode.next = toDelete.next;

Traversing a Linked List //print elements, starting at the node called head; // Uses a for loop for(Node current=head; current!=null; current=current.next){ System.out.println(current.data); }

Another Traversal //prints Ann==>Barry==>Carol==>Ellen Node current = head; while(current != null){ if (current.next == null) System.out.println(current.data); else System.out.print(current.data + “==>”); current = current.next; }

From a Node to a List… We could simply represent the list by its first node but, how do you represent an empty list? –Empty list has no nodes Solution is to use a head reference –The list object contains a reference to the first node in the list –If this is null, the list is empty

Partial LinkedList Class public class LinkedList { private Node head; //first element or null private int size; //list length … private static class Node { //inner class private E data; private Node next; } //methods go here }

Constructor //Construct an empty list public SingleLinkedList () { head = null; size=0; }

Adding a New First Item //a node for item is added at the beginning public void addFirst(E item){ newNode = Node (item); newNode.next = head; // head is a reference to the first node, so // our new node is now the head. head = newNode; }

Copying Every Node //old is a non-empty list to copy head = new Node(); //make a new head ptr; head.data = old.head.data; Node cur = head; //current position in new list for(Node ptr=old.head.next;ptr !=null; ptr=ptr.next){ cur.next = new Node(); cur = cur.next; cur.data = ptr.data; } cur.next = null;

Is the list empty? //Is it empty? boolean isEmpty(){ return (head == null); }

Implementing get //get - retrieve node at the given index //This is not efficient - worst case O(N) Public E LinkedList.get(int index){ if((index >= size) || (index<0)) throw(IndexOutofRangeException); Node cur = head; for(int j=0;j<index;j++){ cur = cur.next; } return cur.data; }

Accessing Elements of a LinkedList How do we access elements within a list? –Item after which to add –Item after which to delete –Location of item that was found If we return the Node, we break the abstraction –Only LinkedLists have Nodes (cannot be part of the List interface) –If Nodes were accessible, clients could “break” the list by modifying them.

Iterators An Iterator is an object that keeps track of a location in a list Methods include: –Boolean hasNext() - is this the end of the list? –E next() - returns an element and advances the pointer to the next element. –Remove() - removes the last element accessed by next().

Advantage of Iterator Keeps track of the current place in the list (don’t need to traverse all the elements before it to get back) –Printing all elements is back to O(N) if you use an iterator Hides the underlying implementation from the user –Iterators can be implemented for ArrayLists, too.

Iterator Traversal Example //prints the list, one item per line //returns iterator pointing before the list… Iterator itr = aList.iterator(); while(itr.hasNext()){ String name = itr.next(); System.out.println(name); }

More Methods for ListIterator All of the Iterator methods plus… add () –insert item at current location hasPrevious() and Previous() –Allows reverse movement (doubly-linked list) nextIndex() –returns index of next item or size if at end Set (E obj) –Sets value of current item (must be preceded by a call to next)

Getting an Iterator from a Linked List listIterator() - returns iterator before beginning of list listIterator(int index) - returns iterator before index

Enhanced for (for each) Original for(int x=0;x<max;x++) { do something with x } Iterator based ( intList is List ) for (int x : intList){ do something with x }