Data Structures 5th Week

Slides:



Advertisements
Similar presentations
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
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.
Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
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)
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
Stacks Example: Stack of plates in cafeteria.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
CHAPTER 31 STACKS AND QUEUES. CHAPTER 32 A BABA DCBADCBA CBACBA DCBADCBA EDCBAEDCBA top *Figure 3.1: Inserting and deleting elements in a stack (p.102)
 Balancing Symbols 3. Applications
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Arithmetic Expressions
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
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.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
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.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
CH3 Stacks and Queues Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in.
CS Data Structures Chapter 3 Stacks and Queues.
Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions.
Evaluation of Expressions
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
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.
Stacks  Introduction  Applications  Implementations  Complex Applications.
杭州师范大学信息科学与工程学院 袁贞明 STACKS AND QUEUES and their applications.
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
CHP-3 STACKS.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Stack Applications Reading: Chap. 3 Weiss.
Stacks Objective After this lecture you will be able to: Describe a stack Describe the representation of stack using linear array Describe the representation.
STACKS AND QUEUES.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
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.
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.
Stacks Stacks.
Infix to postfix conversion
Stacks Chapter 7 introduces the stack data type.
Stacks Chapter 4.
Stack application: postponing data usage
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Stacks – Calculator Application
Visit for more Learning Resources
Stacks – Calculator Application
PART II STACK APPLICATIONS
Subject Name: DATA STURCTURES WITH C Subject Code: 10CS35
Stacks and Queues The Stack abstract data type
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
CSCE 3110 Data Structures & Algorithm Analysis
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
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:

Data Structures 5th Week Chapter 3 Stacks And Queues 3.4 Evaluation Of Expressions 3.5 Multiple Stacks And Queues

Evaluation of Expressions The order in which the operations are performed precedence hierarchy x = a / b – c + d * e – a * c Infix: operator appears in-between its two operands Postfix: operator appears after its operands ab / c – de * + ac * – The evaluation process of postfix expressions is much simpler than that of infix expressions because there are no parentheses to consider Most compilers use postfix

Infix and postfix notation 2+3*4 2 3 4*+ a*b+5 ab*5+ (1+2)*7 1 2+7* a*b/c ab*c/ ((a/(b-c+d))*(e-a)*c abc-d+/ea-*c* a/b-c+d*e-a*c ab/c-de*+ac*-

Evaluation of postfix expression Place the operands on a stack until we find an operator Remove, from the stack, the correct number of operands for the operator Perform the operation, and place the result back on the stack Repeat until the end of expression

(ex) Postfix evaluation 6 2 / 3 – 4 2 * + Token Stack Top [0] [1] [2] 6 2 1 / 6/2 3 - 6/2-3 4 * 4*2 + 6/2-3+4*2

(ex) Function to evaluate a postfix expression int eval(void) { precedence token; char symbol; int op1, op2; int n = 0; /* counter for the expression string */ int top = -1; token = get_token(&symbol, &n); while(token != eos) { if(token == operand) add(&top, symbol-’0’); /* stack insert */ else { /* remove two operands, perform operation, and return result to the stack */ op2 = delete(&top); /* stack delete */ op1 = delete(&top); switch(token) { case plus: add(&top, op1+op2); break; case minus: add(&top, op1-op2); case times: add(&top, op1*op2);

(ex) Function to evaluate a postfix expression (cont’d) case divide: add(&top, op1/op2); break; case mod: add(&top, op1%op2); } token = get_token(&symbol, &n); return delete(&top); /* return result */

(ex) Function to get a token from the input string precedence get_token(char *symbol, int *n) { *symbol = expr[(*n)++]; switch(*symbol) { case ‘(’: return lparen; case ‘)’: return rparen; case ‘+’: return plus; case ‘-’: return minus; case ‘/’: return divide; case ‘*’: return times; case ‘%’: return mod; case ‘ ’: return eos; default : return operand; /* no error checking, default is operand */ }

Infix to postfix Fully parenthesize the expression Move all binary operators so that they replace their corresponding right parentheses Delete all parentheses e.g., a/b-c+d*e-a*c ((((a/b)-c)+(d*e))-(a*c)) ab/c-de*+ac*-

(ex) Simple expression Translation of a + b * c to postfix The operands are output immediately, but two operators need to be reversed Operators are stacked until precedence(operator at the top of the stack) < precedence(incomming operator) Token Stack Top Output [0] [1] [2] a -1 + b ab * 1 c abc eos abc*+

In-stack precedence (isp), incoming precedence (icp) Left-parenthesis placed on the stack whenever it is found in the expression (i.e. highest precedence) unstacked only when its matching right parenthesis is found (i.e. lowest precedence) In-stack precedence (isp), incoming precedence (icp) precedence stack[MAX_STACK_SIZE]; /* isp and icp arrays – index is value of precedence lparen, rparen, plus, minus, times, divide, mod, eos */ static int isp[] = { 0, 19, 12, 12, 13, 13, 13, 0 } static int icp[] = { 20, 19, 12, 12, 13, 13, 13, 0 } Analysis of postfix: Θ(n), where n is the number of tokens in the expression

(ex) Function to convert from infix to postfix void postfix(void) { /* output the postfix of the expression. The expression string, the stack, and top are global */ char symbol; precedence token; int n = 0; int top = 0; /* place eos on stack */ stack[0] = eos; for(token = get_token(&symbol, &n); token != eos; token = get_token(&symbol, &n)) { if(token == operand) printf(“%c”, symbol); else if(token == rparen) { /* unstack tokens until left parenthesis */ while(stack[top] != lparen) print_token(delete(&top)); delete(&top); /* discard the left parenthesis */ }

(ex) Function to convert from infix to postfix (cond’t) else { /* remove and print symbols whose isp is greater than or equal to the current token’s icp */ while(isp[stack[top]] >= icp[token]) print_token(delete(&top)); add(&top, token); } while((token = delete(&top)) != eos) print_token(token); printf(“\n”);

(ex) Parenthesized expression Translation of a*(b+c)*d to postfix Token Stack Top Output [0] [1] [2] a -1 * ( 1 b ab + 2 c abc ) abc+ abc+* d abc+*d eos abc+*d*