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

Similar documents
Lecture 12 ADTs and Stacks

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

STACKS AND QUEUES. Problem Solving with Computers-II

March 13/2003 Jayakanth Srinivasan,

Stacks. Revised based on textbook author s notes.

What is an algorithm?

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

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

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

Stack Abstract Data Type

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

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.

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Stacks Fall 2018 Margaret Reid-Miller

Stacks, Queues and Hierarchical Collections

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

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

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

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

The Stack and Queue Types

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stack and Its Implementation

15. Stacks and Queues

Lecture Data Structure Stack

08 STACKS DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

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

PA3 Design Specification

CSC148 Week 2. Larry Zhang

Problem with Scanning an Infix Expression

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

3137 Data Structures and Algorithms in C++

CS 206 Introduction to Computer Science II

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

Data Structures Week #3. Stacks

Stacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

CS 206 Introduction to Computer Science II

Stacks and their Applications

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

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

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

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

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

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Stacks and Queues. CSE Data Structures April 12, 2002

CSCA48 Term Test 1 Seminar

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

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

CS W3134: Data Structures in Java

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

DEEPIKA KAMBOJ UNIT 2. What is Stack?

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

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

An Introduction to Trees

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

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

12 Abstract Data Types

Table of Contents. Chapter 1: Introduction to Data Structures... 1

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

CSI33 Data Structures

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

CMPT 225. Stacks-part2

DATA STRUCTURE UNIT I

Fundamentals of Programming CS-110. Lecture 3

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

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Data Structures and Algorithms

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

Algorithms and Data Structures

File Input and Output

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

Programming Abstractions

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

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

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

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

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

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

CMPT 225. Lecture 9 Stack

CSC 222: Computer Programming II. Spring 2005

Lab 7 1 Due Thu., 6 Apr. 2017

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

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

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

Lecture 4 Stack and Queue

[CS302-Data Structures] Homework 2: Stacks

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

CHAPTER 8: Central Processing Unit (CPU)

UNIT 3

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

Largest Online Community of VU Students

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

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

Draw a diagram of an empty circular queue and describe it to the reader.

Transcription:

Outline Chapter 5: and Queues 1 Chapter 5: and Queues

Chapter 5: and Queues The Stack ADT A Container Class for Last-In-First-Out Access A stack is a last in, first out (LIFO) structure, i.e. a list-like container with access restricted to one end of the list (the top of the stack). One can push an item onto the stack pop an item off the stack (precondition: stack is not empty) Inspect the top position (precondition: stack is not empty) Obtain the current size of the stack.

Chapter 5: and Queues The Stack ADT Specification for a typical stack class Stack: def init (self): """ post: creates an empty LIFO stack""" def push(self,x): """post: places x on top of the stack""" def pop(self): """pre: self.size()>0 post: removes and returns the top element""" def top(self): """pre: self.size()>0 post: returns the top element""" def size(self): """post: returns the number of elements in the stack"""

Chapter 5: and Queues Simple Stack Applications Few Examples of Stack Applications graphical editors ( undo operations) function calls ( nested function calls) Evaluation of expressions example: ((x + y)/(2 x) 10 z) - balance of grouping symbols See the code of Stack.py

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols Assume we are given an algebraic expression and are asked to check that the grouping symbols are ballanced. Examples: ((x + y)/(2 x) 10 z) [x 3 2 (2 x 5 19x 3)] {2 x ([a b] 2 10 g) + 7 (2 5 [a 2 b 2])} 10 x {x y}/{x + y}

