0% found this document useful (0 votes)
3 views9 pages

Stack

The document explains the concept of a stack data structure, which operates on a Last In, First Out (LIFO) principle. It details stack operations such as Push, Pop, and Peek, as well as potential issues like overflow and underflow. Additionally, it discusses applications of stacks in programming, including backtracking and undo mechanisms.

Uploaded by

schoolwork7002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Stack

The document explains the concept of a stack data structure, which operates on a Last In, First Out (LIFO) principle. It details stack operations such as Push, Pop, and Peek, as well as potential issues like overflow and underflow. Additionally, it discusses applications of stacks in programming, including backtracking and undo mechanisms.

Uploaded by

schoolwork7002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STACK

Data Structule
a nomed Roup of doda ofAiosant
A DatAa Slsuctunt 1 can he
and
ohich is stpred in a specrhe o
doda pes as
kune, ho3
stuucune
U - defhned
oel-delined
e
unit, A dada stsuuc
as asimgle
pho ces8ed
ehviou and pmDpexht.
opencchons,

Stack stuctne
stsauctae in uhich
Unea Seoquin ce
A Stack i a
end 1e
take placa
plae ony
ony
nsebon dele tion can
is call
Cale ed
d LTFO
LIFO
Stack A
Because of thM 8,
Stack's top. s
8 tt
suuc
ctt
u he
n e. LLFO means,
LLFO meana3
data
LastIn Fist out) to be deluted
delted.
ha finst
toould be
elemerd inseste last can SMOV onlp
o books, whel Hou
a pil
PlLe

top boOk
PUSH and POP
o peAah ons ons
opeah
wo mayor
Stack pevfoo ms ofthe Stack, it )
elemet
added top
on tup
hen an
(
called Push penath en. he Stace
fomthe top of
Semoved
eloment J
when
called POP opeNahon
it

Othe Stack eAB


the Stack's top
Peek: Refas to inspecting the value a
it.
wHhout sumovT{ tkm
an Hemn
e s to pUh
PU on
one toes
t
to situation when
Oveflow: Refes ohen h e
Cccuns
occos Ohan
in stack t h a t
full. This situation
gho
ulhel o
ixed Cand Cannot
Biae of the stack
ne
o accOmmocote
Hhene no memoH deft
»Undaflo Refens to tuation hon One tie to
to pop
po
an cm om an
empt stack

Given Bourydad stack of capaitt 4hich intally


a
empty d a pictuns of the stath aftU each of t h tllowna

seps (v) Pubh c'


Stack is empty i) Push 'a' ii) Pusshbb
vi) Aush e
(v) PopP i Ash 'd Pop
(xi) Pop ii) Pop
XPush 8
civ Pobp v) Pop

An C Stack t empt top None

) Push 'o' to p O

d) Psh top1

T
top= 2
) Push 'c
ab C

) Pop top 1

v i Puh top- 2
a bdA
(vit) Pop tup1
ob

) Push 'e top 2

K)Ash top3 b l e1

a b le 1f Ovu-flow
PLsh top 3

xi PoP top 2 abe

ii) Pop topI

top Op
PoP

top None
pop
a
O
top None
poP Undekflou
Implemertodion of stack using ist
def Pushl, em)
[Link] iHem
top Nen(stk) -1

def PopCstk)
f len(Atk) 0
Setn "Undeflow
else

tem Atk pop C)


f Aen( stk) = =
0
to p N one
else
top Len( tk) - 1

sctun km

def Peek( stk)


if JenAtk) 0:
Stuhn "Undaflow"
elbe
top en(Atk) -
Setuan tkltop]

def Dispy (stle)


4 e n 3tk) = = 0

