Stacks Cmput 115 - Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.

Slides:



Advertisements
Similar presentations
Linked Lists Linear collections.
Advertisements

AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Binary Search Trees Cmput Lecture 23 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Stacks, Queues, and Deques
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Container Traversal Cmput Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
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.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
Vectors Cmput Lecture 4 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from.
Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.
TCSS 342, Winter 2005 Lecture Notes
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Dictionaries and Hash Tables Cmput Lecture 24 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
The Daikon system for dynamic detection of likely invariants MIT Computer Science and Artificial Intelligence Lab. 16 January 2007 Presented by Chervet.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
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.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Section 3.7 Linked-Based Implementations
Stack: a Linked Implementation
CHAPTER 4: Linked Structures
Efficiency of in Binary Trees
Data Structures and Algorithms
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Trees Tree nomenclature Implementation strategies Traversals
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
Stacks.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
CSC 1052 Stacks 1/11/2019.
Data Structures and Algorithms
Stacks Abstract Data Types (ADTs) Stacks
Stacks.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 2/24/00

©Duane Szafron About This Lecture In this lecture we will learn about a group of data structures called linear structures and specifically one representative from the group called a Stack.

©Duane Szafron Outline Linear Structure Interface Stack Interface Maze - An example of using a Stack Vector Implementation of Stack List Implementation of Stack

©Duane Szafron A Linear Structure A Linear Structure is a non-indexed container object (in fact a Store) that can grow and shrink one element at a time. However, it is not specified whether the container shrinks at the same end that it grows. "Fred""Barney""Wilma" "Fred""Barney""Wilma"”Betty""Fred""Barney""Wilma" ”Barney"”Wilma"”Betty"

©Duane Szafron Linear Container Hierarchy In the structure package the following interface hierarchy appears. Store Linear StackQueue In the java.util package Stack is a sub- class of Vector, not an interface

