Chapter 4 Linked Structures.

Slides:



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

DATA STRUCTURES USING C++ Chapter 5
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.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
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.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Lecture 6 of Computer Science II
Unit – I Lists.
CHAPTER 4: Linked Structures
The List ADT.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Pointers and Linked Lists
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Program based on queue & their operations for an application
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Storage Strategies: Dynamic Linking
Chapter 12: Data Structures
Chapter 4 Linked Lists.
Creating Objects & String Class
Chapter 12 Collections.
LINKED LISTS CSCD Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Chapter 15 Lists Objectives
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 7 Functions of Random Variables (Optional)
Chapter 14 Graphs and Paths.
Arrays and Linked Lists
Chapter 18: Linked Lists.
Chapter 13 Collections.
Linked Lists.
Introduction to Data Structures
Chapter 10 Datapath Subsystems.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Dynamic Data Structures and Generics
Summary Array List.
Chapter 20 Hash Tables.
A List Implementation That Links Data
Chapter 17: Linked Lists.
Chapter 12 Collections.
Lecture 14 Linked Lists CSE /26/2018.
The Facts to Be Explained
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Circuit Characterization and Performance Estimation
Chapter 6 Dynamic Programming.
Classes and Objects Object Creation
Chapter 2 Reference Types.
A List Implementation That Links Data
Topic 11 Linked Lists -Joel Spolsky
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Chapter 4 Linked Structures

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.

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.

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

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.

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.

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

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.

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<T> class Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

Analysis of Linked Operations Like the ArraySet, we must check first to see if the given element is in the set, thus adding an element to the set is O(n) 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) Copyright © 2005 Pearson Addison-Wesley. All rights reserved.