Rossella Lau Lecture 12, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.

Slides:



Advertisements
Similar presentations
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Advertisements

CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
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
Stacks Example: Stack of plates in cafeteria.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
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.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
 Balancing Symbols 3. Applications
EC-211 DATA STRUCTURES LECTURE 5. 2 Stack Data Structure Stack of Books Stack of Notes Stack of Gifts.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
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,
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
The Stack Alexander G. Chefranov CMPE-231 Spring 2012.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Data Structures and Algorithms
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.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
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.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structures & Algorithms
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
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.
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(),
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.
Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss,
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Stacks.
Revised based on textbook author’s notes.
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
Stacks Stack: restricted variant of list
Stacks.
Stacks Chapter 4.
Data Structures – Week #3
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
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Mark Allen Weiss, Addison Wesley
Lecture No.07 Data Structures Dr. Sohail Aslam
Stack and Queues Stack implementation using Array
COMPUTER 2430 Object Oriented Programming and Data Structures I
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
Data Structures and Algorithms 2/2561
Stacks.
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:

Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic operations of a stack its implementation using a vector applications of stack  Expression Evaluation Infix, Prefix, and Postfix expressions Implementation of evaluating a postfix expression -- By Rossella Lau

Rossella Lau Lecture 12, DCO20105, Semester A, Stack  Stack is an ordered list of items, ordered in the input sequence  Basically, items  are inserted and deleted at one end, called the top of the stack  are last in first out (LIFO)  An example: The original Take an item Add an item A B C A B A B D A B D E C

Rossella Lau Lecture 12, DCO20105, Semester A, Operations of a stack  Basic:  push() – to add an item  pop() – to remove an item  Assistance:  size() – returns the number of items in a stack  empty() – to check if a stack is empty  top() – returns the first element of a stack; note that in a queue, it is called front()

Rossella Lau Lecture 12, DCO20105, Semester A, Exercises  Ford: 7: 13,14; 8:11

Rossella Lau Lecture 12, DCO20105, Semester A, Implementation of a stack using a vector // Stack.h template class Stack { private: vector stack; public: void push(T const & item) {stack.push_back(item);} T & pop() {T t(top()); stack.pop_back(); return t;} size_t size() const { return stack.size();} bool empty() const { return stack.empty();} T const & top() const { return stack.back();} };

Rossella Lau Lecture 12, DCO20105, Semester A, Applications of stack  Nested parentheses check  Expression evaluation  The underlying structure for recursion  Function calls (and its associated variables), e.g., : main check init main check init main check push main check push pop …

