Sunteți pe pagina 1din 32

Data structures & Algorithms

Stacks

S. Swami

Objectives

What is a stack ?

Objectives

What is a stack ? Applications

Objectives

What is a stack ? Applications Concept of push pop routines

Objectives

What is a stack ? Applications Concept of push pop routines !mplementation using arra"s

Objectives

What is a stack ? Applications Concept of push pop routines !mplementation using arra"s Developing a function librar" for stacks

Objectives

What is a stack ? Applications Concept of push pop routines !mplementation using arra"s Developing a function librar" for stacks Creating solutions using stacks

Objectives

What is a stack ? Applications Concept of push pop routines !mplementation using arra"s Developing a function librar" for stacks Creating solutions using stacks #aking stacks more practical & useful

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

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

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

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

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

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

Applications

Operating s"stem manages function calls using stacks ,-pression evaluation *emporar" storage in $!%O manner

,-amples

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"

,-amples

!mplementing Stacks

!mplementing Stacks
2)efine #A3S*AC4 0555

!mplementing Stacks
2)efine #A3S*AC4 0555 int stack)ata6#A3S*AC478

!mplementing Stacks
2)efine #A3S*AC4 0555 int stack)ata6#A3S*AC478 int top/58

!mplementing Stacks
stack9initiali1e &' :

!mplementing Stacks
stack9initiali1e &' : top / <08 ;

!mplementing Stacks
stack9initiali1e &' : top / <08 ;

stack9isempt"&' :

!mplementing Stacks
stack9initiali1e &' : top / <08 ;

stack9isempt"&' : if&top//<0' return 08 else return 58 ;

!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 ;

!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 return )8 ;

%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).

%unctions $ibrar"
2inclu)e@st)io.hA 2inclu)e Bstacks.cC main&' : "our application co)e which reDuires stacks ;

Solution !)eas using stacks

,-pression evaluation & compiler )esign '

Decimal to binar" conversion

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

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.

S-ar putea să vă placă și