The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Similar documents
Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Foundations of Data Structures

Stacks Goodrich, Tamassia

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Stacks Goodrich, Tamassia Stacks

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Application of Stack (Backtracking)

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

1. Stack Implementation Using 1D Array

data structures and algorithms lecture 6

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Data Structures Lecture 5

Stack and Its Implementation

Stacks Fall 2018 Margaret Reid-Miller

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

STACKS AND QUEUES. Problem Solving with Computers-II

Stacks. Revised based on textbook author s notes.

Stack Abstract Data Type

CSC 222: Computer Programming II. Spring 2005

CSC 273 Data Structures

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

infix expressions (review)

CSC 222: Computer Programming II. Spring 2004

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSC148 Week 2. Larry Zhang

Lecture 3 Linear Data Structures

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

Data Structures Week #3. Stacks

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

CMPT 225. Lecture 9 Stack

Lecture 3 Linear Data Structures

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

[CS302-Data Structures] Homework 2: Stacks

Formal Languages and Automata Theory, SS Project (due Week 14)

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

1 P age DS & OOPS / UNIT II

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Stacks and Queues. Chapter Stacks

Lecture 12 ADTs and Stacks

Largest Online Community of VU Students

Top of the Stack. Stack ADT

Lecture Data Structure Stack

8/28/12. Outline. Part 2. Stacks. Linear, time ordered structures. What can we do with Coin dispenser? Stacks

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

COMP9024: Data Structures and Algorithms

March 13/2003 Jayakanth Srinivasan,

Top of the Stack. Stack ADT

Lecture No.04. Data Structures

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Stacks Goodrich, Tamassia, Goldwasser Stacks

csci 210: Data Structures Stacks and Queues

What can we do with Coin dispenser?

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

CSE 143. Lecture 4: Stacks and Queues

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

Types of Data Structures

Computer Science BS Degree - Data Structures (I2206) Abstract Data Types (ADT)

Stacks and their Applications

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University

Stacks and Queues. CSE Data Structures April 12, 2002

Lab 7 1 Due Thu., 6 Apr. 2017

CS212:Data Structure

Data Structures And Algorithms

15. Stacks and Queues

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Stacks, Queues (cont d)

CSC148H Week 3. Sadia Sharmin. May 24, /20

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Stack Implementation

ITI Introduction to Computing II

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

! A data type for which: ! An ADT may be implemented using various. ! Examples:

CIS Fall Data Structures Midterm exam 10/16/2012

Lists are great, but. Stacks 2

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Data Structures and Algorithms

ITI Introduction to Computing II

DataStruct 5. Stacks, Queues, and Deques

Transcription:

The Stack ADT Stacks Set of objects in which the location an item is inserted and deleted is prespecified Stacks! Insert in order! Delete most recent item inserted! LIFO - last in, first out Stacks 2 The Stack ADT Examples of stacks! Cafeteria tray dispenser! Coin dispenser in your car! Balancing braces! Recognizing strings in a language! Evaluating postfix expressions! Converting infix to postfix! Undo sequence in a text editor! Saving local variables when one function calls another, and this one calls another, and so on. Stacks 3 The Stack ADT Main stack operations:! push(object o): inserts element o! pop(): removes the last inserted element! top(): returns a reference to the last inserted element without removing it Auxiliary stack operations:! size(): returns the number of elements stored! isempty(): returns true if the stack is empty, else false Stacks 4

