2.mod 2-2 stack

  • Upload
    robinpt

  • View
    227

  • Download
    0

Embed Size (px)

DESCRIPTION

2.mod 2-2 stack

Citation preview

  • STACK

  • STACKStack is a ordered collection of homogeneous elements where insertion and deletion take place at one end only.

    stack = last-in, first-out (LIFO) list; A stack grows by adding an item to its top, and shrinks by removing an item from its top, so the last item added to (or push) the stack is always the first to be removed (or pop).

    Stack is a linear data structure.

    An element of the stack is termed as ITEM.Maximum number of elements of the stack can be termed as SIZE.Main two operations are PUSH and POP.Stack pointer(SP) points to the top of the stack.

  • IMPLEMENTING STACK

    By using Single Dimensional ArrayBy using Single linked list

  • Operations on the stackPUSH: Insertion Storing the element to the stack to the location SP points to after incrementing the SP.

    POP: DeletionDeleting one item from the top of the stack.

    ISEMPTYReturns true or false after checking the stack is empty or not.

    ISFULLReturns true or false after checking the stack is full or not.

    TRAVERSEDisplay all elements in the stack

  • STACK1234TOPIndex 3Index 2Index 1Index 0

  • STACK : push & pop

    PUSHTo insert an item into the stackThe index of the stack varies from 0 to SIZE-1TOP point to the current top most item in the stack

    POPThis operation is the remove an item from the top of the stack

  • STACK : pushPUSH 11TOP

  • STACK : pushPUSH 221TOP

  • STACK : pushPUSH 3312TOP

  • STACK : pushPUSH 44123TOP

  • STACK : pushPUSH 551234TOP

  • STACK : pushPUSH 551234STACK OVERFLOW

  • Algorithm: STACK_pushAlgorithm Stack_push(ITEM)Input: An ITEM to insert in to the stackOutput: ITEM on top of the stack if successfulData structure: A Stack implemented using arraySteps:If TOP>=SIZE-1 thenPrint Stack is full..No insertion is possibleExitElse TOP=TOP+1A[TOP]=ITEMEndIfStop

  • STACK : popPOP1234TOP

  • STACK : pop1234TOP

  • STACK : popPOP123TOP

  • STACK : pop123TOP

  • STACK : popPOP12TOP

  • STACK : pop12TOP

  • STACK : popPOP1TOP

  • STACK : pop1TOP

  • STACK : popPOPSTACKUNDERFLOWTOP

  • Algorithm: STACK_pop Algorithm Stack_popInput: A stack Output: An element is removed from top of the stack if it is not emptyData Structure: A stack implemented using array

    Steps:If TOP

  • Algorithm: STACK_statusAlgorithm: Stack_statusInput: A stack Output: states whether stack is empty or full, available free space, item at TOPData Structure: A stack implemented using array

    Steps:If TOP=size-1Print stack fullElsePrint element on top is , A[TOP]FREE = (SIZE TOP-1)/SIZE *100Print percentage of free stack is , freeEnd ifEnd ifstop

  • Multiple StackIn several application, more than one stack may be used together.One stack may be empty while other overflows.Two stacks may be implemented using a single array whose SIZE = SIZE1+ SIZE2 One stack can grow from one end, while the other grows from the other end Basic operations for these stacks are same to the normal stack.

    TOPxTOPySTACKxSTACKy

  • Algorithm : Multiple stack_pushAlgorithm Stack_push(ITEM,NO)Input: An ITEM to insert Output: ITEM on top if successfulData structure: A Stack , A[MAX]

    Steps:

    If NO=1If TOP1>=SIZE1-1 thenPrint Stack is full..No insertion is possibleExitElse TOP1=TOP1+1A[TOP1]=ITEMEndIfElseIf NO=2If TOP2

  • Algorithm : Multiple stack_popAlgorithm MStack_pop(NO)Input: A stack Output:element removed from top Data Structure: A stack, A[MAX]

    Steps:If NO=1If TOP1MAXPrint Stack1 is empty..No POPExitElseITEM=A[TOP2]TOP2=TOP2+1EndIfEnd ifStop

  • Applications of stack

    String ReversalInfix to postfix conversionPostfix evaluation

  • Application: String ReversalInputOutput

    HELLO

  • Application: String ReversalInputOutputstackTOP

    HELLO

  • Application: String ReversalInputOutputstackTOP

    HELLO

  • Application: String ReversalInputOutputstackTOPH

    HELLO

  • Application: String ReversalInputOutputstackTOPHE

    HELLO

  • Application: String ReversalInputOutputstackTOPHEL

    HELLO

  • Application: String ReversalInputOutputstackTOPHELL

    HELLO

  • Application: String ReversalInputOutputstackTOPHELLO

    HELLO

  • Application: String ReversalInputOutputstackTOPHELLOInput String completeSo start popping

    HELLO

  • Application: String ReversalInputOutputstackTOPHELL

    HELLO

    O

  • Application: String ReversalInputOutputstackTOPHEL

    HELLO

    OL

  • Application: String ReversalInputOutputstackTOPHE

    HELLO

    OLL

  • Application: String ReversalInputOutputstackTOPH

    HELLO

    OLLE

  • Application: String ReversalInputOutputstackTOPSTACK EMPTY

    HELLO

    OLLEH

  • Applications of stack String ReversalInfix to postfix conversionPostfix evaluation

    What is prefix, infix and postfix ?

  • NotationsThree types of notation:Infix notation: operator is written between the operands (A+B)Prefix notation: operator is written before the operands, also called polish notation (+AB)Postfix notation: operator is written after operands, also known as suffix or reverse Polish notation (AB+)

    Advantages of using postfix notation:Human beings are quite used to work with infix notation, but infix is much complex and required to remember set of rules. (E.g. precedence and associativity) Postfix is much easy to work, and no need for operator precedence and other rules.

  • Infix to postfix expressionQ-> arithmetic expression in infix notation

    The following algorithm uses a stack to temporarily hold operators and left parenthesis.

    Postfix expression P will be constructed from left to right using the operands in Q and operators which are removed from STACK

  • Algorithm Push ( onto STACK and add ) to the end of QScan Q from left to right and repeat steps 3 to 6 for each element of Q until the STACK is empty:If an operand is encountered, add it to PIf a left parenthesis is encountered, push it onto STACK

  • 5) If an operator # is encountered, then a) Repeatedly pop from stack and which has same or higher precedence than #add to P each operator b) Add # to STACK6) If a right parenthesis is encountered, then a) Reapeatedly pop from STACK and add to P each operator until a left parenthesis is encountered b) Remove the left parenthesis. [Do not add ( to P] 7) exit Algorithm

  • Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form, So, the Postfix Expression is 23*21-/53*+

    2Empty2

    **2

    3*23

    //23*

    (/(23*

    2/(23*2

    -/(-23*2

    1/(-23*21

    )/23*21-

    ++23*21-/

    5+23*21-/5

    3+*23*21-/53

    ExpressionStackOutput

    *+*23*21-/53

    Empty23*21-/53*+

  • infix to postfixQuestion: convert A-B/(C*D^E)

  • infix to postfixEasy method:manual conversion

    ( A - ( B / ( C * ( D ^ E ) ) ) )

  • infix to postfixEasy method: manual conversion( A - ( B / ( C * ( D ^ E ) ) ) )( A - ( B / ( C * D E ^) ) )( A - ( B / ( C * D E ^) ) )( A - ( B / C D E ^* ) )( A - ( B / C D E ^* ) )( A - B C D E ^* / )( A - B C D E ^* / ) A B C D E ^* / -

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expression

    A-B/C^D*E

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP

    A-B/C^D*E

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP

    A-B/C^D*E

    A

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-

    A-B/C^D*E

    A

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-

    A-B/C^D*E

    AB

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-Topstack has less precedence so push to stack/

    A-B / C^D*E

    AB

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-/

    A-B / C^D*E

    ABC

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-Topstack has less precedence so push to stack/^

    A-B / C^D*E

    ABC

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-/^

    A-B / C^D*E

    ABCD

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-Topstack has more precedence so POP stack/^

    A-B / C^D*E

    ABCD

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-/Topstack has more precedence so POP stack

    A-B / C^D*E

    ABCD^

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-Topstack has less precedence so PUSH to stack*

    A-B / C^D*E

    ABCD^/

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-*

    A-B / C^D*E

    ABCD^/E

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-*Reached end of string, so POP till stack is empty

    A-B / C^D*E

    ABCD^E

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOP-

    A-B / C^D*E

    ABCD^/E*

  • infix to postfixImplementation :

    Input- Infix expressionOutput- Postfix expressionTOPStack emptyConversion complete

    A-B / C^D*E

    ABCD^/E*-

  • infix: A - B / C ^ D * EPostfix: A B C D ^ / E * -

  • Precedence Table

    symbolIn-stackPriority valueIn-comingPriority value+ -21* /43^56operand87(09)-0

  • Postfix expression EvaluationPostfix expression: 53*2+

  • Postfix expression EvaluationPostfix expression: 53*2+5

  • Postfix expression EvaluationPostfix expression: 53*2+53

  • Postfix expression EvaluationPostfix expression: 53*2+53

  • Postfix expression EvaluationPostfix expression: 53*2+53

  • Postfix expression EvaluationPostfix expression: 53*2+5*3

  • Postfix expression EvaluationPostfix expression: 53*2+15

  • Postfix expression EvaluationPostfix expression: 53*2+152

  • Postfix expression EvaluationPostfix expression: 53*2+15+2

  • Postfix expression EvaluationPostfix expression: 53*2+17

  • Postfix expression EvaluationPostfix expression: 53*2+17Expression completePop the final result

  • 53*2+ = 17

  • Postfix expression EvaluationEvaluation of postfix expression also maintains stack but here stack contains the operands instead of operators.

    Scan the postfix from left to rightIf the symbol is operand, push it into the stack.If the symbol is operator, pop last two elements of stack and evaluate, after evaluation result push into the stack.Continue this process until the end of the expression.Finally pop the final result from the stack.

  • Algorithm: Postfix evaluationAlgorithm POSTFIX-EVALInput: An expression in postfix formOutput: Result of the expressionData Structure: A Stack to store the operand and intermediate result and an arraySteps:Postfix=READ(expression)Length=STRLENGTH(expression),i=0While(i
  • Assignment QuestionsGive an application of stack data structure. String ReversalInfix to postfix conversionPostfix evaluation

    Write algorithms to implement 2 stacks in a single array to use same STACKFULL for the 2 stacks.Multiple stacksWhat is meant by stack? Implement stack using array. Write the 5 functions/ algorithms to perform different operations on stack. Give its application.What is a stack?LIFOFunctions push(), pull(), status(), isempty(), isfull()