Topic 15 Implementing and Using Stacks

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Topic 15 Implementing and Using Stacks
Stacks.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
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.
CSC 211 Data Structures Lecture 23
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Data Structures & Algorithms
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
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.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Building Java Programs Chapter 14
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Lecture No.07 Data Structures Dr. Sohail Aslam
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Topic 15 Implementing and Using Stacks
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
Data Structures and Algorithms 2/2561
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Topic 15 Implementing and Using Stacks "stack n. The set of things a person has to do in the future. "I haven't done it yet because every time I pop my stack something new gets pushed." If you are interrupted several times in the middle of a conversation, "My stack overflowed" means "I forget what we were talking about." -The Hacker's Dictionary Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955. CS 307 Fundamentals of Computer Science Stacks

Stack Overflow CS 307 Fundamentals of Computer Science Stacks

Sharper Tools Stacks Lists CS 307 Fundamentals of Computer Science

Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations are limited: push (add item to stack) pop (remove top item from stack) top (get top item without removing it) clear isEmpty size? Described as a "Last In First Out" (LIFO) data structure CS 307 Fundamentals of Computer Science Stacks

Stack Operations Assume a simple stack for integers. Stack s = new Stack(); s.push(12); s.push(4); s.push( s.top() + 2 ); s.pop() s.push( s.top() ); //what are contents of stack? CS 307 Fundamentals of Computer Science Stacks

Stack Operations Write a method to print out contents of stack in reverse order. CS 307 Fundamentals of Computer Science Stacks

Common Stack Error Stack s = new Stack(); // put stuff in stack for(int i = 0; i < 5; i++) s.push( i ); // print out contents of stack // while emptying it. (??) for(int i = 0; i < s.size(); i++) System.out.print( s.pop() + “ “); // What is output? CS 307 Fundamentals of Computer Science Stacks

Attendance Question 1 What is output of code on previous slide? B 4 3 2 1 0 C 4 3 2 D 2 3 4 E No output due to runtime error. CS 307 Fundamentals of Computer Science Stacks

Corrected Version Stack s = new Stack(); // put stuff in stack for(int i = 0; i < 5; i++) s.push( i ); // print out contents of stack // while emptying it int limit = s.size(); for(int i = 0; i < limit; i++) System.out.print( s.pop() + “ “); //or // while( !s.isEmpty() ) // System.out.println( s.pop() ); CS 307 Fundamentals of Computer Science Stacks

Implementing a stack need an underlying collection to hold the elements of the stack 2 basic choices array (native or ArrayList) linked list array implementation linked list implementation Some of the uses for a stack are much more interesting than the implementation of a stack CS 307 Fundamentals of Computer Science Stacks

Applications of Stacks CS 307 Fundamentals of Computer Science Stacks

Problems that Use Stacks The runtime stack used by a process (running program) to keep track of methods in progress Search problems Undo, redo, back, forward CS 307 Fundamentals of Computer Science Stacks

Mathematical Calculations What is 3 + 2 * 4? 2 * 4 + 3? 3 * 2 + 4? The precedence of operators affects the order of operations. A mathematical expression cannot simply be evaluated left to right. A challenge when evaluating a program. Lexical analysis is the process of interpreting a program. Involves Tokenization What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3 CS 307 Fundamentals of Computer Science Stacks

Infix and Postfix Expressions The way we are use to writing expressions is known as infix notation Postfix expression does not require any precedence rules 3 2 * 1 + is postfix of 3 * 2 + 1 evaluate the following postfix expressions and write out a corresponding infix expression: 2 3 2 4 * + * 1 2 3 4 ^ * + 1 2 - 3 2 ^ 3 * 6 / + 2 5 ^ 1 - CS 307 Fundamentals of Computer Science Stacks

Attendance Question 2 What does the following postfix expression evaluate to? 6 3 2 + * 18 36 24 11 30 CS 307 Fundamentals of Computer Science Stacks

Evaluation of Postfix Expressions Easy to do with a stack given a proper postfix expression: get the next token if it is an operand push it onto the stack else if it is an operator pop the stack for the right hand operand pop the stack for the left hand operand apply the operator to the two operands push the result onto the stack when the expression has been exhausted the result is the top (and only element) of the stack CS 307 Fundamentals of Computer Science Stacks

Infix to Postfix Convert the following equations from infix to postfix: 2 ^ 3 ^ 3 + 5 * 1 11 + 2 - 1 * 3 / 3 + 2 ^ 2 / 3 Problems: Negative numbers? parentheses in expression CS 307 Fundamentals of Computer Science Stacks

Infix to Postfix Conversion Requires operator precedence parsing algorithm parse v. To determine the syntactic structure of a sentence or other utterance Operands: add to expression Close parenthesis: pop stack symbols until an open parenthesis appears Operators: Have an on stack and off stack precedence Pop all stack symbols until a symbol of lower precedence appears. Then push the operator End of input: Pop all remaining stack symbols and add to the expression CS 307 Fundamentals of Computer Science Stacks

Simple Example Infix Expression: 3 + 2 * 4 PostFix Expression: Operator Stack: Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1 - 1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 CS 307 Fundamentals of Computer Science Stacks

Simple Example Infix Expression: + 2 * 4 PostFix Expression: 3 Operator Stack: Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1 - 1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 CS 307 Fundamentals of Computer Science Stacks

Simple Example Infix Expression: 2 * 4 PostFix Expression: 3 Operator Stack: + Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1 - 1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 CS 307 Fundamentals of Computer Science Stacks

Simple Example Infix Expression: * 4 PostFix Expression: 3 2 Operator Stack: + Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1 - 1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 CS 307 Fundamentals of Computer Science Stacks

Simple Example Infix Expression: 4 PostFix Expression: 3 2 Operator Stack: + * Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1 - 1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 CS 307 Fundamentals of Computer Science Stacks

Simple Example Infix Expression: PostFix Expression: 3 2 4 Operator Stack: + * Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1 - 1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 CS 307 Fundamentals of Computer Science Stacks

Simple Example Infix Expression: PostFix Expression: 3 2 4 * Operator Stack: + Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1 - 1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 CS 307 Fundamentals of Computer Science Stacks

Simple Example Infix Expression: PostFix Expression: 3 2 4 * + Operator Stack: Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1 - 1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 CS 307 Fundamentals of Computer Science Stacks

Example 1 - 2 ^ 3 ^ 3 - ( 4 + 5 * 6 ) * 7 Show algorithm in action on above equation CS 307 Fundamentals of Computer Science Stacks

Balanced Symbol Checking In processing programs and working with computer languages there are many instances when symbols must be balanced { } , [ ] , ( ) A stack is useful for checking symbol balance. When a closing symbol is found it must match the most recent opening symbol of the same type. Algorithm? CS 307 Fundamentals of Computer Science Stacks

Algorithm for Balanced Symbol Checking Make an empty stack read symbols until end of file if the symbol is an opening symbol push it onto the stack if it is a closing symbol do the following if the stack is empty report an error otherwise pop the stack. If the symbol popped does not match the closing symbol report an error At the end of the file if the stack is not empty report an error CS 307 Fundamentals of Computer Science Stacks

Algorithm in practice list[i] = 3 * ( 44 - method( foo( list[ 2 * (i + 1) + foo( list[i - 1] ) ) / 2 * ) - list[ method(list[0])]; Complications when is it not an error to have non matching symbols? Processing a file Tokenization: the process of scanning an input stream. Each independent chunk is a token. Tokens may be made up of 1 or more characters CS 307 Fundamentals of Computer Science Stacks