19
Applications of STACK Presented By:- Yadraj Meena K.V. INS KALINGA

Application of Stack - Yadraj Meena

Embed Size (px)

Citation preview

Page 1: Application of Stack - Yadraj Meena

Applications of STACK Presented By:-

Yadraj MeenaK.V. INS KALINGA

Page 2: Application of Stack - Yadraj Meena

STACK In computer science, a stack is an abstract data

type based on principle of(LIFO) that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element.

Page 3: Application of Stack - Yadraj Meena

Functions necessary to implement a stack :#include<iostream.h>#define STACK_SIZE 20int stack[STACK_SIZE]; /*space for stacking

integers*/int top=-1; / *top_of_stack is

defined as global

variable for a global stack *//*Function to check whether the stack is ‘full’ */int stack_full(){ if(top==STACK_SIZE-1) return(1); else return(0);}

Page 4: Application of Stack - Yadraj Meena

/* Function to check whether the stack is ‘empty’ */int stack_empty(){ if(top==-1) return(1); else return(0);}/*Function to push or add an element on the stack.*/void push(int number){stack[++top]=number; /*add element on top of

stack */}

Page 5: Application of Stack - Yadraj Meena

/* Function to pop an element from the stack*/int pop(){ int number; number=stack[top]; /*remove top element from stack */ top--; return(number);}

Page 6: Application of Stack - Yadraj Meena

1. INFIX TO POSTFIX CONVERSION2. EVALUATION OF POSTFIX EXPRESSION

APPLICATIONS OF STACK

Page 7: Application of Stack - Yadraj Meena

Infix notation is the common arithmetic and logical

formula notation, in which operators are written infix-

style between the operands they act on

E.g. A + B

Infix notation

Page 8: Application of Stack - Yadraj Meena

In Postfix notation, the operator comes after the

Operand.

For example, the Infix expression A+B will be written

as AB+ in its Postfix Notation.

Postfix is also called ‘Reverse Polish Notation’ 

Postfix notation

Page 9: Application of Stack - Yadraj Meena

In Prefix notation, the operator comes before the

operand.

The Infix expression A+B will be written as +AB in its

Prefix Notation.

Prefix is also called ‘Polish Notation’ 

Prefix notation

Page 10: Application of Stack - Yadraj Meena

ARITHMETIC OPERATORS PrecedenceExponentiation( ^ or ↑ ) HIGHESTMultiplication and Division ( * , / ) MIDDLEAddition and Subtraction ( + , - ) LOWESTLOGICAL OPERATORSNOT HIGHESTAND MIDDLEOR LOWEST

Precedence of operators

Page 11: Application of Stack - Yadraj Meena

Algorithm: Infix to Postfix conversion1. Enclose the expression in parentheses, that is, ( ).2. Read next symbol of expression and repeat step 3 to 6 until

STACK is empty.3. If the symbol is operand add to Postfix Expression4.If the symbol read is ‘(‘ then push it into STACK.5. If symbol read = operator then (1) Repeat while ( Precedence of TOP(STACK) >= precedence of operator read) { POP operator from STACK and add operator to PE} (2) Push operator into STACK6. If the symbol read is ‘)’ then (1) Repeat while( TOP[Stack] != ‘(‘ ) { POP operator from stack and add to PE}

(2) Remove the ‘(‘ . [ it must not be added to PE ]7. PE is the desired equivalent Postfix Expression8. End

Page 12: Application of Stack - Yadraj Meena

Example: Conversion infix to Postfix: ( A + B * ( C – D ) / E )( A B+ (* -C /)D )E

(A ( A+ (+ AB (+ AB* (+* AB( (+*( ABC (+*( ABC- (+*(- ABCD (+*(- ABCD) (+* ABCD-/ (+/ ABCD-*E (+/ ABCD-*E) ABCD-*E/+

Scanned Element STACK OUTPUT(PE)(

OUTPUT : ABCD-*E/+

Page 13: Application of Stack - Yadraj Meena

Convert infix to Postfix: ( A * B + ( C – D / E ))

Solve:

Page 14: Application of Stack - Yadraj Meena

Example :Conversion infix to Postfix: ( A * B + ( C – D / E ))( A * +B C( D- F/ ) )Scanned Element STACK OUTPUT(PE)( (A ( A* (* AB (* AB+ (+ AB*( (+( AB*C (+( AB*C- (+(- AB*C

D (+(- AB*CD/ (+(-/ AB*CDF (+(-/ AB*CDF) (+ AB*CDF/-) AB+CDF/-+

OUTPUT : AB+CDF/-+

Page 15: Application of Stack - Yadraj Meena

Evaluation of Postfix Expression Algorithm Steps1. Create an empty stack STACK2. Read next symbol of the Postfix expression PE and repeat

steps 3 and 4 until end of the expression is reached.3. If(symbol read = operand) then PUSH(STACK , Symbol read)4. If(symbol read = operator) then { if(operator = unary operator) { POP(Stack, symbol) Evaluate the expression so formed and PUSH the result onto STACK } else { POP(STACK, symbol1)

POP(STACK, symbol 2) Evaluate result = symbol2 operator symbol1

PUSH(STACK, result) } }5. POP(STACK, result)6. End

Page 16: Application of Stack - Yadraj Meena

Evaluate: 50 , 60 , + , 20 , 10 , - , *

OUTPUT : 1100

ScannedElement

Operation Stack Status

50 Push 5060 Push 50, 60+ Pop twice, 50 + 60 = 110

Push result110

20 Push 110, 2010 Push 110,20,10- Pop twice, 20 – 10 =10

Push result 110,10

* Pop twice, 110 * 10 = 1100Push result

1100

Page 17: Application of Stack - Yadraj Meena

Evaluate the expression: True, False, NOT, AND ,False ,True ,OR

, AND

Page 18: Application of Stack - Yadraj Meena

Evaluate: True, False, NOT, AND ,False ,True ,OR , AND

OUTPUT: True

ScannedElement

Operation Stack Status

True Push TrueFalse Push True, FalseNOT Pop one element and apply NOT operation

And Push the result into stackTrue, True

AND True

False Push True, FalseTrue Push True, False

TrueOR True, True

AND True

Pop one element and apply AND operationAnd Push the result into stack

Pop one element and apply OR operationAnd Push the result into stackPop one element and apply AND operationAnd Push the result into stack

Page 19: Application of Stack - Yadraj Meena

THANKS….. YOU