Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.

Slides:



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

DATA STRUCTURES USING C++ Chapter 5
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Chapter 9: Linked Lists Learn about linked lists. Learn about doubly linked lists. Get used to thinking about more than one possible implementation of.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
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 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Linked Lists Revision Based on: v/
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Dynamic Data Structures and Generics
Linked Lists Chapter 5 (continued)
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists Chapter 5 (continued)
Presentation transcript:

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony Gaddis and Godfrey Muganda Chapter 20 : Linked Lists

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2 Chapter Topics Introduction to Linked Lists Linked List Operations Doubly Linked and Circularly Linked Lists Recursion on Linked Lists

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3 Elementary Facts About Lists A list is a collection of objects organized in a sequence. Each list element is assigned an integer index based on its position in the sequence. The first element in the sequence has index 0, and each succeeding element has index 1 greater than its predecessor.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 Contiguous Allocation Implementation of lists can be array-based, this is known as contiguous memory allocation. In contiguous allocation, list elements occupy consecutive memory locations.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 Contiguous Allocation index 0index 1index 2index 3

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6 Random Access Access to a contiguously allocated list element is very fast: given any index k, we can compute the memory address of the list element at that position by adding the size (in bytes) of k items to the address of the first element in the list. Contiguous allocation is said to allow random access because we can directly jump to any given element without going through its predecessors.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7 Contiguous Allocation Contiguous allocation is used by array-based list implementations such as ArrayList and Vector.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8 Disadvantages of Contiguous Allocation Insertion or deletion of elements in the middle of the list involves the laborious relocation of all elements that come after. For example, if a list has a thousand elements, then to insert a new element at position 5 means all elements from position 5 on up must be moved up. This overhead is bad for applications that do a lot of insertions and deletions.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9 Linked Allocation In linked allocation, the list keeps a reference to its first element: this is the head reference Every element in the list keeps a reference to its successor, the element that follows it on the list. Memory for list elements does not have to be consecutive.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10 Linked Allocation head index 0 index 1 index 2 Index 3

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11 Nodes and Links The objects that hold the list element and the reference (link) to its successor are called nodes. The references to successors are called successor links.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 Disadvantages of Linked Allocation Given a node in the list, we can only access it by starting at the first node and following the successor links till we come to the desired node. This is called sequential access. Sequential access takes a lot longer than random access to get to nodes near the end of a long list.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13 Java Implementation of Linked Lists Requires a Java implementation of a node object. A list is represented using a reference to the node with the first element. An empty list is represented with a reference whose value is null. Each node has a reference to its successor. The successor link of the last node is set to null.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14 Java Implementation of Nodes A class that implements a node must hold a list element plus a successor link: // A node for a list of string elements: class Node { String element; // list element Node next; // successor link Node(String el, Node n) { element = el; next = n; } Node(String el) { element = el; next = null; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15 Creation of Linked Lists

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16 Inserting Nodes into a Linked List To add new elements in the middle of a list: Node b = p.next; // b points to “Bob” Node c = b.next; // c points to “Carol” b.next = new Node(“Brad”, c); This is illustrated in the next slide:

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17 Figure 2 - Inserting Nodes

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18 Removing a Node To remove the first node of a list, set the head reference to its successor: Node myList = ….; // Remove first item myList = myList.next;

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19 Removing a Node To remove a node other than the first: 1. Set a reference to the predecessor of the targeted node. 2. Route the successor link in the predecessor around the targeted node.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20 Remove Carol from “Alan Bob Carol Debby” Set b to predecessor of Carol (Bob) Set d to successor of Carol (Debby) Make d the successor of b: b.next = d;

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 21 Removing a Node

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 22 Traversal of a Linked List A traversal of a linked list is a systematic method of examining, in order, every element of the list. A traversal usually performs some operation as it visits each element.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 23 Traversal of a Linked List To traverse a linked list, start a reference at the head of the list, and keep moving it from a node to a successor: Node ref = myList; while (ref != null) { System.out.println(ref.value); ref = ref.next; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24 Implementation of A Linked List A linked list class needs –A node class Node. –A head reference first to point to the first node: Node first; –A reference last to point to the last node in the list: Node last; The reference last facilitates quick addition of a new node at the end of the list.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25 Implementation of a Linked List Class Class LinkedList1 { private class Node { String value; Node next; Node(String val, Node n) { value = val; next = n; } Node(String val) { // Call the other (sister) constructor. this(val, null); } private Node first; // list head private Node last; // last element in list }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 26 Class Initialization The constructor sets first and last to null: public LinkedList1() { first = null; last = null; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 27 Checking for an Empty List A list is empty when first is equal to null: public boolean isEmpty() { return first == null; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 28 Determining the Size of a List This is done by traversing the list while incrementing a count for each node visited: public int size() { int count = 0; Node p = first; while (p != null) { // There is an element at p count ++; p = p.next; } return count; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 29 Adding to the End of the List To add e, consider two cases: 1.If list is empty: first = new Node(e); last = first; 2. If list is not empty: last.next = new Node(e); last = last.next;

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 30 Adding to the End of the List public void add(String e) { if (isEmpty()) { first = new Node(e); last = first; } else { // Add to end of existing list last.next = new Node(e); last = last.next; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 31 Adding an Element at a Given Index Check index: if out of bounds, throw an exception. Is index 0? Put new element at beginning of the list, set last and return. Is index > 0? Skip index nodes from beginning of the list and splice in the new node. If the new node was added at the end, set last to the new node.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 32 public void add(int index, String e) { if (index size()) throw new IndexOutOfBoundsException(); // Index is at least 0 if (index == 0) { // New element goes at beginning first = new Node(e, first); if (last == null) last = first; return; } // Set pred to node that will be the predecessor of new node Node pred = first; for (int k = 1; k <= index - 1; k++) pred = pred.next; // Splice in a node containing the new element pred.next = new Node(e, pred.next); // Is there a new last element ? if (pred.next.next == null) last = pred.next; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 33 Removing a List Element Based on Value Use a list traversal to locate the node containing the given value, return false if there is no such node. If the target node is the first node, remove it by moving first forward and resetting last if necessary, and then return true. Otherwise, find the predecessor of the target node and remove the target node. If the target node was the last node in the list, reset last.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 34 public String remove(int index) { if (index = size()) throw new IndexOutOfBoundsException(); String element; // The element to return if (index == 0) { // Removal of first item in the list element = first.element; first = first.next; if (first == null) last = null; return element; } // To remove an element other than the first // find the predecessor of the element to be removed. Node pred = first;0 // Move pred forward index - 1 times for (int k = 1; k <= index -1; k++) pred = pred.next; // Store the element to return element = pred.next.element; // Route link around the node to be removed pred.next = pred.next.next; // Check if pred is now last if (pred.next == null) last = pred; return element; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 35 Doubly Linked Lists

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 36 Doubly Linked Lists Singly-linked lists allow one way traversal: one can move from a node to its successor. Limitation: one cannot easily move from a node to its predecessor. Doubly-linked lists allow easy transitions in both directions.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 37 Nodes for Doubly Linked Lists A node for a doubly linked list must have both a successor link and a predecessor link:

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 38 Doubly-Linked Lists Can be represented by a head (first) reference and a reference to the last node (for efficiency)

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 39 Node Class for Doubly-Linked Lists class Node { String element; // Element Node next; // Reference to successor Node prev; // Reference to predecessor Node(String val, Node n, Node p) { element = val; next = n; prev = p; } Node(String val) { // Call the other constructor this(val, null, null); }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 40 Operations on Doubly-Linked Lists Implementations of size(), isEmpty() are similar to those of singly-linked lists Addition and removals are also similar, but require care to manipulate the two links for successor and predecessors.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 41 Addition at beginning of a doubly linked List To add a new element e to an empty list: first = new Node(e, null, null); last = first; To add e to beginning of a nonempty doubly linked list: first = new Node(e, first, null); first.next.prev = first;

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 42 Addition After the First Element Set pred to what will be predecessor of the new node. Set succ to what will be the successor of the new node. Put the new node in the middle (between) pred and succ.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 43 public void add(int index, String e) { if (index size()) throw new IndexOutOfBoundsException(); // Index is at least 0 if (index == 0) { // New element goes at beginning Node p = first; // Old first first = new Node(e, p, null); if (p != null) p.prev = first; if (last == null) last = first; return; } // pred will point to the predecessor of the new node. Node pred = first; for (int k = 1; k <= index - 1; k++) pred = pred.next; // Splice in the new node: pred-- succ becomes pred--middle--succ Node succ = pred.next; Node middle = new Node(e, succ, pred); pred.next = middle; if (succ == null) last = middle; else succ.prev = middle; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 44 Recursion on Linked Lists

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 45 Recursion on Singly-Linked Lists Recursion is natural on lists because a list is a recursive data structure: –The empty collection is a list. –A non empty list consists of a head (the first element) and a tail. –The tail of a list is also a list.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 46 Recursive Implementation of size A private int size(Node list) method for computing the size of a list: The size of an empty list is 0. The size of a non empty list is one greater than the size of the tail: size(list.next) + 1.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 47 Recursive implementation of size int size(Node list) { if (list == null) return 0; else return size(list.next) + 1; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 48 Recursive add to End of List The Node add(String e, Node list) method adds e to the end of list and returns a reference to the first node of the resulting list: –If list is null, return new Node(e); –If list is not empty, add e to the end of the tail, attach the original head to the beginning of the modified tail, and return a reference to the original head.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 49 Add e to the End of a List Node add(String e, Node list) { if (list == null) return new Node(e); else { // Replace the tail with result of adding e to the end // of the tail list.next = add(e, list.next); return list; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 50 Adding an Element at an Index In a method Node add(int index, String e, Node list) the value of index must be nonnegative and less or equal to the size of the list. The base case is when index is 0: place e at the beginning of the list. If index != 0, the list cannot be empty. Place e at position index -1 in the tail of the original list.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 51 Recursive add at an index private Node add(int index, String e, Node list) { if (index size()) { String message = String.valueOf(index); throw new IndexOutOfBoundsException(message); } if (index == 0) return new Node(e, list); // 0 < index and index <= size so list is not empty // Replace the tail with result of adding e at index - 1 // in the tail list.next = add(index-1, e, list.next); return list; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 52 Removing a Value From a List A method for removing needs to return a pair of results: class RemovalResult { Node node; // Node containing removed element Node list; // List remaining after removal of node }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 53 RemovalResult remove(int index, Node list) { if (index = size()) throw new IndexOutOfBoundsException(); if (index == 0) { // Remove the first node on list RemovalResult remRes = new RemovalResult(list, list.next); list.next = null; return remRes; } // Recursively remove the element at index-1 in the tail RemovalResult remRes = remove(index-1, list.next); // Replace the tail with the results and return // after modifying the list part of RemovalResult list.next = remRes.list; remRes.list = list; return remRes; }

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 54 The public Interface to Recursive Methods In general, recursive methods need to access parameters that refer to private implementation details, and must themselves be private. Such recursive methods must be called through a public interface that calls a corresponding recursive method.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 55 A Public Interface to size() public int size() { // Call the private recursive size(Node p) method return size(first); } Here Node first; is a private reference to the head element of the list.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 56 A Public Interface to remove(int) This method passes an index to the private method RemovalResult remove(int index, Node List) The public method extracts its return value from the returned RemovalResult object.

Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 57 Public remove(int) public String remove(int index) { // Pass the job on to the recursive version RemovalResult remRes = remove(index, first); String el = remRes.node.value; // Element to return first = remRes.list; // Remaining list return el; }