Linked Structures, LinkedStack

Slides:



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

Linked Lists Geletaw S..
Stacks, Queues, and Linked Lists
Linked Lists.
Queues and Linked Lists
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Summary of lectures (1 to 11)
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 13 Linked Structures - Stacks. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked implementation.
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 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Grade 12 Computer Studies HG
Reference: Vinu V Das, Principles of Data Structures using C and C++
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 5 Linked Lists II
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Linked Structures Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
UNIT-II Topics to be covered Singly linked list Circular linked list
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Stack: a Linked Implementation
Elementary Data Structures
CHAPTER 4: Linked Structures
Chapter 4 Linked Structures.
Lectures linked lists Chapter 6 of textbook
Storage Strategies: Dynamic Linking
Stacks.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stacks Linked Lists Queues Heaps Hashes
Linked Lists.
Chapter 4 Linked Structures - Stacks
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Example: LinkedSet<T>
Lecture 14 Linked Lists CSE /26/2018.
Presentation transcript:

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

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 Object reference variable An object of Object class obj

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

References as 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<T> does that this way top null LinearNode <T> next; T element; LinearNode <T> next; T element; LinearNode <T> next; T element; Object of type T Object of type T Object of type T

References as Links A self-referential (or recursive) LinearNode<T> object has a pointer to another LinearNode<T> object public class LinearNode<T> { // attributes of the data in an element object private T element // encapsulated element // link (or pointer) to another LinearNode object private LinearNode<T> next; // next item in list

References as Links // constructor public LinearNode(T element) // encapsulate { this.element = element // the element next = null; // set the link to null } // accessor and mutator methods public void setNext(LinearNode<T> next) { this.next = next; public LinearNode<T> getNext() return next;

References as Links public T getElement() { return element; } public void setElement(T element) this.element = element; }// end class LinearNode<T>

Linked Lists Unlike an array which has a fixed size, a linked list is considered to be a dynamic memory 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

Managing Singly Linked Lists The order in which references are changed is crucial to maintaining a linked list Can insert a new LinearNode in two places: At the front In the middle or at the end Can delete a LinearNode in two places:

Inserting Objects in a Linked List Create new LinearNode object and link at top LinearNode<T> newNode = new LinearNode<T>(element); newNode.setNext(top); top = newNode; newNode = null; // may be needed for stale reference // if newNode won’t go out of scope 1 newNode LinearNode<T> next; 2 4 top LinearNode<T> next; LinearNode<T> next; 3

Inserting Objects in a Linked List Create new LinearNode object Link after current LinearNode object (current could be at the end) LinearNode<T> newNode = new LinearNode<T>(element); newNode.setNext(current.getNext()); current.setNext(newNode); newNode = null; 1 newNode LinearNode<T> next; 2 4 current top LinearNode<T> next; LinearNode<T> next; LinearNode<T> next; 3

Removing Objects from a Linked List Remove LinearNode object at front LinearNode<T> removed = top; top = top.getNext()); removed.setNext(null); // remove stale reference 1 removed 3 top LinearNode<T> next; LinearNode<T> next; LinearNode<T> next; 2

Removing Objects from a Linked List Remove LinearNode after current LinearNode object (removed object could be at end) LinearNode<T> removed = current.getNext(); current.setNext(removed.getNext()); removed.setNext(null); // remove stale reference current removed 1 LinearNode<T> next; top LinearNode<T> next; LinearNode<T> next; 3 2

Linked Stack Implementation We can use the LinearNode class to implement a Stack using linking We use the attribute name “top” to have a meaning consistent with a stack count integer top LinearNode next; T element; LinearNode next; T element; null Object of type T Object of type T

Linked Stack Implementation push – O(1) public void push (T element) { LinearNode<T> temp = new LinearNode<T>(element); temp.setNext(top); top = temp; count++; } Note difference between the LinkedStack push method and ArrayStack push method

Linked Stack Implementation pop – O(1) public T pop () throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); T result = top.getElement(); top = top.getNext(); // LinearNode is garbage now count--; return result; } Note difference between the LinkedStack pop method and ArrayStack pop method

LinkedStack Implementation Notice that we don’t need an expandCapacity method in our LinkedStack implementation The “new” operator called in the push method automatically allocates the memory for each LinearNode object when it is needed When the reference to the LinearNode at top is overwritten in the pop method, the JVM garbage collector will release the memory for the now unneeded LinearNode

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

Doubly Linked Lists To add a DoubleNode object to the list, your code must set the DoubleNode next and prev variables in both the new node and its adjacent neighbors To delete a DoubleNode object from the list, your code must bypass the DoubleNode next and prev variables in both neighbors adjacent to the removed node and may need to set its two stale references to null

Doubly Linked List Technique To simplify the code for adding and removing a node to/from the list we can use a dummy node at the front and the back of the list Real data is in nodes linked between them The dummy nodes are ignored in searching the list for data count = 2 (for real data) front back null null Dummy Dummy T T

Traversing Linked Lists We can use “for” or “while” loops to traverse a linked list or a double linked list - examples: for(LinearNode<T> node = front; node != null; node = node.getNext()) { . . . } for(DoubleNode<T> node = back; node != null; node = node.getPrev()) { . . . } LinearNode<T> node = front; // or DoubleNode back while(node != null) { . . . node = node.getNext(); // or Doublenode prev }