CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
COSC 2006 Chapter 7 Stacks III
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.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Infix, Postfix, Prefix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Implementing and Using Stacks
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
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.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Stack Applications.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
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.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 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
A grammar for arithmetic expressions involving the four basic operators and parenthesized expressions. Parenthesized expressions have the highest precedence.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
CHP-3 STACKS.
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.
Stacks Objective After this lecture you will be able to: Describe a stack Describe the representation of stack using linear array Describe the representation.
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.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
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.
Review Use of Stack Introduction Stack in our life Stack Operations
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
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
Stacks.
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Stack.
More About Stacks: Stack Applications
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
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:

CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1

Quick Recap  Stacks are linear lists.  All deletions and insertions occur at one end of the stack known as the TOP.  Data going into the stack first, leaves out last.  Stacks are also known as LIFO data structures (Last- In, First-Out). 2

Basic Stack Operations  push – Adds an item to the top of a stack.  pop – Removes an item from the top of the stack and returns it to the user.  stack top (top, peek) – Copies the top item of the stack and returns it to the user; the item is not removed, hence the stack is not altered. 3

Additional Notes  Stacks structures are usually implemented using arrays or linked lists.  For both implementations, the running time is O(n).  We will be examining common Stack Applications. 4

5 Uses of Stacks  The runtime stack used by a process (running program) to keep track of methods in progress  Undo, redo, back, forward

Search Backtracking  Stacks can be used to backtrack to achieve certain goals.  Usually, we set up backtrack tokens to indicate a backtrack opportunity. 7

Stack Applications  Reversing Data: We can use stacks to reverse data. (example: files, strings) Very useful for finding palindromes. Consider the following pseudocode: 1)read (data) 2)loop (data not EOF and stack not full) 1) push (data) 2) read (data) 3)Loop (while stack notEmpty) 1) pop (data) 2) print (data) 8

Stack Applications  Converting Decimal to Binary: Consider the following pseudocode 1) Read (number) 2) Loop (number > 0) 1) digit = number modulo 2 2) print (digit) 3) number = number / 2 // from Data Structures by Gilbert and Forouzan The problem with this code is that it will print the binary number backwards. (ex: 19 becomes instead of ) To remedy this problem, instead of printing the digit right away, we can push it onto the stack. Then after the number is done being converted, we pop the digit out of the stack and print it. 9

10 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.  Applicable to checking html and xml tags!

11 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

12 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

13 Mathematical Calculations  What does * 4 equal? 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. What about ^ 5 * 3 * 6 / 7 ^ 2 ^ 3

CS314Stacks 14 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 *  evaluate the following postfix expressions and write out a corresponding infix expression: * + * ^ * ^ 3 * 6 / +2 5 ^ 1 -

Clicker Question 2  What does the following postfix expression evaluate to? * A. 18 B. 36 C. 24 D. 11 E. 30 CS314Stacks 15

Stack Applications  Postponement: Evaluating arithmetic expressions.  Prefix: + a b  Infix: a + b (what we use in grammar school)  Postfix: a b +  In high level languages, infix notation cannot be used to evaluate expressions. We must analyze the expression to determine the order in which we evaluate it. A common technique is to convert a infix notation into postfix notation, then evaluating it. 16

Infix to Postfix Conversion  Rules:  Operands immediately go directly to output  Operators are pushed into the stack (including parenthesis) - compare current operator o 1 to stack top operator o 2 - while ((o 1 <= o 2 and o 1 is left-associative) or (o 1 < o 2 and o 1 is right associative)) - Pop (o 2 ) and check the next o 2 to remain in the loop or exit - Push (o 1 ); - i.e. push o 1 when its precedence is higher than o 2 generally, otherwise, pop o 2 taking into consideration the associativity rules shown above. - Priority 1: * / ^ - Priority 0: + - Open parenthesis are always pushed on the stack. If we encounter a closing parenthesis, pop the stack and print the operators until you see a left parenthesis. Do not output parenthesis. 17

Infix to Postfix Example #1 A + B * C - D / E (general example) Infix Stack(bot->top)Postfix a) A + B * C - D / E b) + B * C - D / EA c) B * C - D / E+A d) * C - D / E+A B e) C - D / E+ *A B f) - D / E+ *A B C g) D / E-A B C * + h) / E-A B C * + D i) E- /A B C * + D j)- /A B C * + D E k)A B C * + D E / - 18

Infix to Postfix Example #2 A / B ^ C - D (operator precedence example) Infix Stack(bot->top)Postfix a) A / B ^ C - Demptyempty b) / B ^ C - DemptyA c) B ^ C - D/A d) ^ C - D/A B e) C - D/ ^A B f) - D/ ^A B C g) - D/A B C ^ h) - DemptyA B C ^ / i) D-A B C ^ / j) -A B C ^ / D k) emptyA B C ^ / D - 19

Infix to Postfix Example #3 A * B - ( C + D ) + E ( parenthesis example) Infix Stack(bot->top)Postfix a) A * B - ( C + D ) + Eemptyempty b) * B - ( C + D ) + EemptyA c) B - ( C + D ) + E*A d) - ( C + D ) + E*A B e) - ( C + D ) + EemptyA B * f) ( C + D ) + E-A B * g) C + D ) + E- (A B * h) + D ) + E- (A B * C i) D ) + E- ( +A B * C j) ) + E- ( +A B * C D k) + E-A B * C D + l) + EemptyA B * C D + - m) E+A B * C D + - n) +A B * C D + - E o) emptyA B * C D + - E + 20

Infix to Postfix Example #4 A - B + C ( left to right association) Infix Stack(bot->top)Postfix a) A - B + C emptyempty b) - B + CemptyA c) B + C-A d) + C-A B e) C+A B - f) +A B - C g) emptyA B - C + 21

Infix to Postfix Example #5 A ^ B ^ C( right to left association) Infix Stack(bot->top)Postfix a) A ^ B ^ Cemptyempty b) ^ B ^ CemptyA c) B ^ C^A d) ^ C^A B e) C^ ^A B f) ^ ^A B C g) emptyA B C ^ ^ 22

23 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

Postfix Evaulation Operand: push Operator: pop 2 operands, do the math, pop result back onto stack * PostfixStack( bot -> top ) a) * b) *1 c) 3 + *1 2 d) + *1 2 3 e) *1 5 // 5 from f) 5// 5 from 1 * 5 24

Postfix Evaulation For Example #1 Infix: A + B * C - D / E Postfix: A B C * + D E / - Values: A = 1, B = 2, C = 3, D = 4, E = 2 PostfixStack( bot -> top ) a) * / -empty b) 2 3 * / -1 c) 3 * / -1 2 d) * / e) / -1 6 // 6 from 2 * 3 f) 4 2 / - 7// 7 from g) 2 / h) / i) - 7 2// 2 from 4 / 2 j) 5// 5 from