dc presentation on stack representation

Upload: harpreetbachra

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 DC Presentation on Stack Representation

    1/21

    By:--

    1. Himanshu Bhagat

    2. Kazi Mostafizur Rahman

    3. K. Badash

    4. Lal Bahadur Mishra

    5. Nipun Bhardwaj

    6. Nitesh Kumar

    7. Rakesh Ranjan Choudhary

  • 8/3/2019 DC Presentation on Stack Representation

    2/21

    A stack is a list of elements/items.

    The elements may be inserted only at one end called the top of the stack.

    In other words,A stack is collection/ organization

    of data in a way such that the last element/datainserted is the first one to bedeleted/removed/taken out.

  • 8/3/2019 DC Presentation on Stack Representation

    3/21

    For Example:-

    1) a bunch ofbooks placed on each other on a table.

    2) a lady wearing bangles.

    3) A stack of coins (pennies).

    Basic operations:-

    1) Push:-

    A push operation is used to insert an element into thestack.

    2) Pop:-

    A Pop operation is used to delete an item from the stack.

    Note:- A pointer variable is used to point the top of the stack where theinsertion & deletion of elements takes place.

  • 8/3/2019 DC Presentation on Stack Representation

    4/21

    Operations:- Stack contents

    1. Push A* A is inserted into the stack.* Top points to the locationcontaining A.

    ATop

    B

    ATop

    2. Push A* B is inserted into the stack.* Top points to the locationcontaining B.

    3. Pop* B is deleted from the stack.* Top points to the locationcontaining A.

    ATop

  • 8/3/2019 DC Presentation on Stack Representation

    5/21

    Stacks may be represented in computers as both statically as well asdynamically.

    A representation of stack by means of an array is called staticrepresentation of sack.

    A dynamic representation of stack include linked lists.

    Stack Implementation

    Static Dynamic

    Using linear

    array

    Using linked

    list

  • 8/3/2019 DC Presentation on Stack Representation

    6/21

    Example:-

    77

    23

    5

    77 * 23 * 5 *

    Array representation

    Linked representation

    |

    V

  • 8/3/2019 DC Presentation on Stack Representation

    7/21

    An array is used to represent a stack statically.

    A pointer variable top is used which contains the locationof the top element of the stack.

    Another variable MAXSIZE can be used to specify themaximum no of elements that the stack can hold.

    Note:-

    * Top= 0/NULL indicate that the stack is EMPTY.

    * Top= MAXSIZE indicate that the stack is full.

    Operations:-

    1. Push 2. Pop

    STATIC IMPLEMENTATION

  • 8/3/2019 DC Presentation on Stack Representation

    8/21

    PUSH(STACK,TOP,MAXSIZE,ITEM)1. IFTOP=MAXSTK then print OVERFLOW and return

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

    3. SetSTACK[TOP]=ITEM, Increase ITEM in new TOP position

    4. return

    POP(STACK,TOP,ITEM)

    1. IF TOP=0, PRINT underflow and return

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

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

    4. Return

    Push operation

    Pop operation

  • 8/3/2019 DC Presentation on Stack Representation

    9/21

    In a static representation, We must specify the size of stack before its use.

    Which results in:-

    * Memory wastage,

    * Increase of the chance of overflow.

    Limitations of static representation

  • 8/3/2019 DC Presentation on Stack Representation

    10/21

    The Dynamic represntation or linked representation of a stack,commonly known as linked stack is a stack that is implementedusing a singly linked list.

    The INFO field of the node hold the elements of the stack.

    The LINK field holds pointers to the neighboring element.

    The START pointer of the linked list behaves as the TOP pointer ofthe stack.

    DYNAMIC IMPLEMENTATION

  • 8/3/2019 DC Presentation on Stack Representation

    11/21

    PUSH_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 inthe stack]

    5.Set TOP=NEW[Reset TOP to point to the new node at the TOP of thestack]

    6.Exit

    Push operation

    7 * 11 * 2 *

    For Example:- pushin an element 5 in a list containing the elements :- 7, 11, 2

    Top

    7 * 11 * 2 *

    Top

    5 *

  • 8/3/2019 DC Presentation on Stack Representation

    12/21

    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 intoITEM]

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

    [Remember the old value of the Top pointer in TEMP and reset 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

    Pop operation

    7 * 11 * 2 *

    Top

    Example:-

    11 * 2 *

    Top

  • 8/3/2019 DC Presentation on Stack Representation

    13/21

    An Arithmetic expression is a combination of operand & operator.

    The representation of an arithmetic expression can be done inthree ways:-

    1. Infix notation/ Infix Expression.

    2. Polish/prefix notation.

    3. Reverse polish/Postfix notation.

    If an operator is placed between two operands, then theexpression is called as infix expression.

    Example:-

    (A+B)*C

    ARITHMETIC EXPRESSION

    Infix notation

  • 8/3/2019 DC Presentation on Stack Representation

    14/21

    The notation in which the operator is placed before itstwo operands is called a polish/prefix notation.

    For Example:-

    *+ABC

    The notation in which the operator isplaced before its two operands is called a polish/prefix notation.

    For Example:-

    AB+c*

    Prefix notation

    Postfix notation

  • 8/3/2019 DC Presentation on Stack Representation

    15/21

    POLISH(Q,P)

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

    2. Scan Q from left to right & repeat step 3. & 4. for each element untilthe Stack is EMPTY.

    3. If an operand is encountered, add it to P.

    4. If a left parenthesis is encountered, push it onto the stack.

    5. if an operator is encountered, then:

    a) Repeatedly pop from the stack & add it to p, eachoperator which has same as or higher precedence then the operatorencountered.

    b) Add operator to stack.

    TRANSFORMING INFIX INTOPOSTFIX

    Procedure

  • 8/3/2019 DC Presentation on Stack Representation

    16/21

    6. If a right parenthesis is encountered, then:

    a) Repeatedly pop from the stack & add it to P, eachoperator until a left parenthesis is encountered.

    b) Remove left parenthesis from the stack.

    7. EXIT.

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

    Symbol scanned STA

    CKP

    A ( A

    + (+ A

    ( (+( A

    B (+( AB

    * (+(* AB

    C (+(* ABC

    ) (+ ABC*

    - (- ABC*+

    D (- ABC*+

    ) EMPTY ABC*+-

  • 8/3/2019 DC Presentation on Stack Representation

    17/21

    1. Add ) at the end of P.

    2. Scan P from left to right and repeat step 3. & 4. until thesentinel ) is encountered.

    3. If an operand is encountered, push it on STACK.

    4. If an operator is encountered, then:

    a) Remove the top 2 elements of stack, where A

    is the top element & B is the next to top element.

    b) Evaluate a * b

    c) Place the result ofb)back on stack.5. Set VALUE=Top element on stack.

    6. EXIT.

    EVALUATINGPOSTFIX EXPRESSION

    Procedure

  • 8/3/2019 DC Presentation on Stack Representation

    18/21

    For Example:-

    consider p:-5, 6, 2,+,*, 12, 4, /, -

    SYMBOL SCANNED STACK

    5 5

    6 5, 6

    2 5, 6, 2

    + 5, 8

    * 40

    12 40, 12

    4 40, 12, 4

    / 40, 3- 37

    )

    VALUE= 37

  • 8/3/2019 DC Presentation on Stack Representation

    19/21

    Expression evaluation

    Backtracking (game playing, finding paths, exhaustivesearching)

    Memory management, run-time environment for nestedlanguage features.

    APPLICATIONSOF STACKS

  • 8/3/2019 DC Presentation on Stack Representation

    20/21

    Thus we can say that a stack is an effective way of

    representing data that maybe used in expressionevaluation, backtracking, memory management.

    CONCLUSION

  • 8/3/2019 DC Presentation on Stack Representation

    21/21