+ All Categories
Home > Documents > Satck Lect 5

Satck Lect 5

Date post: 03-Jun-2018
Category:
Upload: tanmay-baranwal
View: 229 times
Download: 0 times
Share this document with a friend

of 21

Transcript
  • 8/12/2019 Satck Lect 5

    1/21

    Satck

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    2/21

    Stack- A stack is a linear data structure in which items may be addedor removed only at one end . Accordingly, stacks are also called last-in-first-out or LIFO lists. The end at which element is added orremoved is called the top of the stack . Two basic operationsassociated with stacks are :

    Push - Term used to denote insertion of an element onto a stack.

    Pop - Term used to describe deletion of an element from a stack.

    The order in which elements are pushed onto a stack is reverse ofthe

    order in which elements are popped from a stack

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    3/21

    Representation of stacks Stacks may be represented in memory in various ways usually by

    means of one-way list or a linear array

    In array representation of stack, stack is maintained by an arraynamed STACK , a variable TOP which contains the location/index oftop element of stack and a variable MAXSTK giving the maximumnumber of elements that can be held by the stack. The conditionTOP=0 or TOP=NULL indicates that stack is empty.

    The operation of addition of an item on stack and operation ofremoving an item from a stack may be implemented respectively bysub algorithms, called PUSH and POP . Before executing operationPUSH on to a stack, one must first test whether there is room in the

    stack for the new item. If not, then we have the condition known asoverflow. Analogously, in executing the POP operation, one must firsttest where there is an element in stack to be deleted. If not, then wehave condition known as underflow.

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    4/21

    ARRAY

    IMPLEMENTATION OFSTACK

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    5/21

    Algorithm: PUSH (STACK,TOP,MAXSTK,ITEM)This algorithm pushes an item onto the stack array. TOP

    stores the index of top element of the stack and MAXSTK

    stores the maximum size of the stack. Step 1: [Stack already filled]

    If TOP=MAXSTK, then:

    Write: OVERFLOW Return

    Step 2: Set TOP:=TOP+1 Step 3: Set STACK[TOP]:=ITEM Step 4: Return

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    6/21

    Algorithm: POP(STACK,TOP,ITEM)

    This procedure deletes the top element ofSTACK array and assign it to variable ITEM

    Step 1: If TOP=0,then:Write: UNDERFLOW

    Return Step 2: Set ITEM:=STACK[TOP] Step 3: Set TOP:=TOP-1

    Step 4: Return

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    7/21

    A stack represented using a linked list is also known as linked stack.The array based representation of stack suffers from followinglimitations:

    Size of stack must be known in advance Representing stack as an array prohibits the growth of stack beyond

    finite number of elements.

    In a linked list implementation of stack, each memory cell willcontain the data part of the current element of stack and pointer thatstores the address of its bottom element and the memory cellcontaining the bottom most element will have a NULL pointer

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    8/21

    Push operation on linked list representation of stack Algorithm: PUSH(INFO, LINK, TOP, ITEM, AVAIL)

    This algorithm pushes an element to the top of the stackStep 1: If AVAIL=NULL, then

    Write: OVERFLOW Return

    Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]Step 3: Set INFO[NEW]:=ITEM

    Step 4: If TOP=NULL, thenSet LINK[NEW]:=NULLSet TOP:=NEWReturn

    Else:Set LINK[NEW]:=TOPSet TOP:=NEW

    Step 5: Return

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    9/21

    POP operation on linked list representation of stack Algorithm: POP(INFO, LINK, TOP, AVAIL) This algorithm deletes an element from the top of the

    stack Step 1: If TOP=NULL , then:

    Write: UNDERFLOLW

    Return Step 2: Set PTR:=TOP

    Set TOP:=LINK[TOP]Write: INFO[PTR]

    Step 3: Set LINK[PTR]:=AVAIL and AVAIL:=PTR Step 4: Return

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    10/21

    Application of stack Evaluation of arithmetic expression. For most common arithmetic operations, operator symbol is placed

    between its operands. This is called infix notation of an expression. To usestack to evaluate an arithmetic expression, we have to convert theexpression into its prefix or postfix notation.

    Polish notation , refers to the notation in which operator symbol is placedbefore its two operands. This is also called prefix notation of an arithmetic

    expression. The fundamental property of polish notation is that the order inwhich operations are to be performed is completely determined bypositions of operators and operands in expression. Accordingly, one neverneeds parentheses when writing expression in polish notation.

    Reverse Polish notation refers to notation in which operator is placed after

    its two operands. This notation is frequently called postfix notation .Examples of three notations are:

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    11/21

    INFIX NOTATION: A+B PREFIX OR POLISH NOTATION: +AB POSTFIX OR REVERSE POLISH NOATATION: AB+

    Convert the following infix expressions to prefix and postfix forms A+(B*C) (A+B)/(C+D) Prefix: +A*BC Postfix: A BC*+ Prefix: / +AB+CD Postfix: AB+CD+/

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    12/21

    The computer usually evaluates an arithmetic expression written in infixnotation in two steps.

    Converts expression to postfix notation

    Evaluates the postfix notation.

    Evaluation of postfix expression Suppose P is an arithmetic expression written in postfix notation. The

    following algorithm which uses a STACK to hold operands andevaluates P

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    13/21

    Algorithm: This algorithm finds the VALUE of an arithmeticexpression P written in Postfix notation.

    Step 1: Add a right parentheses ) at the end of P Step 2: Scan P from left to right and repeat step 3 and 4 for each element

    of P until ) is encountered Step 3: If an operand is encountered, put the operand on the stackStep 4: If an operator is encountered , then:

    (a) Remove the two top elements of stack, where A is top element

    and B is next-top-element.(b) Evaluate B A(c ) Place the result of (b) back on stack[End of if structure][End of step 2 loop]

    Step 5: Set VALUE equal to the top element of stackStep 6: Exit

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    14/21

    Transforming Infix Expression into Postfix Expression The algorithm uses a stack to temporarily hold operators and left

    parentheses. The postfix expression P will be constructed from left to rightusing operands from Q and operators which are removed from STACK. Thealgorithm begins by pushing a left parentheses onto stack and adding aright parentheses at the end of Q

    Algorithm: POSTFIX (Q, P)Suppose Q is an arithmetic expression written in infixnotation. This algorithm finds the equivalent postfix

    expression PStep 1: Push ( on to the STACK and add ) to the end of Q Step 2: Scan Q from left to right and repeat step 3 to 6 for each element

    of Q until the STACK is empty

    Step 3: If an operand is encountered, add it to PStep 4: If left parentheses is encountered, add it to STACK

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    15/21

    Step 5: If an operator is encountered, then:(a) Repeatedly pop from STACK and add to P each operator

    which has same precedence or higher precedence than

    (b) Add to STACKStep 6: If a right parentheses is encountered , then:

    (a) Repeatedly pop from STACK and add to P each operator until

    a left parentheses is encountered

    (b) Remove the left parentheses[End of Step 2 Loop]

    Step 7: Exit

    C#ODE Studio || codstudio.wordpress.com

    E l C t Q A+(B*C) / D) t it di g tfi f

  • 8/12/2019 Satck Lect 5

    16/21

    Example: Convert Q=A+(B*C) / D) to its corresponding postfix form Solution: put ) at the end of Q and put ( on stack Starting from left: Operand A , put it on P Operator + move to stack as no operator there ( move on stack Operand B, put it on P Operator * , move to stack as no operator Operand C , move to P ) , pop from stack and put on P until ( is encountered. Pop ( also operator /, as precedence of / is higher than + on stack, no pop

    possible. Push / on stack Operand D , put it on P Right parentheses ), Pop all the elements and add the P until ( is

    encountered. Also remove ( from stack

    P= A B C* D / +

    C#ODE Studio || codstudio.wordpress.com

    +

    (

    (

    *

    +

    (

    +

    (

    /

    f f f

  • 8/12/2019 Satck Lect 5

    17/21

    Transforming Infix Expression into Prefix Expression Algorithm: [Polish Notation] PREFIX (Q, P)

    Suppose Q is an arithmetic expression written in infixnotation. This algorithm finds the equivalent prefix

    expression PStep 1: Reverse the input stringStep 2: Examine the next element in the inputStep 3: If it is operand, add it to output string

    Step 4: If it is closing parentheses, push it on stackStep 5: If it is operator, then:(i) if stack is empty, push operator on stack(ii) if top of stack is closing parentheses, push operator on the

    stack(iii) If it has same or higher priority than top of stack, push

    operator on stackElse pop the operator from the stack and add it to outputstring, repeat step 5

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    18/21

    C id h f ll i i h i i P i i fi

  • 8/12/2019 Satck Lect 5

    19/21

    Consider the following arithmetic expression P written in postfixnotationP: 12, 7, 3 -, /, 2, 1, 5, +, *, +

    (a) Translate P, by inspection and hand, into its equivalent infixexpression

    (b) Evaluate the infix expressionSol: (a) Scanning from left to right, translate each operator from postfix

    to infix notation

    P = 12, [7-3], /, 2, 1, 5, +, *, += [12/[7-3]],2, [1+5],*,+= 12/(7-3)+2*(1+5)

    (b) 12/(7-3)+2*(1+5)

    = [3],[2*6],+= 3+12= 15

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    20/21

    Practical applications of stack

    Stacks are used for implementing function calls in a program Used for implementing recursion. Used for conversion of infix expression to its postfix or prefix form Used for evaluation of postfix expression. Used in sorting of arrays (quicksort and mergesort technique)

    C#ODE Studio || codstudio.wordpress.com

  • 8/12/2019 Satck Lect 5

    21/21

    References

    Dinesh Mehta and Sartaj Sahni Handbook of Data Structuresand Applications , Chapman and Hall / CRC Press, 2007.

    Niklaus Wirth , Algorithms and Data Structures , Prentice Hall ,1985.

    Diane Zak, Introduction to programming with c++, copyright2011 Cengage Learning Asia Pte Ltd

    Schaumm Series ,McGraw Hill.

    C#ODE Studio || codstudio.wordpress.com

    http://en.wikipedia.org/wiki/Sartaj_Sahnihttp://en.wikipedia.org/wiki/Chapman_and_Hallhttp://en.wikipedia.org/wiki/CRC_Presshttp://en.wikipedia.org/wiki/Niklaus_Wirthhttp://en.wikipedia.org/wiki/Prentice_Hallhttp://en.wikipedia.org/wiki/Prentice_Hallhttp://en.wikipedia.org/wiki/Niklaus_Wirthhttp://en.wikipedia.org/wiki/CRC_Presshttp://en.wikipedia.org/wiki/Chapman_and_Hallhttp://en.wikipedia.org/wiki/Sartaj_Sahni

Recommended