CMPT 225 Lecture 7 – Stack.

Slides:



Advertisements
Similar presentations
CS Data Structures II Review COSC 2006 April 14, 2017
Advertisements

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.
Stacks.
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.
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.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
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.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Click to edit Master text styles Stacks Data Structure.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Unit 2 - Stack and Queue Slides 18 to end. Queue CMPT Anne Lavergne18.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks Stacks.
Stacks and Queues.
CSCI 3333 Data Structures Stacks.
Chapter 6: The Stack Abstract Data Type
CMPT 120 Topic: Searching – Part 1
CC 215 Data Structures Stack ADT
Week 3 - Friday CS221.
Homework 4 questions???.
Stacks.
Chapter 6: The Stack Abstract Data Type
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
CSE 373: Data Structures and Algorithms
Stack and Queue APURBO DATTA.
Stacks and Queues.
Building Java Programs Chapter 14
Stacks and Queues.
Building Java Programs
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Introduction to Data Structures
Building Java Programs
Building Java Programs
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks and Queues CLRS, Section 10.1.
CSC 1052 Stacks 1/11/2019.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Data Structures and Algorithms for Information Processing
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Stacks Chapter 5.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
CMPT 225 Lecture 5 – linked list.
Lecture 3 – Data collection List ADT
Lecture 4 – Data collection List ADT
CMPT 225 Lecture 8 – Queue.
Data Structures & Programming
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CMPT 225 Lecture 7 – Stack

Last Lecture We saw how to … Create a linked list Perform operations on a linked list Create a Node class Implement a List ADT class: Using an array Using a linked list Determine the time/space efficiency of an algorithm using the Big O notation Compare the various implementations of our List ADT class: Position-oriented versus value-oriented Array-based implementation versus link-based implementation

Learning Outcomes At the end of this lecture (and the activity), a student will be able to: Describe Stack Define public interface of Stack ADT Design and implement Stack ADT using various data structures Compare and contrast these various implementations using Big O notation Give examples of real-life applications (problems) where we could use Stack to solve the problem Solve problems using Stack ADT

Today’s menu Introduce another linear data collection -> Stack

Stack Activity

Step 1 - Problem Statement Web Browser Back and Forward buttons. Sample run: I did some searching on Google and I am currently looking at Google’s search results I click on “wiki” search result and go to “wiki” web page From “wiki” web page, I click on Back button, i.e., go back to “Google” web page I click on Forward button, i.e., go forward to “wiki” web page

Step 2 - Design Solution requires a data collection that allows the following: Add a new element Remove most recently added element

Stack What can we do with a stack? Source: http://i.istockimg.com/file_thumbview_approve/6443810/3/stock-photo-6443810-stack-of-plates.jpg

What characterizes a Stack? A stack only allows elements to be inserted and removed at one end -> top Access to other elements in the stack is not allowed LIFO / FILO Linear data collection Not a “general-purpose” ADT

Step 2 – Design - Stack operations isEmpty: Is the stack empty? push: Insert an element onto the top of the stack pop: Remove the topmost element of the stack peek: Retrieve the topmost element of the stack (but do not remove the element) popAll: Remove all elements of the stack

Step 2 – Design – Stack public interface – Contract - 1 NOTE: Expressed in C++ and using template Class invariant: LIFO / FILO // Description: Returns true if this stack is empty otherwise false. // Time Efficiency: O(1) bool isEmpty( ) const; // Description: Adds a new element to the top of this stack. // Returns true if the addition is successful otherwise false. bool push(const ElementType& newElement);

Step 2 – Design – Stack public interface – Contract - 2 // Description: Removes the top element of this stack. // Returns true if the removal is successful otherwise false. // Precondition: The stack is not empty. // Time Efficiency: O(1) bool pop( ); Alternative: // Description: Removes and returns the top element of this stack. // Exceptions: Throws EmptyStackException if this stack is empty. ElementType pop( ) throw(EmptyStackException);

