stack.ppt

Embed Size (px)

Citation preview

  • 7/27/2019 stack.ppt

    1/16

  • 7/27/2019 stack.ppt

    2/16

    Stack Stack is the list of elements in

    which an element may beinserted or deleted only at oneend, called the TOP of the stack.

    New nodes can be added andremoved only at the top.

    Similar to a pile of dishes. Last-in, first-out (LIFO).

    Eg: A stack of books.

    When a person wear bangles thelast bangle worn is the first one to

    be removed and the first banglewould be the last to be removed.

  • 7/27/2019 stack.ppt

    3/16

    Special terminology used for two basic operationsassociated with stacks:

    PUSH is the term used to insert an element into astack

    POP is the term used to delete an element from astack.

  • 7/27/2019 stack.ppt

    4/16

    Array/Static representation of STACKSx y z

    1 2 4 5 6 7 8 93

    TOP 3MAXSTK 9

  • 7/27/2019 stack.ppt

    5/16

    Push operationPUSH(STACK,TOP,MAXSTK,ITEM)

    1. IF TOP=MAXSTK then print OVERFLOW and return

    2.Set TOP=TOP+1[increase top by one]

    3. Set STACK[TOP]=ITEM, Increase ITEM in new TOPposition

    4. return

  • 7/27/2019 stack.ppt

    6/16

    POP operation POP(STACK,TOP,ITEM)

    1. IF TOP=0, PRINT underflow and return

    2. Set ITEM=STACK[TOP] [Assign top element toitem]

    3.Set TOP=TOP-1 [Decrease TOP By one]

    4. Return

  • 7/27/2019 stack.ppt

    7/16

    Linked List/ Dynamic representation of StackPUSH_LINKSTACK(INFO,LINK,TOP,AVAIL, ITEM)

    1. If AVAIL=NULL , then write OVERFLOW and exit.

    2. [Remove first node from AVAIL list]

    Set NEW=AVAIL and AVAIL=LINK[AVAIL].

    3.Set INFO[NEW]=ITEM [Copies ITEM into new node]

    4. Set LINK[NEW]=TOP[New node points to the original

    TOP node in the stack]5. Set TOP=NEW[Reset TOP to point to the new node at

    the TOP of the stack]

    6.Exit

  • 7/27/2019 stack.ppt

    8/16

    POP_LINKSTACK(INFO,LINK,TOP,AVAIL,ITEM)

    1.If TOP=NULL then write UNDERFLOW and exit.

    2. Set ITEM=INFO[TOP] [Copies the TOP element of stack

    into ITEM] 3. Set TEMP=TOP andTOP=LINK[TOP]

    [Remember the old value of the Top pointer in TEMP andreset TOP to point to the next element in the stack]

    4.[Return delete node to the AVAIL list] Set LINK[TEMP]=AVAIL and AVAIL=TEMP

    5. Exit

  • 7/27/2019 stack.ppt

    9/16

    Infix notation: In this operator is placed inbetween two operands. Eg (A+B)*C

    Polish/prefix notation: In this operatorsymbols are placed before two operands. Eg:+AB, *EF

    Reverse Polish/postfix /suffix notation:Operator symbol is placed after its twooperands. Eg AB*, CD/

  • 7/27/2019 stack.ppt

    10/16

    Transforming Infix Expression

    into prefix/polish notation Eg1. Infix expressing: (A+B)*C

    [+AB]*C

    Now [+AB] will act as operand *+ABC

    Eg2. Infix expressing: A+(B*C)

    A+[*BC]

    Now [*BC] will act as operand

    +A*BC

  • 7/27/2019 stack.ppt

    11/16

    Calculate prefix notation of following infix expression

    (A+B)/(C-D)

  • 7/27/2019 stack.ppt

    12/16

    Evaluation of Postfix expression P: 5,6,2,+,*,12,4,/,-

    Symbol Scanned STACK

    5 56 5,6

    2 5,6,2

    + 5,8

    * 40

    12 40, 12

    4 40,12,4

    / 40,3

    - 37

    )

  • 7/27/2019 stack.ppt

    13/16

    Transforming Infix expression to

    postfix expression Table is divided into 3 parts symbol scanned(Q), STACK, expression P

    1.Push ( onto stack and add ) to the end of Q

    2. Ifoperand is encountered in symbol scanned (Q) , add it to P. Eg

    A,B,C.. 3. If left parenthesis ( is encountered, push it on to STACK

    4. Ifoperator is encountered:

    A) Push operator into the stack if it has lower precedence than that ofnext operator.

    B) Add operator to P if it has equal or higher preference than that ofnext operator.

    Eg: +* : + has lower precedence than * so + will remain in stack

    Eg: *+ : * has higher precedence than + so * will get added to P.

  • 7/27/2019 stack.ppt

    14/16

    Transforming Infix expression to

    postfix expression Q: A+(B*C-(D/E^F)*G)*H

    Symbol Scanned STACK Expression P

    A+(B*C

    -(D/E^

    F

    ((+(+((+((+(*(+(*

    (+(-(+(-((+(-((+(-(/(+(-(/(+(-(/^

    (+(-(/^+ -

    AAAABABABC

    ABC*ABC*ABC*DABC*DABC*DEABC*DE

    ABC*DEFABC*DEF^

  • 7/27/2019 stack.ppt

    15/16

    Q: A*b+(C+D)-(A/B)/C

  • 7/27/2019 stack.ppt

    16/16