Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.

Slides:



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

DATA STRUCTURES USING C++ Chapter 5
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.
Chapter 9: Data Structures I
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
Chapter 12: Data Structures
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
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.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Linked Structures, LinkedStack
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
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,
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
CHAPTER 4: Linked Structures
Chapter 4 Linked Structures.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 12: Data Structures
Summary Array List.
Presentation transcript:

Chapter 4 Linked Structures

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create linked structures Compare linked structures to array-based structures Explore the techniques for managing a linked list Discuss the need for a separate node to form linked structures Implement a set collection using a linked list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-3 References as Links There are many ways to implement a collection In chapter 3 we explored an array-based implementation of a set collection A linked structure uses object reference variables to link one object to another Recall that an object reference variable stores the address of an object In that sense, an object reference is a pointer to an object

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-4 FIGURE 4.1 An object reference variable pointing to an object

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-5 Self-Referential Objects A Person object, for instance, could contain a reference variable to another Person object: public class Person { private String name; private String address; private Person next; // a link to another Person object // whatever else }

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-6 Linked Lists This type of reference can be used to form a linked list, in which one object refers to the next, which refers to the next, etc. Each object in a list is often generically called a node A linked list is a dynamic data structure in that its size grows and shrinks as needed, unlike an array, whose size is fixed Java objects are created dynamically when they are instantiated

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-7 FIGURE 4.2 A linked list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-8 Non-linear Structures A linked list, as the name implies, is a linear structure Object references also allow us to create non-linear structures such as hierarchies and graphs

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-9 FIGURE 4.3 A complex linked structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Managing Linked Lists The references in a linked list must be carefully managed to maintain the integrity of the structure Special care must be taken to ensure that the entry point into the list is maintained properly The order in which certain steps are taken is important Consider inserting and deleting nodes in various positions within the list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 4.4 Inserting a node at the front of a linked list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 4.5 Inserting a node in the middle of a linked list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 4.6 Deleting the first node in a linked list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 4.7 Deleting an interior node from a linked list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Elements without Links The problem with self-referential objects is that they "know" they are part of a list A better approach is to manage a separate list of nodes that also reference the objects stored in the list The list is still managed using the same techniques The objects stored in the list need no special implementation to be part of the list A generic list collection can be used to store any kind of object

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 4.8 Using separate node objects to store and link elements

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Doubly Linked Lists There are variations on the implementation of linked lists that may be useful in particular situations For example, in a doubly linked list each node has a reference to both the next and previous nodes in the list This makes traversing the list easier

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 4.9 A doubly linked list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 4.1

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 4.1 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 4.1 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 4.10 A linked implementation of a set collection

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedSet - Constructor

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedSet - the add Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedSet - the removeRandom Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedSet - the removeRandom Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedSet - the remove Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedSet - the remove Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedSet - the remove Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedSet - the iterator Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 4.2

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 4.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 4.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 4.11 UML description of the LinkedSet class

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Analysis of Linked Operations Since the order is irrelevant, and there is no capacity to expand, adding an element to the set is O(1) Removing a particular element, because it must be found, is O(n) Removing a random element requires a traversal of the list, and therefore is O(n)