lecture 1 stacks.pptx 0

Upload: atif-imam

Post on 04-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    1/32

    Data structures & Algorithms

    Stacks

    S. Swami

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    2/32

    Objectives

    What is a stack ?

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    3/32

    Objectives

    What is a stack ?

    Applications

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    4/32

    Objectives

    What is a stack ?

    Applications

    Concept of pushpop routines

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    5/32

    Objectives

    What is a stack ?

    Applications

    Concept of pushpop routines

    !mplementation using arra"s

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    6/32

    Objectives

    What is a stack ?

    Applications

    Concept of pushpop routines

    !mplementation using arra"s

    Developing a function librar" for stacks

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    7/32

    Objectives

    What is a stack ?

    Applications

    Concept of pushpop routines

    !mplementation using arra"s

    Developing a function librar" for stacks

    Creating solutions using stacks

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    8/32

    Objectives

    What is a stack ?

    Applications

    Concept of pushpop routines

    !mplementation using arra"s

    Developing a function librar" for stacks

    Creating solutions using stacks

    #aking stacks more practical & useful

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    9/32

    What is a Stack ?

    Stack is a t"pe of a $ist

    $ast in %irst Out $!%O '

    (ew item is place) in the stack on the top

    *he top most item is taken out first

    Access to a stack is strictl" through the top

    +est of the stack is conceptuall" unavailable

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    10/32

    What is a Stack ?

    Stack is a t"pe of a $ist

    $ast in %irst Out $!%O '

    (ew item is place) in the stack on the top

    *he top most item is taken out first

    Access to a stack is strictl" through the top

    +est of the stack is conceptuall" unavailable

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    11/32

    What is a Stack ?

    Stack is a t"pe of a $ist

    $ast in %irst Out $!%O '

    (ew item is place) in the stack on the top

    *he top most item is taken out first

    Access to a stack is strictl" through the top

    +est of the stack is conceptuall" unavailable

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    12/32

    What is a Stack ?

    Stack is a t"pe of a $ist

    $ast in %irst Out $!%O '

    (ew item is place) in the stack on the top

    *he top most item is taken out first

    Access to a stack is strictl" through the top

    +est of the stack is conceptuall" unavailable

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    13/32

    What is a Stack ?

    Stack is a t"pe of a $ist

    $ast in %irst Out $!%O '

    (ew item is place) in the stack on the top

    *he top most item is taken out first

    Access to a stack is strictl" through the top

    +est of the stack is conceptuall" unavailable

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    14/32

    What is a Stack ?

    Stack is a t"pe of a $ist

    $ast in %irst Out $!%O '

    (ew item is place) in the stack on the top

    *he top most item is taken out first

    Access to a stack is strictl" through the top

    +est of the stack is conceptuall" unavailable

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    15/32

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    16/32

    Stan)ar) roperties of Stacks

    push-'

    *his function places - on top of the stack

    "/pop'

    *his function removes the top item an) returns it

    isempt"'

    *his function returns 0 if the stack is empt"

    initiali1e'

    *his function initiali1es a stack to be empt"

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    17/32

    ,-amples

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    18/32

    !mplementing Stacks

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    19/32

    !mplementing Stacks

    2)efine #A3S*AC4 0555

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    20/32

    !mplementing Stacks

    2)efine #A3S*AC4 0555

    int stack)ata6#A3S*AC478

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    21/32

    !mplementing Stacks

    2)efine #A3S*AC4 0555

    int stack)ata6#A3S*AC478

    int top/58

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    22/32

    !mplementing Stacks

    stack9initiali1e '

    :

    ;

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    23/32

    !mplementing Stacks

    stack9initiali1e '

    :

    top /

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    24/32

    !mplementing Stacks

    stack9initiali1e '

    :

    top /

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    25/32

    !mplementing Stacks

    stack9initiali1e '

    :

    top /

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    26/32

    !mplementing Stacks

    push ='

    < increases top to make it point to a new location.

    < Stores the )ata = in this location

    push int ) '

    :

    top>>8

    stack)ata6top7/)8

    ;

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    27/32

    !mplementing Stacks

    "/pop'

    < +ea)s the )ata at location pointe) b" top

    < Decrements top

    < +eturns the )ata

    int pop'

    :

    int )8

    )/stack)ata6top78

    top

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    28/32

    %unctions $ibrar"

    #ake a program file containing the global variables nee)e) for stack an) all the functions specific to stacks.

    +emove the main&' function after testing all the stack functions.

    *his file can now be consi)ere) as a librar" to implement stacks? an) can be inclu)e) in an" program where this functionalit" is

    nee)e).

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    29/32

    %unctions $ibrar"

    2inclu)e@st)io.hA

    2inclu)e Bstacks.cC

    main&'

    :

    "our application co)e which reuires stacks

    ;

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    30/32

    Solution !)eas using stacks

    - ,-pression evaluation compiler )esign '

    - Decimal to binar" conversion

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    31/32

    Solution !)eas using stacks

    - ,-pression evaluation & compiler )esign '

    Matching opening and closing braces of all kinds of priority as well as embedded depths

    - Decimal to binar" conversion

  • 8/13/2019 Lecture 1 Stacks.pptx 0

    32/32

    Solution !)eas using stacks

    - ,-pression evaluation & compiler )esign '

    Matching opening and closing braces of all kinds of priority as well as embedded depths

    - Decimal to binar" conversion

    A decimal is divided by 2, and the remainder forms the lowest significant digit of the binary number and the quotient is further

    successively divided by 2 to get the next significant binary digits.