algorithms 7 stack1

Upload: lordjebus2000

Post on 10-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Algorithms 7 Stack1

    1/14

    Problem Solving with ADTProblem Solving with ADT

    ----StacksStacks

  • 8/8/2019 Algorithms 7 Stack1

    2/14

    Definition of StackDefinition of Stack

    ADT levelADT level Abstract Data TypeAbstract Data Type

    A stack is an ordered groupA stack is an ordered groupof homogeneous items, inof homogeneous items, in

    which the removal andwhich the removal andaddition of stack items canaddition of stack items cantake place only at the top oftake place only at the top ofthe stackthe stack

    A limited access dataA limited access data

    structurestructure

    A stack is a LIFO: lastA stack is a LIFO: lastitem in, first one outitem in, first one out

  • 8/8/2019 Algorithms 7 Stack1

    3/14

    Checking for Balanced BracesChecking for Balanced Braces

    Balanced: a+{c*{a+b}*d}+{d+e}

    Unbalanced: {asdf{df}{dfa{df}

  • 8/8/2019 Algorithms 7 Stack1

    4/14

    PseudocodePseudocode

    while (not end of string){

    if (chis {)

    add ch to ADT

    else if (chis }){

    if (ADT is empty)

    return unbalanced;

    remove from ADT the item

    added most recently

    }}

    if (ADT is empty)

    return balanced;

    return unbalanced;Check emptiness

    Remove item

    Add new item

  • 8/8/2019 Algorithms 7 Stack1

    5/14

    Stack ADTStack ADT

    5

    char stack[Size]; //characters

    while (not-end-of string){

    if (chis {)

    push(stack,ch);

    else if (chis }){

    if( stackempty()){

    destroystack();

    return unbalanced;

    }

    item = pop(stack);

    }}endwhile

    if (stackempty())

    return balanced;

    destroyStack();

    return unbalanced;

  • 8/8/2019 Algorithms 7 Stack1

    6/14

    A StackA Stack ---- GraphicGraphic

  • 8/8/2019 Algorithms 7 Stack1

    7/14

    ArrayArray--Based ImplementationBased Implementation

    char/int stack[size]

    bool stackfull();

    bool stackempty();

    void push(stack, item);

    char pop(stack);char gettopitem(stack);

    itemtype stack[MAX];

    int tos; //top of stack

    };

  • 8/8/2019 Algorithms 7 Stack1

    8/14

    PushPush item on Stackitem on Stack

    push (stack, item) item is of type charpush (stack, item) item is of type charif (not stackfull()) stack must not be fullif (not stackfull()) stack must not be full

    stack[tos] = item add itemstack[tos] = item add item

    tos = tos + 1 increment toptos = tos + 1 increment top--ofof--stackstackelseelsewrite(Stack is full, cant add item)write(Stack is full, cant add item)

    end ifend ifend pushend push

  • 8/8/2019 Algorithms 7 Stack1

    9/14

    PopPop item of Stackitem of Stack

    char pop (stack )char pop (stack ) popped item is of typepopped item is of type charcharif (not stackempty() )if (not stackempty() ) stack must not be emptystack must not be empty

    tos = tostos = tos 11 decrement the topdecrement the top--ofof--stackstack

    item = stack[tos]item = stack[tos] item on top of stackitem on top of stackreturn itemreturn item

    elseelsewrite(Stack is empty, cant pop item)write(Stack is empty, cant pop item)

    end ifend if

    end popend pop

  • 8/8/2019 Algorithms 7 Stack1

    10/14

    Compare Three ImplementationsCompare Three Implementations

    Will do this later.Will do this later.

    Array v.s. Linked ListArray v.s. Linked List Fixed size v.s.Fixed size v.s.

    dynamic sizedynamic size Linked List v.s. ADTLinked List v.s. ADT

    ListList Efficiency v.s.Efficiency v.s.

    SimplicitySimplicity

  • 8/8/2019 Algorithms 7 Stack1

    11/14

    Application: Algebraic ExpressionsApplication: Algebraic Expressions

    Infix expressionsInfix expressions

    Every binary operator appears between itsEvery binary operator appears between its

    operands: a+b*c, (a+b)*coperands: a+b*c, (a+b)*c Prefix expressionsPrefix expressions

    Operator appears before its operands:Operator appears before its operands:+a*bc, *+abc+a*bc, *+abc

    Postfix expressionsPostfix expressions

    Operator appears after its operands:Operator appears after its operands:abc*+, ab+c*abc*+, ab+c*

  • 8/8/2019 Algorithms 7 Stack1

    12/14

    Evaluating Postfix ExpressionsEvaluating Postfix Expressions

  • 8/8/2019 Algorithms 7 Stack1

    13/14

    Pseudocode (binary operations only)Pseudocode (binary operations only)

    Stack mystack; declare a stack

    for (each token t in the input){

    if (t is an operand)

    push(mystack,t);

    else {// pop top item of stack

    operand2 = pop(mystack);

    // pop top item of stack again

    operand1 = pop(mystack);

    // compute bi

    nary operati

    onresult = operand1 t operand2;

    push(mystack, result);

    }

    }

  • 8/8/2019 Algorithms 7 Stack1

    14/14

    Postfix Evaluation AlgorithmPostfix Evaluation Algorithm

    Initialise stack to empty;Initialise stack to empty;while (not end of postfix expression) {while (not end of postfix expression) {Get next postfix item (char)Get next postfix item (char)if (item is operand)if (item is operand)push(stack, item)push(stack, item)else if (item is binary operator) {else if (item is binary operator) {

    x = pop(stack)x = pop(stack)y = pop(stack)y = pop(stack)z = y operator xz = y operator xpush(stack, z) }push(stack, z) }

    else if (item is unary operator) {else if (item is unary operator) {

    x = pop(stack)x = pop(stack)z = operator(x)z = operator(x)push(stack,z) }push(stack,z) }

    }}result = pop(stack) // single value at end on stackresult = pop(stack) // single value at end on stack