lecture 3 stack

Upload: vicky-butt

Post on 08-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Lecture 3 Stack

    1/54

    Data Structures & Algorithms

    Lecture # 3

  • 8/7/2019 Lecture 3 Stack

    2/54

    Today

    Topics

    What are Stacks?

    Representation of Stacks

    Operations on Stacks Push Pop

    Evaluation of Expressions

    Infix to Postfix Conversion

  • 8/7/2019 Lecture 3 Stack

    3/54

    Stack application Function calls

    Reversing Data: We can use stacks to reverse data.

    (example: files, strings)

    Very useful for finding palindromes.Consider the following pseudocode:

    1) read (data)

    2) loop (data not EOF and stack not full)

    1) push (data)

    2) read (data)

    3) Loop (while stack notEmpty)

    1) pop (data)

    2) print (data)

  • 8/7/2019 Lecture 3 Stack

    4/54

    Stack application

  • 8/7/2019 Lecture 3 Stack

    5/54

    What are Stacks?

    In array insertion and deletion can take place atboth end, i.e. start and end,

    But if insertion and deletion are restricted from oneend, must used STACKS , QUEUES.

    Stack can be defined as:

    A stack is a linear data structure which canbe accessed only at one of its ends for

    storing and retrieving data. OR In which item may b inserted or deleted only at one

    end called top of the stack. There are two ways of implementing a stack:

    Array (Static) Link List (Dynamic)

  • 8/7/2019 Lecture 3 Stack

    6/54

    Example of Stack A common model of a

    stack is a plate or coinstacker. Plates are

    "pushed onto to the topand "popped" off from thetop.

    Stacks form Last-In-First-Out (LIFO) queues.

  • 8/7/2019 Lecture 3 Stack

    7/54

    Stack terminology

    Top: end where insertion and

    deletion take place

    Push: is the term used to insert an

    element into an array

    Pop: is the term used to delete an

    element from a stack.

  • 8/7/2019 Lecture 3 Stack

    8/54

    Basic operations of a StackNew Push

    Pop Peek

    ?

    Empty?

  • 8/7/2019 Lecture 3 Stack

    9/54

    Array implementation of Stacks

    A stack can be implemented with an arrayand an integer.

    A pointer variable TOP which contains thelocation of the top element of the stack;

    Variable MAXSTK which gives the maximum

    number of elements that can be held by thestack.

    Condition TOP=0, or MAXSTK=NULLindicate that the stack is empty.

  • 8/7/2019 Lecture 3 Stack

    10/54

    Note that all operations need check arraybounds

    Pushing onto a full stack: Overflow When TOP=MAXSTK

    Popping an empty stack: Underflow

    TOP=NULL

    Stack implemented in an array

  • 8/7/2019 Lecture 3 Stack

    11/54

    Operations on Stack A stack is generally implemented with only

    two principle operations (apart from aconstructor and destructor methods):

    initializeStack: initializes the stack to an emptystate

    destroyStack: Removing all the element 4m thestack

    Is EmpthyStack: check whether stack is empty or

    not, and return true or false. IsFullStack: returns true if full else false. Top: returns the top elements of the stack; Push: adds an item to a stack Pop: extracts the most recently pushed item from

    the stack

  • 8/7/2019 Lecture 3 Stack

    12/54

    How the stack routines work

    empty stack; push(a), push(b); pop

    empty stacktop = 0

    a

    push(a)top = 1

    b

    a

    push(b)top = 2

    a

    pop()top = 1

  • 8/7/2019 Lecture 3 Stack

    13/54

    Implementation of the operation Initialize Stack: Destroy Stack: EmpthyStack: Full Stack:

    Top: returns the top elements of the stack; Push: tow step process, adding and

    incrementing the top pointer. Pop: extracts the most recently pushed item

    from the stack

  • 8/7/2019 Lecture 3 Stack

    14/54

    Algorithms of Push OperationPush (STACK , TOP, MAXST, ITEM)

    Algorithm for push element into the Stack

    1. [stack already filled]IfTOP= MAXSTK, then: print: OVERFLOW,and return.

    2. [increases TOP by 1] Set TOP:= TOP + 1

    3. [insert ITEM in new TOP position ]

    Set STACK[TOP]: =ITEM4. RETRUN

  • 8/7/2019 Lecture 3 Stack

    15/54

    Algorithms of Pop OperationPop (STACK, TOP, ITEM)

    Algorithm for pop element from the Stack

    1. [Stack has an item to be removed]

    IfTOP= NUL, then

    Print: UNDERFLOWand return

    2. [assignTOP element to ITEM]

    Set ITEM := STACK[TOP]

    3. [DecreaseTOP by 1]Set TOP:= TOP -1

    4. Return

  • 8/7/2019 Lecture 3 Stack

    16/54

    Algorithm for Display Stack

    elementsDisplay_Stack ()

    Algorithm for display stack elements

    1. Start2. Repeat step 3 For i = 1 to TOP by 1

    3. Print S[i]

    4. End

  • 8/7/2019 Lecture 3 Stack

    17/54

    Algorithm for isempty Stackisempty ()

    Algorithm for return the Stack status

    1. Start2. ifTOP = 0 then

    Return True

    else

    Return False3. End

  • 8/7/2019 Lecture 3 Stack

    18/54

    Algorithm for return the top of

    stack valuetop ()

    Algorithm for return the top of Stackvalue

    1. Start

    2. Return TOP

    3. End

  • 8/7/2019 Lecture 3 Stack

    19/54

  • 8/7/2019 Lecture 3 Stack

    20/54

    Evaluation of Expression Order in which the expression is evaluated is:

    If the expression has parenthesis, then they areevaluated first

    Exponential (^) is given highest priority

    Multiplication (*) and division (/) have the nexthighest priority

    Addition (+) and subtraction (-) have the lowestpriority

  • 8/7/2019 Lecture 3 Stack

    21/54

    Evaluation of Expression Steps to evaluate the following expression:

    (2^3 + 6) * 2 9 / 3

    = (8 + 6) * 2 9 / 3

    = 14 * 2 9 / 3

    = 28 3

    = 25

  • 8/7/2019 Lecture 3 Stack

    22/54

    Infix, Prefix (Polish) and Postfix

    (Reverse Polish) NotationInfix Prefix

    (Polish Notation)

    Postfix

    (Reverse PolishNotation)

    A+B +AB AB+

    A*B *AB AB*

    A/B /AB AB/

    A-B -AB AB-

  • 8/7/2019 Lecture 3 Stack

    23/54

    Evaluation of Expression Computer evaluates an expression given in infix notation by

    converting it into postfix notation.

    Stack is used to perform this operation

    Following steps are take to evaluate a postfix expression:

    Expression is scanned from left to right until the end of theexpression

    When an operand is encountered, it is pushed into stack

    When an operator is encountered, then Top two operands of stack are removed Arithmetic operation is performed Computed result is pushed back to the stack

    When end of the expression is reached, the top value from thestack is picked. It is the computed value of the expression

  • 8/7/2019 Lecture 3 Stack

    24/54

    Infix to Postfix Conversion Stack is used to convert an infix expression to postfix.

    Stack is used to store operators and operands and then pass tothe postfix expression according to their precedence.

    Infix expression is converted into postfix expression accordingto the following rules:

    Infix expression is scanned from left to right until end ofthe expression.

    Operands are passed directly to the output.

    Operators are first passed to the stacks.

    Operand is encountered, it is added to the output.

  • 8/7/2019 Lecture 3 Stack

    25/54

    Infix to Postfix Conversion Each time an operator is read, the stack is repeatedly

    popped and operands are passed to the output, until anoperator is reached that has a lower precedence than themost recently read operator. The most recently readoperator is then pushed onto the stack.

    When end of the infix expression is reached, all operatorsremaining in the stack are popped and passed to theoutput in the same sequence.

    Parentheses can be used in the infix expression but theseare not used in the postfix expression.

    During conversion process, parentheses are treated asoperators that have higher precedence than any otheroperator.

  • 8/7/2019 Lecture 3 Stack

    26/54

    Infix to Postfix Conversion Left parenthesis is pushed into the stack, when

    encountered.

    Right parenthesis is never pushed to the stack.

    Left parenthesis is popped only when rightparenthesis is encountered.

    The parentheses are not passed to the output postfixexpressions. They are discarded.

    When end of expression is reached, then all operatorsfrom stack are popped and added to the output.

  • 8/7/2019 Lecture 3 Stack

    27/54

    Example (Infix to Postfix

    Conversion)Convert infix expression

    A+B*C+(D*E+F)*G into postfix

    A + B * C + ( D * E + F ) * G

    1. Scanned from left to right. Firstoperand read is A and passed tooutput

    Stack

    Output: A

  • 8/7/2019 Lecture 3 Stack

    28/54

    Example (Infix to Postfix

    Conversion)A + B * C + ( D * E + F ) * G

    2. Next the + operator is read, atthis stage, stack is empty.

    Therefore no operators arepopped and + is pushed into thestack. Thus the stack and outputwill be:

    +

    Stack

    Output: A

  • 8/7/2019 Lecture 3 Stack

    29/54

    Example (Infix to Postfix

    Conversion)A + B * C + ( D * E + F ) * G

    3. Next the B operand is read andpassed to the output. Thus the

    stack and output will be:

    +

    Stack

    Output: AB

  • 8/7/2019 Lecture 3 Stack

    30/54

    Example (Infix to Postfix

    Conversion)A + B * C + ( D * E + F ) * G

    4. Next the * operator is read, Thestack has + operator which has

    lower precedence than the *operator. Therefore no operatorsare popped and * is pushed intothe stack. Thus the stack andoutput will be:

    *

    +

    Stack

    Output: AB

  • 8/7/2019 Lecture 3 Stack

    31/54

    Example (Infix to Postfix

    Conversion)A + B * C + ( D * E + F ) * G

    5. Next the C operand is read andpassed to the output. Thus the

    stack and output will be:

    *

    +

    Stack

    Output: ABC

  • 8/7/2019 Lecture 3 Stack

    32/54

    Example (Infix to Postfix

    Conversion)A + B * C + ( D * E + F ) * G

    6. Next the + operator is read, Thestack has * operator which has

    higher precedence than the +operator. The Stack is poppedand passed to output. Next stackhas + operator which has sameprecedence than the + operator.The Stack is popped and passedto output. Now stack is empty,therefore no operators arepopped and + is pushed into thestack. Thus the stack and outputwill be:

    +

    Stack

    Output: ABC*+

  • 8/7/2019 Lecture 3 Stack

    33/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    7. Next the left parenthesis ( isread, Since all operators have

    lower precedence than the leftparenthesis, it is pushed into thestack. Thus the stack and outputwill be:

    (

    +

    Stack

    Output: ABC*+

  • 8/7/2019 Lecture 3 Stack

    34/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    8. Next the D operand is read andpassed to the output. Thus the

    stack and output will be:

    (

    +

    Stack

    Output: ABC*+D

  • 8/7/2019 Lecture 3 Stack

    35/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    9. Next the * operator is read.Now, left parenthesis ( has

    higher precedence than *; it cannot be popped from the stackuntil a right parenthesis ) hasbeen read. Thus the stack is notpopped and * is pushed into thestack. Thus the stack and outputwill be:

    *

    (

    +

    Stack

    Output: ABC*+D

  • 8/7/2019 Lecture 3 Stack

    36/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    10. Next the E operand is read andpassed to the output. Thus the

    stack and output will be:

    *

    (

    +

    Stack

    Output: ABC*+DE

  • 8/7/2019 Lecture 3 Stack

    37/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    11. Next the + operator is read, Thestack has * operator which has

    higher precedence than the +operator. The Stack is poppedand passed to output. Next stackhas left parenthesis ( which hasnot been popped and + operatoris pushed into the stack. Thusthe stack and output will be:

    +

    (

    +

    Stack

    Output: AB*+DE*

  • 8/7/2019 Lecture 3 Stack

    38/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    12. Next the F operand is read andpassed to the output. Thus the

    stack and output will be:

    +

    (

    +

    Stack

    Output: ABC*+DE*F

  • 8/7/2019 Lecture 3 Stack

    39/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    14. Next the * operator is read, Thestack has + operator which has

    lower precedence than the *operator. Therefore no operatorsare popped and * is pushed intothe stack. Thus the stack andoutput will be:

    *

    +

    Stack

    Output: ABC*+DE*F+

  • 8/7/2019 Lecture 3 Stack

    40/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    15. Next the G operand is read andpassed to the output. Thus the

    stack and output will be:

    *

    +

    Stack

    Output: ABC*+DE*F+G

  • 8/7/2019 Lecture 3 Stack

    41/54

    Example (Infix to PostfixConversion)

    A + B * C + ( D * E + F ) * G

    16. The end of expression isencountered. The operators are

    popped from the stacked andpassed to the output in the samesequence in which these arepopped. Thus the stack andoutput will be:

    Stack

    Output: ABC*+DE*F+G*+

  • 8/7/2019 Lecture 3 Stack

    42/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    1. Scanned from left to right. In first,second and third iteration, the valueof A, B and C are pushed into thestack. Thus the stack will be: 9

    6

    5

    Stack

  • 8/7/2019 Lecture 3 Stack

    43/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    2. In fourth iteration, the operator * isread. So the two values 9 and 6are popped from the stack andmultiplication is perform. i.e 9*6=54.The computed value pushed back tothe stack. Thus the stack will be: 54

    5

    Stack

  • 8/7/2019 Lecture 3 Stack

    44/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    3. In fifth iteration, the operator + isread. So the two values 54 and 5are popped from the stack andaddition is perform. i.e 54+5=59.The computed value pushed back tothe stack. Thus the stack will be:

    59

    Stack

  • 8/7/2019 Lecture 3 Stack

    45/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    4. In sixth and seventh iteration, thevalue of D and E are pushed into thestack. Thus the stack will be:

    4

    2

    59

    Stack

  • 8/7/2019 Lecture 3 Stack

    46/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    5. In eighth iteration, the operator * isread. So the two values 4 and 2are popped from the stack andmultiplication is perform. i.e 2*4=8.The computed value pushed back tothe stack. Thus the stack will be:

    8

    59

    Stack

  • 8/7/2019 Lecture 3 Stack

    47/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    6. In ninth iteration, the value of F ispushed into the stack. Thus the stackwill be:

    8

    8

    59

    Stack

  • 8/7/2019 Lecture 3 Stack

    48/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    7. In tenth iteration, the operator + isread. So the two values 8 and 8are popped from the stack andaddition is perform. i.e 8+8=16. Thecomputed value pushed back to thestack. Thus the stack will be:

    16

    59

    Stack

  • 8/7/2019 Lecture 3 Stack

    49/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    8. In eleventh iteration, the value of Gis pushed into the stack. Thus thestack will be:

    3

    16

    59

    Stack

  • 8/7/2019 Lecture 3 Stack

    50/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    9. In twelve iteration, the operator * isread. So the two values 3 and 16are popped from the stack andmultiplication is perform. i.e3*16=48. The computed valuepushed back to the stack. Thus the

    stack will be:

    48

    59

    Stack

  • 8/7/2019 Lecture 3 Stack

    51/54

    Example (Evaluation of Expression)

    Postfix Expression

    A B C * + D E * F + G * +

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    10. In thirteen iteration, the operator +is read. So the two values 48 and59 are popped from the stack andaddition is perform. i.e 48+59=107.The computed value pushed back tothe stack. Thus the stack will be:

    107

    Stack

  • 8/7/2019 Lecture 3 Stack

    52/54

    Example (Evaluation of Expression)

    A + B * C + ( D * E + F ) * G

    A=5, B=6, C=9, D=2, E=4, F=8, G=3

    = 5 + 6 * 9 + (2 * 4 + 8) * 3

    = 5 + 54 + (8 + 8) * 3

    = 59 + 16 * 3

    = 59 + 48

    =107

  • 8/7/2019 Lecture 3 Stack

    53/54

    Next Lecture

    What are Queues?

    Representation of Queues

    Operations on Queues

    QInsert

    QDelete

  • 8/7/2019 Lecture 3 Stack

    54/54

    References

    Structures: A Pseudocode Apporoach with C. Gilberg, RichardF., Forouzan, Data Behrouz A.. PWS Publishing Company:

    1998