Stack Applications - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

Some concept of Data Structures and Algorithm are Permutation, Representation, Implemented, Algorithm Design, Dynamic Programming, Graph Data Structures, String Processing, General Trees. Main points of this lecture are: Stack Applications, Postfix Conversion, Evaluation, Expressions, Generate, Machine Instructions, Corresponding, Precedence Rule, Complementunary, Minus

Typology: Slides

2012/2013

Uploaded on 04/27/2013

shareeka_555
shareeka_555 🇮🇳

4

(6)

74 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures &
Algorithm Analysis
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Stack Applications - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structures &

Algorithm Analysis

Applications

  • Infix to Postfix conversion [Evaluation of Expressions]

Token Operator Precedence 1 Associativity ( ) [ ] ->. function call array element struct or union member 17 left-to-right -- ++ increment, decrement 2 16 left-to-right -- ++ !

& * sizeof decrement, increment 3 logical not one’s complement unary minus or plus address or indirection size (in bytes) 15 right-to-left (type) type cast 14 right-to-left

  • / % mutiplicative 13 Left-to-right
    • binary add or subtract 12 left-to-right << >> shift 11 left-to-right

= < <= relational 10 left-to-right == != equality 9 left-to-right & bitwise and 8 left-to-right ^ bitwise exclusive or 7 left-to-right bitwise or 6 left-to-right && logical and 5 left-to-right ξξ logical or^4 left-to-right

Infix Postfix 2+3* ab+ (1+2) ab/c (a/(b-c+d))(e-a)c a/b-c+de-a*c

ab5+ 12+7 abc/ abc-d+/ea-c* ab/c-deac- user compiler Postfix: no parentheses, no precedence

Token Stack [0] [1] [2] Top 6 2 / 3 - 4 2 * + 6 6 2 6/ 6/2 3 6/2- 6/2-3 4 6/2-3 4 2 6/2-3 4* 6/2-3+4*

int eval(void) { /* evaluate a postfix expression, expr, maintained as a global variable, ‘\0’ is the the end of the expression. The stack and top of the stack are global variables. get_token is used to return the token type and the character symbol. Operands are assumed to be single character digits / 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) push(&top, symbol-’0’); / stack insert */

Evaluation of Postfix Expressions

else { /* remove two operands, perform operation, and return result to the stack / op2 = pop(&top); / stack delete / op1 = pop(&top); switch(token) { case plus: push(&top, op1+op2); break; case minus: push(&top, op1-op2); break; case times: push(&top, op1op2); break; case divide: push(&top, op1/op2); break; case mod: push(&top, op1%op2); } } token = get_token (&symbol, &n); } return pop(&top); /* return result */ } Docsity.com

case ‘/’ : return divide; case ‘’ : return times; case ‘%’ : return mod; case ‘\0‘ : return eos; default : return operand; / no error checking, default is operand */ } }

Infix to Postfix Conversion

(Intuitive Algorithm) (1) Fully parenthesized expression a / b - c + d * e - a * c --> ((((a / b) - c) + (d * e)) – (a * c)) (2) All operators replace their corresponding right parentheses. ((((a / b) - c) + (d * e)) – (a * c)) (3) Delete all parentheses. ab/c-de+ac- two passes /

  • (^) *

Token Stack [0] [1] [2] Top Output a * 1 ( b + c ) * 2 d eos

1

1

1

1

1

a a a ab ab abc abc+ abc+* 1 abc+* 1 d abc+* 1 d* 2 a * 1 (b +c) * 2 d match )

= * 2

(1) Operators are taken out of the stack as long as their in-stack precedence is higher than or equal to the incoming precedence of the new operator. (2) ( has low in-stack precedence, and high incoming precedence. ( ) + - * / % eos isp 0 19 12 12 13 13 13 0 icp 20 19 12 12 13 13 13 0

Rules

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 ){

Infix to Postfix

/*unstack tokens until left parenthesis / while (stack[top] != lparen) print_token(delete(&top)); pop(&top); /discard the left parenthesis / } 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)); push(&top, token); } } while ((token = pop(&top)) != eos) print_token(token); print(“\n”);

Infix to Postfix (cont’d)