©Duane Szafron Structure Interface - Store public interface Store { public int size (); //post: returns the number of elements contained in // the store. public boolean isEmpty (); // post: returns the true iff store is empty. public void clear (); // post: clears the store so that it contains no elements. } code based on Bailey pg. 18

©Duane Szafron Structure Interface - Linear public interface Linear extends Store { public void add (Object anObject); // pre: anObject is non-null // post: the object is added to the container. The // consistent replacement policy is not specified public Object peek (); // pre: container is not empty /* post: returns the next object to be removed, but does in not remove it */ public Object remove (); // pre: container is not empty // post: removes an object from the container and returns it } code based on Bailey pg. 127

©Duane Szafron Structure Interface - Stack 1 public interface Stack extends Linear { public void add (Object anObject); // post: the object that is added will be popped next if // nothing else is added. Same operation as push public void push (Object anObject); // post: the object that is added will be popped next if // nothing else is pushed. Same operation as add public Object remove (); // pre: stack is not empty // post: removes the most recently added object and // returns it. Same operation as pop code based on Bailey pg. 128

©Duane Szafron Structure Interface - Stack 2 public Object pop (); // pre: stack is not empty // post: removes the most recently pushed object and // returns it. Same operation as remove public Object peek (); // pre: stack is not empty // post: returns the next object to be removed, but does in // not remove it } code based on Bailey pg. 128

©Duane Szafron Stack Example - Stack frames One common application of Stacks is to hold the program state during the execution of a method (or procedure or function). In this case, the objects on the Stack are stack frames. In recursive calls, many stack frames can be created in the virtual machine. The textbook shows how an application level stack can simulate recursion and eliminate the actual recursion in the virtual machine.

©Duane Szafron Stack Example - Maze Algorithm One common approach to solving search problems is to use a Stack to hold “unsearched” paths. 0 S F Select the start square as the current square. Repeat as long as the current square is not null and is not the finish square: –“Visit” the square. –Push one square on the stack for each unvisited legal move from the current square. –Pop the stack into current square or bind the current square to null if the stack is empty. If the current square is the goal we are successful, otherwise there is no solution

©Duane Szafron Stack Example - Maze Trace 1 0 S F P0 P1 0 S F P1 P6 P2 0 S F P2 P6 P3 0 S F P3 P6 P4 0 S F P4 P6 P5 0 S F P5 P6

©Duane Szafron Stack Example - Maze Trace 2 P6 P10 0 S F P7 P10 P8 0 S F P8 P10 P9 0 S F P9 P10 0 S F Succes s!

©Duane Szafron Book Implementation of a Maze In the maze example of the textbook: –The walls of the maze are “widened” so they become positions in the maze. –This allows the maze to be represented in a textual format with # characters as “walls”. 0 S F ###### #s# # # ## # # # #### # # ## # # #f# ######

©Duane Szafron Maze Program 1 public static void main (String[ ] args) { // A Maze program. Maze maze; Position goal; Position current; Linear todo; maze = new Maze(args[0]); // Create a maze from a file goal = maze.finish(); todo = new StackList(); // A class that implements Stack current = maze.start(); code based on Bailey pg. 150

©Duane Szafron Maze Program 2 while (current != null && !current.equals(goal)) { maze.visit(current); pushUnvisitedNeighbor(todo, maze, current.north()); pushUnvisitedNeighbor(todo, maze, current.south()); pushUnvisitedNeighbor(todo, maze, current.west()); pushUnvisitedNeighbor(todo, maze, current.east()); if (todo.isEmpty()) current = null; else current = (Position) todo.remove(); } if (current == null) System.out.println(“No solution exists”); else System.out.println(“Solution found”); System.out.println(maze); } code based on Bailey pg. 150

©Duane Szafron Maze Program 3 public static void pushUnvisitedNeighbor (Linear todo, Maze maze, Position position) { // Post: If the given position is unvisited and is clear (not // a wall) in the given maze, it is pushed onto the given Stack. if (maze.isClear(position) && ! maze.isVisited(position)) todo.add(position); } code based on Bailey pg. 150

©Duane Szafron Vector-Based Stacks We can implement a Stack using a single Vector. This implementation is space and time efficient. It is also simple, so few mistakes can be made. However, the time for adding a particular element can vary, depending on whether the Vector must grow or not. Since the Vector will never shrink, the physical memory required is at least as large as the maximum Stack size, even if the Stack shrinks.

©Duane Szafron StackVector - State and Constructors public class StackVector implements Stack { /* An implementation of the Stack Interface that uses a single Vector to store the Stack elements. */ // Instance Variables // protected Vector data; public StackVector () { // post: initialize the Stack to be empty this.data = new Vector(); } public StackVector (int size) { /* post: initialize the Stack to be empty, but with the given initial capacity. */ this.data = new Vector(size); } code based on Bailey pg. 132

©Duane Szafron StackVector - Linear Interface 1 public void add (Object anObject) { // post: the object that is added will be popped next if // nothing else is added. Same operation as push this.data.addElement(anObject); } public Object remove (); // pre: stack is not empty // post: removes the most recently added object and // returns it. Same operation as pop Object result; result = this.data.elementAt(this.data.size() - 1); this.data.removeElementAt(this.data.size() - 1); return result; } code based on Bailey pg. 132

©Duane Szafron StackVector - Linear Interface 2 public Object peek (); // pre: stack is not empty // post: returns the next object to be removed, but // does in not remove it Assert.pre(!this.isEmpty(), “Stack is not empty”); return this.data.elementAt(this.data.size() - 1); } code based on Bailey pg. 132

©Duane Szafron StackVector - Stack Interface public void push (Object anObject); // post: the object that is added will be popped next if // nothing else is pushed. Same operation as add this.add(anObject); } public Object pop (); // pre: stack is not empty // post: removes the most recently pushed object and // returns it. Same operation as remove return this.remove(); } code based on Bailey pg. 133

©Duane Szafron StackVector - Store Interface public int size (); //post: returns the number of elements contained in the store. return this.data.size(); } public boolean isEmpty (); // post: returns the true iff store is empty. return this.size() == 0; } public void clear (); // post: clears the store so that it contains no elements. this.data.clear(); } code based on Bailey pg. 134

©Duane Szafron List-Based Stacks We can implement a Stack using a SinglyLinkedList. Recall that operations at the head of a SinglyLinkedList take constant time so this implementation is time efficient. As the Stack grows and shrinks, so does the list, unlike the Vector implementation that never shrinks. The time for adding a single element is always the same, unlike the Vector implementation There is extra link space required for each element. The implementation is simple, so few mistakes can be made.

©Duane Szafron StackList - State and Constructors public class StackList implements Stack { /* An implementation of the Stack Interface that uses a SinglyLinkedList to store the Stack elements. */ // Instance Variables // protected List data; public StackList() { // post: initialize the Stack to be empty this.data = new SinglyLinkedList(); } code based on Bailey pg. 134

©Duane Szafron StackList - Linear Interface 1 public void add (Object anObject) { // post: the object that is added will be popped next if // nothing else is added. Same operation as push this.data.addToHead(anObject); } public Object remove (); // pre: stack is not empty // post: removes the most recently added object and // returns it. Same operation as pop return this.data.removeFromHead(); } code based on Bailey pg. 134

©Duane Szafron StackList - Linear Interface 2 public Object peek (); // pre: stack is not empty // post: returns the next object to be removed, but does in // not remove it Assert.pre(!this.isEmpty(), “Stack is not empty”); return this.data.peek(); } code based on Bailey pg. 135

©Duane Szafron StackList - Stack Interface public void push (Object anObject); // post: the object that is added will be popped next if // nothing else is pushed. Same operation as add this.add(anObject); } public Object pop (); // pre: stack is not empty // post: removes the most recently pushed object and // returns it. Same operation as remove return this.remove(); } code based on Bailey pg. 135

©Duane Szafron StackList - Store Interface public int size (); //post: returns the number of elements contained in the store. return this.data.size(); } public boolean isEmpty (); // post: returns the true iff store is empty. return this.size() == 0; } public void clear (); // post: clears the store so that it contains no elements. this.data.clear(); } code based on Bailey pg. 135