u d (" Stauck empt


else
to p
den (stk)- 1

pnnt (tk [top, "tU p)


tu am s anaetop-,1, -1)
pund( 4k[al)
Stack
top None

ohile T3ue
p nt Stack Opehath ons)
punt (1. Push'")
poit (" 2. Pop ")
pint( 3 . Peek")

pount (" 4. Displa Steck)


prunt ( 5. Exit")
ch i r t (input Enter HoUuA choice (1-5)"))
ch = = 1

Hem= it(input (" Entel item "))


Psh stciek, i+em)
elif ch= = 2

Hem Pop(Stack)
T = =
"Ond aflow
pirt (" Underflow Stack i enpb
ele
pumd (" Popped iem 3

elt ch = = 3

em Peek(Stack )
i Hem = "Underflo"
psunt (" Undefloo, Stack s empty)
else
punt(Topmot i4em i s " , ittm
eluf ch - 4
Displa (Stack)
eli ch 5 .
bsaa K

else
pnt( Invalud Chove)
wite a functionb Push (A), whene A aUist
of numbes. Forom this Jist push all tumb di visiblu
by i nto a Stach implemented ubinga ist. Display
he stack iit hos at least one element Ofhau Ibe

display appovpsute e N 0 me33}


Any def Push(Asn) .
Stack )
top None

fos dn A

[Link] (t
topAen (Stack) -

1 top None
pod (" Statk is empt

else
*1)
foo n ange ltop -1, -1,
paint ( Stack [ top))

Pop (A), whe


wue
wute o function in Pathon
A astack implementc a ust of numbas.
A
The funcion 3 ns 4he Value deleded toonm

Stack
Applications of Stack
I. The
The comprens use Stck to sto thu puvto stale
of a coled duumg ne ungi on
1ogiam hn a function o

2. Anothei impoatavt Stack applicati om J backthackirg. Botktacking


LAsed in a losge numbex of puzzles, üke Sudoku
in optim2cihon psobfems Such as Knapsack.

opeach oM
3 Undo mechanism untext edstos. Thib
stack
[Link] by keeping alltext changesi n a

Q1 Fi l/ in the blanks
data.
meas a9 a2ah m of
and
.AA data stuctwL has well defined
knodn as LIFO List.
C. A isauneas Ust, also
of dato elamerts
mutable quan a
d A i a

indexed +hei pasition.

means accessIM ol VISitmg each element


e
ofany d o t a stsuuetune
or
indeahion ot elument
is the tum Coinedfux

in a stack ist.
to umo ve an elument
the stack, if a us tu
8In
4han it called
tomt h e empty stack,
8
2 MCG
The 0f insotirg an elament in statk i callea
(a The pvcess
0) Cate ui) Push ) Evaluuation (v) Pop

colled.
() phocess of JuMoVIT9 on loment fom Statk i
The

Cseate Ci) Push (i) Evaluation (M) Pbp

)In a stack, if a usd tnes to amove an elumet


om an empy statk, 4he situahn s called
() Undenftous ) EmptH collecti
n) Ovef lous )Gonboe Collectiom
Psring
Pushing an element into o stack havmg ive eements
and a Stack of S12e S, +hen the stack becomes
(v) OveAftous
Usa-flow )Crash ri) Onderflo
in stack Osdened. What is 4he meanNr
Ce Enti a a

of th stacement ?
A collechion of stack can be sosted
mo be compaed oi4h openahan.
Gi Stack erdu
entueS ae stoyed in a Jnkad Ust .

ii) The

iv) The
a 39urdial enty thadt o n bg ene,

appucatium mH use Stack ?


(f) which of +he follodima
pountheses
balanci ng progaam
A
vahiahlu at un tim
0)Takirg of local
i Compi l e Symdax Apaly zel
(v Al f the above
Q3 lHe a povgAam to implement aStatk frr 4hesa booh
details (book no, book
mme). That ib not each Lem,ilem
nod of Stack cont ainM too types of inforamotion.a book
hO and Hs name. Just implemer Push opelation).

def PUSH ( Stk)


bno intmput (" Edter book no, to be inbtd))
Dname I n p u t (" Entl book name to be mbted

Item bno, b rame )


Stk. append ((IHkm)
top en (stk) - L

w i t e add (Books) and dalek ( Books) muthod in Python


to add and emove Books considexing +hum to act as

append )) & pop() opeations i n Stack.

QS ut Add Custo meausbmel) and DelateCusomea(lustbmea

muthods. in thor to add a dele


nu Cusbmer a

+hem
som a List of Customun Names, consideurg
Custpme from
to act Psh pop opeohong of th stack dota

stuctue

You might also like