Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.

Slides:



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

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 7 Iterators Modified. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Chapter 12: Data Structures
Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists.
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.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
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.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 12 Introduction to Collections - Stacks
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 3 Introduction to Collections – Stacks Modified
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Linked Structures, LinkedStack
COS 312 DAY 19 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 Layers using non-opaque panels.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
COS 312 DAY 18 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 5 Corrected – 1 A, 2 B’s, 1 C, 1 D 1 F and 1 MIA –
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.
Collections & Stacks Collections A collection is an object that holds and organizes other objects It provides operations for accessing and managing.
COS 312 DAY 20 Tony Gauvin. Ch 1 -2 Agenda Questions? Next Capstone progress report is due April 25 Assignment 5 Graded – Not much original code Assignment.
Section 3.7 Linked-Based Implementations
Stack: a Linked Implementation
Elementary Data Structures
CHAPTER 4: Linked Structures
The List ADT.
Chapter 4 Linked Structures.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Storage Strategies: Dynamic Linking
Chapter 12: Data Structures
Stacks.
Chapter 14: Stacks.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stacks and Queues.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stack and Queue APURBO DATTA.
Top Ten Words that Almost Rhyme with “Peas”
Chapter 4 Linked Structures - Stacks
Java Software Structures: John Lewis & Joseph Chase
Stacks, Queues, and Deques
Chapter 4 Linked Structures - Stacks
Chapter 13 Collections.
Stacks, Queues, and Deques
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
CHAPTER 11: Priority Queues and Heaps
Stacks, Queues, and Deques
CHAPTER 3: Collections—Stacks
Presentation transcript:

Linked Structures - Stacks

Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references to create links between objects Recall that an object reference variable holds the address of an object

Linked Structures A Person object, for instance, could contain a reference to another Person object A series of Person objects would make up a linked list: 4 - 3Java Software Structures, 4th Edition, Lewis/Chase

Linked Structures Links could also be used to form more complicated, non-linear structures

Linked Lists There are no index values built into linked lists To access each node in the list you must follow the references from one node to the next Person current = first; while (current != null) { System.out.println(current); current = current.next; } 4 - 5

Linked Lists Care must be taken to maintain the integrity of the links To insert a node at the front of the list, first point the new node to the front node, then reassign the front reference 4 - 6

Linked Lists To delete the first node, reassign the front reference accordingly If the deleted node is needed elsewhere, a reference to it must be established before reassigning the front pointer

Linked Lists So far we've assumed that the list contains nodes that are self- referential ( Person points to a Person ) But often we'll want to make lists of objects that don't contain such references Solution: have a separate Node class that forms the list and holds a reference to the objects being stored

Linked Lists There are many variations on the basic linked list concept For example, we could create a doubly-linked list with next and previous references in each node and a separate pointer to the rear of the list

Stacks Revisited In the previous chapter we developed our own array- based version of a stack, and we also used the java.util.Stack class from the Java API The API's stack class is derived from Vector, which has many non-stack abilities It is, therefore, not the best example of inheritance, because a stack is not a vector It's up to the user to use a Stack object only as intended

Stacks Revisited Stack characteristics can also be found by using the Deque interface from the API The LinkedList class implements the Deque interface Deque stands for double-ended queue, and will be explored further later For now, we will use the stack characteristics of a Deque to solve the problem of traversing a maze

Implementing a Stack using Links Let's now implement our own version of a stack that uses a linked list to hold the elements Our LinkedStack class stores a generic type T and implements the same StackADT interface used previously A separate LinearNode class forms the list and hold a reference to the element stored An integer count will store how many elements are currently in the stack

Implementing a Stack using Links Since all activity on a stack happens on one end, a single reference to the front of the list will represent the top of the stack top

Implementing a Stack using Links The stack after A, B, C, and D are pushed, in that order: Java Software Structures, 4th Edition, Lewis/Chase “D” “C” “B” “A” top count 4 4

Implementing a Stack using Links After E is pushed onto the stack: Java Software Structures, 4th Edition, Lewis/Chase “D” “C” “B” “A” top count 5 5 “E”

/** * Represents a node in a linked list. Lewis and Chase */ public class LinearNode { private LinearNode next; private T element; /** * Creates an empty node. */ public LinearNode() { next = null; element = null; } /** * Creates a node storing the specified element. elem element to be stored */ public LinearNode(T elem) { next = null; element = elem; }

/** * Returns the node that follows this one. reference to next node */ public LinearNode getNext() { return next; } /** * Sets the node that follows this one. node node to follow this one */ public void setNext(LinearNode node) { next = node; } /** * Returns the element stored in this node. element stored at the node */ public T getElement() { return element; } /** * Sets the element stored in this node. elem element to be stored at this node */ public void setElement(T elem) { element = elem; }

/** * Represents a linked implementation of a stack. */ public class LinkedStack implements StackADT { private int count; private LinearNode top; /** * Creates an empty stack. */ public LinkedStack() { count = 0; top = null; } top count 0 0

/** * Adds the specified element to the top of this stack. element element to be pushed on stack */ public void push(T element) { LinearNode temp = new LinearNode (element); temp.setNext(top); top = temp; count++; } “C” “B” “A” top count 3 3 “D” temp 4 4

/** * Removes the element at the top of this stack and returns a * reference to it. element from top of stack EmptyCollectionException if the stack is empty */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack"); T result = top.getElement(); top = top.getNext(); count--; return result; } “C” “B” “A” top count result