Rossella Lau Lecture 12, DCO20105, Semester A, A stack application: nested parentheses check  Consider the parentheses in mathematical expressions 1. Equal number of right and left parentheses 2. Every right parenthesis is preceded by a matching parenthesis  Wrong expressions: ((A+B) violate condition 1 )A+B(-Cviolate condition 2  Solution: nesting depth parenthesis count

Rossella Lau Lecture 12, DCO20105, Semester A, Use of nesting depth and parenthesis count  Nesting depth: left parenthesis: open a scope right parenthesis: close a scope nesting depth at a particular point is the number of scopes that have been opened but not closed  Parenthesis count: at a particular point as the number of left parentheses minus the number of right parentheses if the count is not negative, it is the nesting depth  Check parentheses use by checking if parenthesis count: is greater than or equal to 0 at any point is equal to 0 at the end of the expression of course, checking should include the parenthesis type: {}, [],()

Rossella Lau Lecture 12, DCO20105, Semester A, Use of a stack to check  For each left parenthesis, do a push  For each right parenthesis, do a pop and make sure the item popped is equal to the right parenthesis (same type)  If the stack is empty when doing a pop  ERROR  If the stack is not empty when the expression ends  ERROR  Examples of checking invalid expressions: ( (( ((A+B) !empty(s) )A+B(-C Cannot pop ( [(A+B]) [ ( Type mismatch ( [ (

Rossella Lau Lecture 12, DCO20105, Semester A, Algorithm to check pairs of parentheses bool check(string expression){ valid = true stack brackets while (expression not end){ read a symbol (symb) if symb is a left bracket brackets.push(symb) if symb is a right bracket{ if brackets.empty() valid=false if brackets.front() not match symb valid = false brackets.pop() } } if !brackets.empty() valid = false return valid} { {x+(y-[a+b])*c-(d+e)}/(h-(j-(k-[l-n]))) ( [ {x+(y-[ [ ( { { … a+b]) ( {{ ( { … *c-( { … d+e) { {…}{…} ( ( ( …/(h-(j-(k-[ [ ( ( ( [ …l-n])))

Rossella Lau Lecture 12, DCO20105, Semester A, Expression evaluation  An expression is a series of operators and operands  Operators: +, -, *, /, %, $ (exponentiation)  Operands: the values going to be operated Three representations of an expression:  Infix: The usual form, operator is between operands  Prefix: the operator is in front of the operand(s)  Postfix: the operand(s) is(are) in front of the operator

Rossella Lau Lecture 12, DCO20105, Semester A, Examples of Prefix Expressions Infix A+B A+B-C (A+B)*(C-D) A$B*C-D+E/F/(G+H) ((A+B)*C-(D-E))$(F+G) A-B/(C*D$E) Prefix +AB -+ABC *+AB-CD +-*$ABCD//EF+GH $-*+ABC-DE+FG -A/B*C$DE

Rossella Lau Lecture 12, DCO20105, Semester A, Examples of Postfix Expressions Infix A+B A+B-C (A+B)*(C-D) A$B*C-D+E/F/(G+H) ((A+B)*C-(D-E))$(F+G) A-B/(C*D$E) Postfix AB+ AB+C- AB+CD-* AB$C*D-EF/GH+/+ AB+C*DE--FG+$ ABCDE$*/-

Rossella Lau Lecture 12, DCO20105, Semester A, Evaluating a postfix expression  The advantage is no parentheses, i.e., less complication  Since the operator comes later, operands can be pushed to the stack first and once an operator is encountered, the operands are popped for the calculation  The algorithm (one digit operands) : /* for one digit operands and no space in the expression */ int eval(string const & expr) { Stack operands; for (size_t i=0; i<expr.size();i++){ char c = expr[i]; if (isdigit(c)) //operand operands.push((double)(c-'0'); else { // operator int operand2=operands.pop(); int operand1=operands.pop(); operands.push(cal (c, operand1, operand2)); } } return operands.top(); }

Rossella Lau Lecture 12, DCO20105, Semester A, The algorithm for more than one digit double eval(string const & expr) { Stack operands; char c, number[MAX]; size_t i, j=0; double num; for (i=0; i<expr.size(); i++) { c = expr[i]; if (isdigit(c) || c=='.') number[j++]=c; else { // a token is parsed if (j) // convert number to num operands.push(num); if (c != ' ') { // operand // pop, cal, and push }} } return operands.top(); }......//convert number to num number[j]='\0'; j=0; num = atof(number); // pop, cal, and push double operand2 = operands.pop(); double operand1 = operands.pop(); operands.push(cal(c, operand1, operand2));

Rossella Lau Lecture 12, DCO20105, Semester A, cal(char, double, double) double cal(char oper, double op1,double op2) { switch (oper) { // “operator” is a key word case '+': return op1 + op2; case '-': return op1 - op2; case '*': return op1 * op2; case '/': return op1 / op2; case '$': return pow(op1,op2); default: // should not happen exit(EXIT_FAILURE); }

Rossella Lau Lecture 12, DCO20105, Semester A, An example of postfix expression evaluation  *  scan 457: push(4), push(5), push(7)  scan +: op2=7, pop(), op1=5, pop(), push(op1+op2)  scan *: op2=12, pop(), op1=4, pop(), push(op1*op2)  expression end ==> return 48!

Rossella Lau Lecture 12, DCO20105, Semester A, Summary  A stack is one of the most basic data structures in the study.  It only has one end for insert and delete.  An item in a stack is LIFO while it is FIFO in a queue  Stack is used in many hidden areas of computer systems  One popular important application is expression evaluation  An expression can be in three forms: infix, prefix, and postfix  It is common to evaluate an expression by using a postfix format

Rossella Lau Lecture 12, DCO20105, Semester A, Reference  Ford: 7  Example programs: evaluateIntExpression.cpp, evaluateDoubleExpression.cpp  STL online references   END --