Step 2 – Design – Stack public interface – Contract - 3 // Description: Removes all elements from this stack. // Returns true if the removal is successful otherwise false. // Precondition: The stack is not empty. bool popAll( ); // Description: Returns the top of this stack. // Postcondition: This stack is unchanged. // Exceptions: Throws EmptyStackException if this stack is empty. // Time Efficiency: O(1) ElementType peek( ) const throw(EmptyStackException);

Let’s test the Stack public interface Using our “Web Browser Back and Forward buttons” problem statement Currently looking at “google” -> currentURL Click on “wiki” -> newURL -> open(newURL ) if ( ! back.push(currentURL) ) throw exception currentURL = newURL if ( ! forward.popAll( ) ) throw exception Click on Back button, i.e., go back to “google”, currentURL -> “wiki” -> back( ) if ( back.isEmpty( ) ) throw exception if ( ! forward.push(currentURL) ) throw exception currentURL = back.pop( ) Click on Forward button, i.e., go forward to “wiki”, currentURL -> “google” -> forward( ) if ( forward. isEmpty( ) ) throw exception currentURL = forward.pop( ) ( why not forward.peek( )? )

Step 3 - Implementing Stack as an ADT Array-based implementation

Step 3 - Implementing Stack as an ADT Link-based implementation

Step 3 - Implementing Stack as an ADT - 1 List ADT-based implementation – refer to List.h (List public interface) posted on our course web site – we shall use it to implement a Stack: class Stack { private: List * elements = new List( ); public: /* Stack public interface */ bool isEmpty( ) const; bool push(const ElementType& newElement); bool pop( ); bool popAll( ); ElementType peek( ) const throw(EmptyStackException); };

Step 3 - Implementing Stack as an ADT - 2 List ADT-based implementation bool isEmpty( ) const { return elements->getElementCount( ) == 0; } bool push(const ElementType& newElement) { // If we consider the “top of the Stack” being the “front of the List” return elements->insert(1, newElement); OR // If we consider the “top of the Stack” being the “end of the List” return elements->insert(elements->getElementCount( ) + 1, newElement);

Step 3 - Implementing Stack as an ADT - 3 bool pop() { // If we consider the “top of the Stack” being the “front of the List” return elements->remove(1); OR // If we consider the “top of the Stack” being the “end of the List” return elements->remove(elements->getElementCount( ) + 1); } bool popAll( ) { return elements->clear();

Step 3 - Implementing Stack as an ADT - 4 ElementType peek( ) const throw(EmptyStackException) { // If we consider the “top of the Stack” being the “front of the List” return elements->getElement(1); OR // If we consider the “top of the Stack” being the “end of the List” return elements->getElement(elements->getElementCount( ) + 1); }

Step 3 - Implementing Stack as an ADT - 5 List ADT-based implementation Advantages: Simple implementation Using code (the List ADT class) that has already been tested Disadvantage: Unless the List ADT class public interface states the time efficiency of its public methods, we will not be able to guaranty that the Stack public methods (calling the List ADT class public methods) will execute in O(1)

Stack ADT - Comparing both its implementations Time efficiency of Stack ADT’s operations (worst case scenario) expressed using the Big O notation Operations array-based link-based List ADT-based isEmpty push pop peek popAll

When Stack ADT is appropriate Examples of problem statements that would be solved using a Stack Compiler: checking for balanced braces while parsing the code in order to verify the syntax Evaluating Postfix expressions Finding our way through a maze Simulating the execution of recursive operations by displaying the call stack, i.e., the activation records (or stack frames) kept in memory and their content Text editing application: Undo and Redo buttons

√ Learning Check We can now … Describe Stack Define public interface of Stack ADT Design and implement Stack ADT using various data structures Compare and contrast these various implementations using Big O notation Give examples of real-life applications (problems) where we could use Stack to solve the problem Solve problems using Stack ADT