lecture - stack adt.pdf

Upload: cindy-jones

Post on 03-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Lecture - Stack ADT.pdf

    1/16

    Data Structures CSC212 (1)

    Stack ADTStack ADT

    Dr Muhammad Hussain Lecture - Stack ADT

  • 7/28/2019 Lecture - Stack ADT.pdf

    2/16

    Data Structures CSC212 (2)

    Stack ADTStack ADTAn ADT that manages data elements linearly but provides access at only oneend i.e. data elements can be inserted and removed from one end only.

    23

    14 34

    23

    14

    23

    14

    2

    6

    8

    2

    6

    8

    2

    6

    8

    NOTE:NOTE:

    InIn Stack ADT, the elements are removed only in the inverse orderStack ADT, the elements are removed only in the inverse order

    Dr Muhammad Hussain Lecture - Stack ADT

    o t e r arr va .e. t e ast nserte e ement s remove rst o a .o t e r arr va .e. t e ast nserte e ement s remove rst o a .

    So, it has lastSo, it has last--in/firstin/first--out (LIFO) behavior.out (LIFO) behavior.

  • 7/28/2019 Lecture - Stack ADT.pdf

    3/16

    Data Structures CSC212 (3)

    handled using stackhandled using stack The a ers ut in the tra of aThe a ers ut in the tra of a rinter. Therinter. The a ersa ers

    from the tray are removed by the printer infrom the tray are removed by the printer in reversereverse

    order they are put in theorder they are put in the tray.tray. StackStack is used to evaluate expressions likeis used to evaluate expressions like 1010x((1212++77))

    AA runtime system uses a stack to handleruntime system uses a stack to handle functionfunction

    ..

    Dr Muhammad Hussain Lecture - Stack ADT

  • 7/28/2019 Lecture - Stack ADT.pdf

    4/16

    Data Structures CSC212 (4)

    S ecification of Stack ADTS ecification of Stack ADTElementsElements:: Any valid data typeAny valid data typeStructureStructure:: AnAn arrangement of elements that allows to removearrangement of elements that allows to remove

    DomainDomain:: Number of elements is boundedNumber of elements is boundedOperationsOperations ::OperationOperation SpecificationSpecification

    booleanboolean empty()empty()Precondition/Requires:Precondition/Requires: nonenone

    Processing/Results:Processing/Results: returns true if the stack is empty otherwise false.returns true if the stack is empty otherwise false.

    booleanboolean full()full()

    Precondition/Requires:Precondition/Requires: nonenone

    Processing/Results:Processing/Results: returns true if no more elements can be insertedreturns true if no more elements can be inserted

    into the stack otherwise false.into the stack otherwise false.

    void push(Type)void push(Type)

    Processing/Results:Processing/Results: inserts a given element into the stack at the topinserts a given element into the stack at the top

    and top points to it.and top points to it.

    Precondition/Requires:Precondition/Requires: stack is not emptystack is not empty

    Dr Muhammad Hussain Lecture - Stack ADT

    Type pop()Type pop() Processing/Results:Processing/Results: the element at the top of the stack is removedthe element at the top of the stack is removed

    from the stack and returned.from the stack and returned.

  • 7/28/2019 Lecture - Stack ADT.pdf

    5/16

    Data Structures CSC212 (5)

    Re resentation of Stack ADTRe resentation of Stack ADTStack ADT can be represented asStack ADT can be represented as

    -- ArrayArray

    -- Linked ListLinked List

    0

    1

    2

    3

    Null

    Dr Muhammad Hussain Lecture - Stack ADT

    n -

    Array

  • 7/28/2019 Lecture - Stack ADT.pdf

    6/16

    Data Structures CSC212 (6)

    public classArrayStackArrayStack

    //Data Membersprivate int maxsize;

    pr va e n op;

    private T[] nodes;

    // Operationspublic ArrayStack(int n)

    Public boolean empty()

    public void push(T e)

    public T pop()

    Dr Muhammad Hussain Lecture - Stack ADT

  • 7/28/2019 Lecture - Stack ADT.pdf

    7/16

    Data Structures CSC212 (7)

    publicArrayStack(int n)

    maxsize =n;top = -1;nodes = (T[]) new Object[n];

    }

    public boolean empty()

    return top == -1;}

    Dr Muhammad Hussain Lecture - Stack ADT

  • 7/28/2019 Lecture - Stack ADT.pdf

    8/16

    Data Structures CSC212 (8)

    public boolean full()

    {return top == maxsize - 1;

    public void push(T e)

    if(full()) return;top++;

    no es op = e;}

    Dr Muhammad Hussain Lecture - Stack ADT

  • 7/28/2019 Lecture - Stack ADT.pdf

    9/16

    Data Structures CSC212 (9)

    public T pop(){

    return nodes[top--];}

    Dr Muhammad Hussain Lecture - Stack ADT

  • 7/28/2019 Lecture - Stack ADT.pdf

    10/16

    Data Structures CSC212 (10)

    Linked List based Implementation of Stack ADTLinked List based Implementation of Stack ADT

    public class LinkStackLinkStack

    // Data Memebrsprivate Node top;

    // Operations

    public LinkStack()

    public boolean full()

    public void push(T e)

    pu c pop}

    Dr Muhammad Hussain Lecture - Stack ADT

  • 7/28/2019 Lecture - Stack ADT.pdf

    11/16

  • 7/28/2019 Lecture - Stack ADT.pdf

    12/16

    Data Structures CSC212 (12)

    public void push(T e)

    Node tmp = new Node(e);tmp.next = top;

    }

    {

    Node tmp = top;

    T e = top.data;top = top.next;

    Dr Muhammad Hussain Lecture - Stack ADT

    }

  • 7/28/2019 Lecture - Stack ADT.pdf

    13/16

    Data Structures CSC212 (13)

    Array Based Implementation

    - An arra based im lementation suffers from the drawback that

    all the storage space must be reserved in advance and themaximum depth of the stack is limited to this arrays size.

    - .

    Linked List based Implementation

    - The time complexity of all operations is O(1). For applications in which the maximum stack size is known

    ahead of time, an array is suitable

    ,

    a linked list

    Dr Muhammad Hussain Lecture - Stack ADT

  • 7/28/2019 Lecture - Stack ADT.pdf

    14/16

    Data Structures CSC212 (14)

    A lication of Stack ADTA lication of Stack ADTInfix Expressions- An expression in which everybinary operation appears between

    Example: (i) a+b + is a binary operation and a andb areits operands

    *

    Prefix Expressions

    - An expression in which operator comes before its operands

    (ii) (a+b)*c *+abc

    (iii) a+(b*c) +a*bc

    - An expression in which operator comes after its operands

    Example: (i) a+b ab+

    Dr Muhammad Hussain Lecture - Stack ADT

    a c a c

    (iii) a+(b*c)

    abc*+

  • 7/28/2019 Lecture - Stack ADT.pdf

    15/16

    Data Structures CSC212 (15)

    Application of Stack ADT Some calculators require that the input expression be in postfixform

    them to postfix form

    Problem: Convert the infix expression a - (b+c)*d into postfix

    Method: Peroform the following steps with a stacks wr te a a

    push(s, -)

    push(s, ( )

    write b abpush(s, +)

    Dr Muhammad Hussain Lecture - Stack ADT

    When right parenthesis ( is found, pop(s, +) and write it abc+

  • 7/28/2019 Lecture - Stack ADT.pdf

    16/16

    Data Structures CSC212 (16)

    pop(s, ( )

    push(s, *)

    write d

    abc+dpop(s, *) write it abc+d*

    - * -

    Exercise

    ,

    r e a unc on a conver s an n x express on o s

    postfix form using a stacks and its operations given in the

    specification of stack ADT.

    Dr Muhammad Hussain Lecture - Stack ADT