Exceptions Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be thrown by an operation that cannot be executed In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException Stacks 5 C++ Run-time Stack The C++ run-time system keeps track of the chain of active functions with a stack When a function is called, the run-time system pushes on the stack a frame containing! Local variables and return value! Program counter, keeping track of the statement being executed When a function returns, its frame is popped from the stack and control is passed to the method on top of the stack main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { func call bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 Stacks 6 C++ Run-time Stack The C++ run-time system keeps track of the chain of active functions with a stack When a function is called, the run-time system pushes on the stack a frame containing! Local variables and return value! Program counter, keeping track of the statement being executed When a function returns, its frame is popped from the stack and control is passed to the method on top of the stack main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { func call bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 Stacks 7 Array-based Stack A simple way of implementing the Stack ADT uses an array We push (add) elements from left to right A variable keeps track of the index of the last item pushed = 01 23-1 0 1 2 3 4 5 6 7 Stacks 8

Array-based Stack Stack Data Structure We pop (remove) elements from right to left = 01 23-1 0 1 2 3 4 5 6 7 class Stack { private: objecttype stack[ma_stack_size]; int top; public: functions for stack manipulation constructor sets top to -1 ; Stacks 9 Stacks 10 Stack Implementation- Push The array storing the stack elements may become full A push operation will then throw a FullStackException! Limitation of the array-based implementation void push ( objecttype o ) { if ( top + 1 == MA_STACK_SIZE ) throw FullStackException else S[++top] = o; Stacks 11 Stack Implementation- Pop In class exercise - write pop and get functions! Array may be empty when pop! get will return top item/object! will may throw an EmptyStackException objecttype get ( ) { if ( isempty ( ) ) throw EmptyStackException objecttype get ( ) { else if ( isempty ( ) ) return S[top]; throw EmptyStackException else top--; Stacks 12

Performance and Limitations Performance! Let n be the number of elements in the stack! The space used is O(n)! Each operation runs in time O(1) Limitations! The maximum size of the stack must be defined a priori, and cannot be changed! Trying to push a new element into a full stack causes an implementation-specific exception Stack Application - Infix to Postfix Conversion Stack can be used to convert infix mathematical expressions to postfix mathematical expressions Stacks 13 Stacks 14 Stack Application - Infix to Postfix Conversion Algorithm! Process infix expression one item at a time! Operand - write to output! Operator - pop and write to output until an entry of lower priority is found (don t pop parentheses) then push! Left parentheses - push! Right parentheses - pop stack and write to output until left parentheses is found! When done processing expression, pop remaining items and write to output! NOTE - parentheses are not written to the output Stacks 15 Stack Application - Infix to Postfix Conversion a + b * c - (d * e + f) * g Rule Stack Output Left Operand Operator parentheses - - write pop a + a and to - output push write to Operand - + ab output until an write to output +* ab entry of lower +* abc priority is found - abc*+ (don t pop -( abc*+ parentheses) -( abc*+d -(* abc*+d then push -(* abc*+de Stacks 16

Stack Application - Infix to Postfix Conversion Rule Operand Operator When Right done - - write pop parentheses and processing to output write to- expression, output pop stack until and pop remaining write entry to of output lower items priority and until write is left found to parentheses (don t output pop is parentheses) found then push a + b * c - (d * e + f) * g Stack -(+ -(+ - -* -* Output abc*+de* abc*+de*f abc*+de*f+ abc*+de*f+ abc*+de*f+g abc*+de*f+g*- Stacks 17 Stack Application - Evaluating Postfix Expressions You may assume I give you a valid postfix expression Algorithm! Process postfix expression one item at a time! Operand - push! Operator - pop 2 times, evaluate expression ( second_pop operator first_pop), push result onto stack Stacks 18 Stack Application - Evaluating Postfix Expressions 6 * (5 + ((2 + 3) * 8) + 3) => 6 5 2 3 + 8 * + 3 + * Current Symbol Stack 6 6 5 6 5 2 6 5 2 3 6 5 2 3 + 6 5 5 Stack Application - Evaluating Postfix Expressions 6 * (5 + ((2 + 3) * 8) + 3) => 6 5 2 3 + 8 * + 3 + * Current Symbol Stack 8 6 5 5 8 * 6 5 40 + 6 45 3 6 45 3 + 6 48 * 288 Stacks 19 Stacks 20

Other Stack Applications Balanced brace problem! Push every left brace! When you find a right brace, pop and compare. If no matching left brace then error! If stack doesn t end up empty then error Path problem! Take a path and return in the reverse order Stacks 21 Growable Array-based Stack In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be?! incremental strategy: increase the size by a constant c! doubling strategy: double the size Algorithm push(o) if t = S.length! 1 then A " new array of size for i " 0 to t do A[i] " S[i] S " A t " t + 1 S[t] " o Stacks 22 Using a linked list can remove the size restrictions of an array Head will be referred to as the top initially points to NULL All operations and done at the top! Push = Insert at head/top! Pop = Remove from head/top bool isempty ( ) { if ( top == NULL ) return true; else return false; void push ( Node* new ) { new->next = top; top = new; Node* get ( ) { return top; Stacks 23 Stacks 24

Stacks 25 Stacks 26 Stacks 27 Stacks 28

Stacks 29 Stacks 30 In class exercise - Write the pop function! Think about memory leaks " Just delete the node, don t expect user to! Use get ( ) if you want to use the node! Use pop if you just want to remove the node Stacks 31 Stacks 32

Stacks void pop ( ) { if ( isempty ( ) ) throw StackEmptyException else Node* temp = top; top = temp->next; delete temp; Often the array implementation is used since the stack usually never grows very large even when there is a large number of operations Stacks 33 Stacks 34 Stack Big Oh Runtimes Array based! Push! Pop! isempty! get Linked list based! Push! Pop! isempty! get Stacks 35