Chapter 5: and Queues Stack Applications: Grouping Symbols Reasoning Questions: What grouping symbols can we meet? Do we care about all other symbols (non-grouping ones)? Examples: ((x + y)/(2 x) 10 z) [x 3 2 (2 x 5 19x 3)] {2 x ([a b] 2 10 g) + 7 (2 5 [a 2 b 2])} 10 x

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols IDEA: input: a string (or a sequence) of symbols output: verdict (True/False) 1 get the next symbol from the input 2 if it is an opening grouping symbol, push it into the stack 3 if it is a closing grouping symbol, pop the grouping symbol from the stack, check for correspondence : {},(),[] if they correspond, proceed to step 1 otherwise return False 4 (there are no more symbols in the input) if the stack is not empty return False, otherwise return True

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 ( [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 ( [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 ( [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 ( [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 ( [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 [ {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4 {

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols {[2 * (7-4) + 2] + 3} * 4

Chapter 5: and Queues Stack Applications: Grouping Symbols Balanced Grouping Symbols def parensbalance2(s): stack = Stack() for ch in s: if ch in "([{": # push an opening marker stack.push(ch) elif ch in ")]}": # match closing if stack.size() < 1: # no pending open return False else: opener = stack.pop() if opener+ch not in ["()", "[]", "{}"]: return False # not a matching pair return stack.size() == 0 # everything matched?

Chapter 5: and Queues An Application: Expression Manipulation Notations For Operations infix notation: (2 + 3) * 4 operators are between numbers prefix (Polish) notation: * + 2 3 4 start from the right, walk to the left postfix (reverse Polish) notation: 2 3 + 4 * start from the left, walk to the right

Chapter 5: and Queues An Application: Expression Manipulation prefix (Polish) notation * + 2 3 4 =

Chapter 5: and Queues An Application: Expression Manipulation prefix (Polish) notation * + 2 3 4 = = * 5 4 = 20

Chapter 5: and Queues An Application: Expression Manipulation postfix (reverse Polish) notation 2 3 + 4 * =

Chapter 5: and Queues An Application: Expression Manipulation postfix (reverse Polish) notation 2 3 + 4 * = = 5 4 * = 20

Chapter 5: and Queues An Application: Expression Manipulation prefix and postfix notations The advantage of the prefix and postfix notations: parentheses are not necessary to modify the order of operations.

Chapter 5: and Queues An Application: Expression Manipulation Notation For Operations Postfix notation expressions can be evaluated easily using a stack: each time an operation is encountered, two numbers are popped off the stack, the operator is applied to those two numbers, and the result is pushed on the stack.

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * +

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 3

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 4 3

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 5 4 3

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 9 3

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 27

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 2 27

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 25

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 3 25

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 6 3 25

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 18 25

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression 3 4 5 + * 2-3 6 * + 43

Chapter 5: and Queues An Application: Expression Manipulation Evaluating A Postfix Expression Note that the order in which the values are popped from the stack is important! 4 5-2 * stands for (4-5)*2. Not (5-4)*2, not 2*(5-4) Your HW assignment will be to implement the evaluation of a valid post-fix expression.

Chapter 5: and Queues The Call Stack Function Calls Can Be Nested function A calls function B function B returns function A continues

Chapter 5: and Queues The Call Stack Activation Records Function A is running, and calls function B. The local variables of function A, their current values, and where function B should return to are put into an activation record. The activation record is pushed onto the call stack which has been allocated for the program that is running. When function B returns, this record is popped off the call stack and used to continue running the program.

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return locals return Call Stack

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return locals return Call Stack a = 3

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return locals return Call Stack a = 3, b = 4

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return a=3,b=4 main 10: locals return Call Stack x = 3, y = 4

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return x=3,y=4 A 2: a=3,b=4 main 10: locals return Call Stack n = 3

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return x=3,y=4 A 2: a=3,b=4 main 10: locals return Call Stack n = 3, n2 = 9

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return x=3,y=4 A 2: a=3,b=4 main 10: locals return Call Stack n = 3, n2 = 9

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return a=3,b=4 main 10: locals return Call Stack x = 3, y = 4, x2 = 9

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return x=3,y=4 x2=9 A 3: a=3,b=4 main 10: locals return Call Stack n = 4

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return x=3,y=4 x2=9 A 3: a=3,b=4 main 10: locals return Call Stack n = 4, n2 = 16

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return x=3,y=4 x2=9 A 3: a=3,b=4 main 10: locals return Call Stack n = 4, n2 = 16

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return a=3,b=4 main 10: locals return Call Stack x=3,y=4,x2=9,y2=16

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return a=3,b=4 main 10: locals return Call Stack x=3,y=4,x2=9,y2=16,z=25

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return a=3,b=4 main 10: locals return Call Stack x=3,y=4,x2=9,y2=16,z=25

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return locals return Call Stack a = 3, b = 4, c = 25

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return locals return Call Stack a = 3, b = 4, c = 25

Chapter 5: and Queues The Call Stack Example def A(x, y): 1: x2 = B(x) 2: y2 = B(y) 3: z = x2 + y2 4: return z def B(n): squares n 5: n2 = n * n 6: return n2 def main(): 7: a = 3 8: b = 4 9: c = A(a, b) 10: print(c) 11: return locals return Call Stack a = 3, b = 4, c = 25

Chapter 5: and Queues In-class work Re-write expression 7 (2 + 5) 3 (6 7) in postfix notation re-write the expression 3 2 5 7 3 + (it is in postfix notation) in infix notation (common way) Do unit testing of methods push and size in Stack.py. For example, to test the push function: push a value onto the stack, retrieve it immediately (using pop or top) and check whether the retrieved value is equal to the one you just pushed.