+ All Categories
Home > Documents > Pai Ch04 Stack

Pai Ch04 Stack

Date post: 17-Feb-2018
Category:
Upload: anon
View: 224 times
Download: 0 times
Share this document with a friend

of 23

Transcript
  • 7/23/2019 Pai Ch04 Stack

    1/23

    PROPRIETARY MATERIAL. 2008 The McGraw-Hill Companies, Inc. All rights reserve. !o part o" this #ower#oint slie ma$ %e ispla$e, repro&ce or istri%&tein an$ "orm or %$ an$ means, witho&t the prior written permission o" the p&%lisher, or &se %e$on the limite istri%&tion to teachers an e&cators permitte %$ McGraw-Hill

    "or their inivi&al co&rse preparation. I" $o& are a st&ent &sing this #ower#oint slie, $o& are &sing it witho&t permission.

    c raw- ucat on

    Copyrighted Material-Additional resource material supplied with the book Data Structures and Algorithms : Concepts, echni!ues and Applications authored "y G#A#$# %ai and published by

    Tata McGraw Hill. This resource material is for Instructor's use only.

    Stacks

    (Chapter 4)

  • 7/23/2019 Pai Ch04 Stack

    2/23

    &

    Introduction - definitions

    Stack Operations (Push & Pop)

    Stack Implementation (Array & Linked List)

    Algorithms of Push and Pop operationsApplications

    Recursie Programming

    !aluation of !"pressions

    ADT for stacks

    Outline of Chapter 4

  • 7/23/2019 Pai Ch04 Stack

    3/23

    '

    1. Stack : Denition

  • 7/23/2019 Pai Ch04 Stack

    4/23

    (

    A stackis an ordered list with therestriction that elements are addedor deleted from only one end of thelist termed top of stack.

    The other end of the list which lies

    inactive is termed bottom of stack.

    The stack data structure oeys theprinciple ofLast In First Out(!"#$).

    "n other words% elements inserted oradded into the stack &oin last andthose that &oined last are the 'rst toe removed.

    What is Stack? Denition

    Top of stack ottom ofstack

  • 7/23/2019 Pai Ch04 Stack

    5/23

    )

    The two operations which stack data structure

    supports (i) "nsertion or addition of elements known as Push.(ii) eletion or removal of elements known as Pop.

    Stack Operations

    Top of stack ottom of

    stack

  • 7/23/2019 Pai Ch04 Stack

    6/23

    *

    Certain prolems * applications

    need stack mechanism+

    (i) Simulation of people enterin,* leavin, a lift in a uildin,.(ii) -eap memory mana,ement

    in $S.(iii) ana,in, data in recursion(iv) ana,in, arithmetice/pressions

    0e shall see some e/ampleslater.

    Why use Stack?

    1nters !eaves

  • 7/23/2019 Pai Ch04 Stack

    7/23

    +

    2. Stack Ipleentation

  • 7/23/2019 Pai Ch04 Stack

    8/23

    2 A common and a asic method of

    implementin, stacks is to makeuse of another fundamental datastructure vi3.% arrays.

    2 0hile arrays are sequential datastructures the other alternative

    of employin, linked datastructures have eensuccessfully attempted andapplied.

    2 An array ased implementation of

    stacks is fairly convenientconsiderin, the fact that stacksare unidimensional ordered listsand so are arrays which despitetheir multidimensional structureare inherently associated with aonedimensional consecutive set

    Stack Ipleentation

    Top of stack

    ottom ofstack

    55 "n 6ava

    "nt top789 55 currentlocation% top78+ stackempty.int:; Stack7 new int:8

  • 7/23/2019 Pai Ch04 Stack

    9/23

    Algorithm 4.1 Implementation of push

    operation on a stack// Stack is the array

    // n=max size; top=current location;

    // item=data to be pushed

    procedure PUSH(S!"#$ n$ top$ item% if(top = n% thenS!"#&'U; else

    ) top = top * +;

    S!"#,top- = item; /.store

    item as top element of STACK */

    endPUSH

    Stack : !l"orith of P#S$

  • 7/23/2019 Pai Ch04 Stack

    10/23

    ./

    Algorithm 4.2 :Implementation ofpop operation on a stack

    // S!"# is the array

    // top=current location;

    // item=data retrie0edprocedure P1P(S!"#$ top$ item% if(top = 2+% thenS!"#&34P5; else) item = S!"#,top-;

    top = top 2 +;

    endP1P

    Stack : !l"orith of POP

  • 7/23/2019 Pai Ch04 Stack

    11/23

    ..

    %. &'aples of Stack

    !pplications

  • 7/23/2019 Pai Ch04 Stack

    12/23

    .&

    urin, the e/ecutionof a recursive pro,ram% to keep trackof the callsmade to itself and to record the status of theparameters at the time of the call% a stackdata structure isused

    Tail recursionor Tail end recursionis a special case ofrecursion where a recursive call to the function turns out toe the last action in the callin, function. >ote that therecursive call needs to e the last executed statement in

    the function and not necessarily the last statement in thefunction.

    &'aple(1: Stack !pplication

    )ecursi*e pro"rain"

    .'

  • 7/23/2019 Pai Ch04 Stack

    13/23

    .'

    "n a stack implementation of a recursive call% allthe local variales of the function that are to e?rememered@% are pushed into the stack whenthe call is made.

    pon termination of the recursive call% the localvariales are popped out and restored to theirprevious values.

    >ow for tail recursion% since the recursive callturns out to e the last e/ecuted statement% thereis no need that the local variales must e pushedinto a stack for them to e ?rememered@ and?restored@ on termination of the recursive call.

    &'aple: Stack !pplication

    )ecursi*e pro"rain" +cont,

    .(

  • 7/23/2019 Pai Ch04 Stack

    14/23

    .(

    &'aple: Data o*eent inStack for the recursi*e "cf+4-,

    00 call gc12(,*3

    00 a 4 (5 " 4 *5 answer 4 &

    public static int gc1 int a! int b"

    # if a $$ b" %% if a e&uals b

    return a

    if a ( b"

    return gc1 a - b! b"

    else %% if a ) b

    return gc1a! b - a"

    *

    %6SH

    gc1 +! ,"

    %6SH

    gc1+! "

    gc1 +! ,"

    %6SH

    gc1! "

    gc1+! "

    gc1 +! ,"

    %7%

    return

    gc1! "

    gc1+! "

    gc1 +! ,"

    8stcall% BS-

    a% BS-

    aD% BS-a7% B$B

    .)

  • 7/23/2019 Pai Ch04 Stack

    15/23

    .)

    In' e'pression $perandD $peratorD $perandD

    An aritmetic e/pression+

    Post' e'pression $perandD $perandD $peratorD

    Pre' e'pression $peratorD $perandD$perandD

    dcba +

    + dabc

    bcda '+

    &'aple(2: Stack !pplication

    &*aluation of &'pressions

    .*

  • 7/23/2019 Pai Ch04 Stack

    16/23

    .*

    Scenario: Given abc*+d- !e get ""b*c#+a#-d

    Algorithm 4.$ : %rocedure to evaluate a post fi& e&pression '.(nput: '&pression ')utput: )riginal '&pression%rocedure 'A,%)S(/"'# & 0 get,ne&t,character "'#

    *get the next character of expression E */ case & of

    :& is an operand: %ush & into stac3 S

    :& is an operator: %op out reuired number ofoperands from the stac3 S

    evaluate the operator and pushthe result into the stac3 S

    :& 0 567: %op out the result from stac3 S end case

    end 'A-%)S(/.

    .+

  • 7/23/2019 Pai Ch04 Stack

    17/23

    .+

    Post' e'pression $perandD $perandD

    $peratorD + (2)*

    &'ercise: /racin" the Stackoperations to e*aluate Post'

    &'pressions

    232

    732

    212

    ? ? ?

    .

  • 7/23/2019 Pai Ch04 Stack

    18/23

    .

    Pre' e'pression $peratorD $perandD $perandD

    &'ercise: Can 0e use Stack toe*aluate Pre' &'pressions? /ry

    it

    ? ? ? ? ? ? ?

    bcda '+

    .

  • 7/23/2019 Pai Ch04 Stack

    19/23

    .

    4. !D/ of Stack

    &/

  • 7/23/2019 Pai Ch04 Stack

    20/23

    &/

    Data o3ects:

    A 'nite set of elements of the same type

    Operations:Create an empty stack and initiali3e top of stack

    "63!3(S!"#%Check if stack is empty

    "H#&S!"#&34P5(S!"#%(oolean function)

    Check if stack is full "H#&S!"#&'U(S!"#%(oolean function)

    Bush I34into stack S!"#PUSH(S!"#$ I34%

    Bop element from stack S!"#and output the elementpopped in I34

    P1P(S!"#$ I34%

    !D/ for Stack

    &.

  • 7/23/2019 Pai Ch04 Stack

    21/23

    &.

    &'ercises

    &&

  • 7/23/2019 Pai Ch04 Stack

    22/23

    &&

    (1 Write a a*a Interface 5 a*a pro"ra to ipleent the Stack!D/.

    (2 Write a a*a pro"ra- usin" Stack in con*ertin" a 6ecialnuer into a inary nuerThe lo,ic for transformin, a decimal numer into a inary numer is asfollows+

    Eead a numer

    "teration (while numer is ,reater than 3ero)#ind out the remainder after dividin, the numer y FBrint the remainderivide the numer y F

    1nd the iteration-owever% there is a prolem with this lo,ic. Suppose the numer% whose

    inary form we want to 'nd is FG. sin, this lo,ic% we ,et the result as888

  • 7/23/2019 Pai Ch04 Stack

    23/23

    &'

    PROPRIETARY MATERIAL. 2008 The McGraw-Hill Companies, Inc. All rights reserve. !o part o" this #ower#oint slie ma$ %e ispla$e, repro&ce or istri%&te

    in an$ "orm or %$ an$ means, witho&t the prior written permission o" the p&%lisher, or &se %e$on the limite istri%&tion to teachers an e&cators permitte %$ McGraw-Hill

    "or their inivi&al co&rse preparation I" $o& are a st&ent &sing this #ower#oint slie $o& are &sing it witho&t permission

    Copyrighted Material-Additional resource material supplied with the book Data Structures and Algorithms : Concepts, echni!ues and Applications authored "y G#A#$# %ai and published by

    Tata McGraw Hill. This resource material is for Instructor's use only.

    1nd of

    Stacks

    (Chapter 4)


Recommended