1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.

Slides:



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

Linked Lists Geletaw S..
Linked Lists.
Linked Lists Chapter 4.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Linked Lists in Action  This chapter introduces linked lists.  This presentation shows how to implement the most common operations on linked lists. LINKED.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
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
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 5 Linked Lists II
Linked Structures, LinkedStack
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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Linked Lists based on the original work of Dr. Roger deBry Version 1.0
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Memory Management in Java Mr. Gerb Computer Science 4.
UNIT-II Topics to be covered Singly linked list Circular linked list
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.
Linked List, Stacks Queues
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
CHAPTER 4: Linked Structures
Chapter 4 Linked Structures.
Storage Strategies: Dynamic Linking
Doubly Linked List Review - We are writing this code
Algorithm for deleting a node from a singly linked list
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Topic 11 Linked Lists -Joel Spolsky
Chapter 18: Linked Lists.
Chapter 13 Collections.
Example: LinkedSet<T>
Lecture 14 Linked Lists CSE /26/2018.
Data Structures & Algorithms
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Topic 11 Linked Lists -Joel Spolsky
Linked Lists.
Presentation transcript:

1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from Linking Doubly Linked Lists Reading: L&C :

2 References as Links A linked structure is a data structure that uses object reference variables to create links between objects Declaring an object reference variable Object obj = new Object(); A diagram of an object reference variable obj Object reference variableAn object of Object class

3 References as Links A self-referential Person class that contains a pointer to another Person class object public class Person { // attributes of a “person” private String name; private String address; // link (or pointer) to another Person object private Person next; // next Person in list

4 References as Links A self-referential Person class that contains a pointer to another Person class object // constructor, accessor, and mutator methods public Person(...) // parameters are attributes { // set the attributes next = null; } public Person(Person next,...) { this.next = next; } public void setNext(Person next) { this.next = next; } public Person getNext() { return next; }

5 Linked Lists A null terminated linked list of Person objects can be used to represent people waiting in line starting at the “front” Beginning of a linked list of Person objects private Person front; A diagram of a linked list Person next; null front Person next;

6 Linked Lists Unlike an array which has a fixed size, a linked list is considered to be a dynamic structure The amount of memory used grows and shrinks as objects are added to the list or deleted from the list The Java compiler and virtual machine allocate memory for each individual object as it is created (via the new operator) and free memory as each object is garbage collected

7 Linear/Non-Linear Linked Structures A linked list is considered to be a linear structure since it can be visualized as a line Some linked structures are non-linear, e.g. entry

8 Managing Singly Linked Lists The order in which references are changed is crucial to maintaining a linked list Can insert a new Person in three places: –at the front –In the middle –at the end Can delete an existing Person in three places: –At the front –In the middle –at the end

9 Inserting Objects in a Linked List Create new Person object and link at front Person newPerson = new Person(...); newPerson.setNext(front); front = newPerson; newPerson = null; newPerson front Person next;

10 Inserting Objects in a Linked List Create new Person object and link after the current Person object (current could be at end) Person newPerson = new Person(...); newPerson.setNext(current.getNext()); current.setNext(newPerson); newPerson = null; newPerson front Person next; current

11 Inserting Objects in a Linked List How about inserting an element at the end? Let's do it together.

12 Removing Objects from a Linked List Remove Person object at front Person removed = front; front = front.getNext()); removed.setNext(null); removed front Person next; 2 3 1

13 Removing Objects from a Linked List Remove Person object after current Person object (removed object could be at end) Person removed = current.getNext(); current.setNext(removed.getNext()); removed.setNext(null); front Person next; currentremoved

14 Removing Objects from a Linked List How about removing an object from the end?

15 Elements without Links It is desirable to have a generic link class that can be used to link any type of objects of any class encapsulating the “real” data Class LinearNode does that this way Object of type T front LinearNode next; T element; Object of type T LinearNode next; T element; null Object of type T LinearNode next; T element;

16 Elements without Links Code for Linear Node public class LinearNode { private LinearNode next; private T element; public LinearNode() // create an empty node { next = null; element = null; }

17 Elements without Links Code for LinearNode public LinearNode(T element) { next = null; this.element = element; } public LinearNode getNext() { return next; } public void setNext(LinearNode next) { this.next = next; }

18 Elements without Links Code for LinearNode public T getElement() { return element; } public void setElement(T element) { this.element = element; } }// end class LinearNode

19 Elements without Links Using the LinearNode class to create a linked list of three Person objects int size = 3; LinearNode front = new LinearNode (); LinearNode current = front; for(int i = 1; i < 3; i++) { LinearNode newNode = new LinearNode (); current.setNext(newNode); current = newNode; }

20 Doubly Linked Lists Each DoubleNode object has a reference to next DoubleNode and previous DoubleNode public class DoubleNode { private DoubleNode next; private DoubleNode prev; private T element; Object of type T frontDoubleNode next DoubleNode prev T element Object of type T null back DoubleNode next DoubleNode prev T element

21 Doubly Linked Lists To Add a DoubleNode object to the list, your code must set both the DoubleNode next and prev variables. To delete a DoubleNode object from the list, your code must bypass the DoubleNode links in both directions.