[go: up one dir, main page]

0% found this document useful (0 votes)
893 views10 pages

Computer science 2nd pu on stack chapter notes

The document discusses the concept of stacks as a linear data structure that follows the Last In First Out (LIFO) principle, detailing its operations such as push and pop. It provides examples of stack applications, including undo functionality in software and navigating web pages. Additionally, it includes a Python implementation of a stack and the conversion of infix expressions to postfix notation.
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)
893 views10 pages

Computer science 2nd pu on stack chapter notes

The document discusses the concept of stacks as a linear data structure that follows the Last In First Out (LIFO) principle, detailing its operations such as push and pop. It provides examples of stack applications, including undo functionality in software and navigating web pages. Additionally, it includes a Python implementation of a stack and the conversion of infix expressions to postfix notation.
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
You are on page 1/ 10

PAGE NO

DATE

02.STACK.

.Data atudue
A data struduu datins a machanim o sln,oQunig Ord
access dato with openations hat can e tfiity potormad
on th data

Stack and queie ane th tuo popalon datasucug wnd


inpsognmming. biie

Stack
tock iu a lincan dota Ahucua ohu inction and dallion
lan elment akes plau at the sameiud cinny akciud as
a

whee
Stack follougs tho ast in finst out (LIFO) prncigle,ce,
gint one to
tha
the elenent vchich waa inseted lat aill be
todun out iom stack
Applcatiaa stack in nual iy
oStak og books
AStack o bozs
aMultinle ehains in a vetical p i l . t
Plats ananged ome among e othun.

olo seweseia
chanaoten3
shing can be, casilg dore by
hin appeanane in tn
sReening chanauina stack.
placing the
The teat/imag editas
St allouoA usUL to undo sr udo editing oith the most
uCant aditing is undone o sedoe.The systen wses a stack
kupaack a changea madkd aick
Ch Back button on the bro8RA.
alhile bouig a w, e moue kom one
pag
honothu, bu accaAsing lisks betuuxen thn, in eidn to go
back to the dast uisiad wsb page, e may use hei back
kutton on te bnowse, in this co, the histoy o boayd
Page is mainaind a stack.

C)Eopnenion Evaluation
ahil uoriinq any auithmeie equalibn uëte paianthuis
Alhile exitiling the pcqnaim, the. compido chacks kgn matched
pananthes ncae thi pananthis is mismatchad, the compin
Inuda to thos an e1n97.
To handlemotching a paranthiastack is wnd

Opnations on ack

The opuations penkaxtd nthe atack ae push and pop


o PUSH Dpenakon
PUSH adds a ne element a the top og the sack.

taecan add an elerent to a Aack uniltin tul


Tuping
Tujing to add on element to a gullsack usult inen cxcetiom
eald
PAGE NO
DATE:

POP openation
o POP opuation is_uud to umoUe lhe topnost ement oy th

ale can dalte elementa gioma btack until it isi emptiy


lhen stack is empty,no00 mou deltion can be done
stack,9usults in an
s i n g to dalet an element kom an empty

Implamntation gstack in pthan.


The simple way to implemeni a stack in python is using thn
datatupe list )tei at
o Aktack p hecan fia eiten oi that list as TOPto insent
and ppp)o_ the list
ta le ane sing buit-in mthads opend()
implementing stack ie, ae
add inoelemena to the T0P C; stack,hcle
append to
elenento framstack
pop umues the
Cstack dlase)in udich
Ezanple Prognam to chuat a istack
(alase)
dSnset on dleto eluenti
Check i the stack is empty (no glases in the stack)
6o (glases in the stack)
c Find th number o alement
top most elemet (nunben on
the topmost
coRead the vale of the

shall datue he golousing gundions to posfprun


he proqnam
thuse spenatama
oLot s ceati an empty stack. namd glas stack,by asigning an
lst to the idirtiin inainad'qlass stad.
nat
qlas stack List()

oA kunction namd ia unptu() to check cohth thu atad (alem


thu sad

utun Fal

taA untion named op Push to inst (PUSH) aneseamut in


stock,oe çM tha but-in mdhod opband [).o odd an ledinust
in stack that aluas adda at thu end o lit

dey qpPushl¡las stadk ,lament)


alas tackuappznd (elkeat)
a):A kuntion named size ko neod the number o* elementa in the

nd he nunbn C elamtns in the glas stack


dak ize Cala gtatk)
hlusn lunlglas stark)
SA Runtion named top() to ead thu most gent lumeni (TOP)

dek top (qla stack):


PAGE NO:
DATE

etun Nont
else

element -alas stack(x-]


nutun elment

(6)A kuncaon namd opPop to det the top most elemwt om te


stack The fundian ginst dhucks. chithn the stack i tmply on not,4
i nat emptyit numoues thu top most element qom it.f
We shall we the built-in mtthod popl) pythn list Bhat
mova the elenent Aom the end o th lit

dag qpkapglan stack):


priot ('undeflew')
nun Non

aaunlalas stack.pop())

ramed displas to shouo the corstenti o the sack.


CA gunction

daf diplay Calan stadk J:


X- lun Cgla,stack)
stack ae :")
print ("unent lementt in the
(x--):
fori in xang
#Guat enpl stack
Lglas stachkat)
#add eemens to atack
elumentalas '
Paint CPushing alamern', demeit)
bpfuah (glas skack elrnat)
element 'glas '
qp Push (glas tock _elment)
# diplay Nubn o eleienta in stack
pint (Cumet numbo eamenta in stackiize Cglas Aad)
#aat an eement nom the sacki
lament cpfpCalas stac)
paint "Popped elumentis", clemat )
# add n demen o sacki
amentglass 3'
paint("Puhing eamenb sack eluhust)
lelarnani opPush (glas stack, etenent)
diplay the Jast ment added. to the istack
print ('kop lamat isop lalan tak)
# diglay all eleent in te stack
diplay (glas stack)
PAGE NO:
DATE:

ikum sopPop lalos sask)


ik itom=E Aon.
print (Statk is empt now

else:
print ("pintCpopped lameit iaitu)

Pusking elurant glas


Ruhing elument glaa s
wnent numben cf uumenta in sack is
elernent is glau d
popoxd clamat
puling elanunt glay 3
kop elunet iss glas 3
Cunet element in the tock an

glau a
-pepped elumut i glas a
Pppped oluent i glas
undefous
ack in emp nas
Notations fon Anithmelic Enpnsiana.

Anithmetie LxpIs0oYA a onten wsing opuatas botwen


operands, aauatad Wing pananthusia () in comçlz eaptkin,
land golloud by inix upsntation and eoDMAS Jula
Polih mathurmatician Jon ukauaiz in the 1904 i
aditfuunt ny o7 suqnsaning aithmeic czpusiors,calud
ploh Notatin

|Jafiz Pastha and pauiz notatinns.


Snkix ezprasiors
Opratena au placud in btioun the QpuLanda aaL callud

Ezx Z+4ytz, 32 (4+s) (zy) /(z*s)

St i alo beon o% Polish notation, thue qpeatos au plaud


bakozu fhe onmesponding opexanda anu callad pouhia expnain
E+xy, +Z*y #3445 /+ysz5

plaud affa te conupondin cpnands an called pozepao


|Cx: 4t. y z+ 34S+#ytzs*/

dota sucRL..
Scan the infisz exprasioÝn hom ugt to night
PAGE N0:
DATE:

takhenua
a wqet on qpand add it to thu potja cxpauian
and ih ue gat an tpenaton e pasunihuja add it io the stack
by maintaäning thia pnudun.
Conutlan of pnson hom Sniz to postia notation.

Stap Qa St is an qpesand Clike A-z,0-4 ete) add it diucty

Sap 0a:- S it is e'push it onto he stack

Stap 04- it is "'pop_ Rom thu stack and add to potpia until
yau kind a C,then discaxd both panunthuis.

Shep os- it u an ogoato (t, -,*./,ta) :

Cunnnt cpate has highn podunt, push it bthe tack.


(aElx. pop pom thu stack to postjir wntil you tind an
openato. usth louer paacudence er stadk is empl. Then pash
ihu apenato
Step D&zAfan the umde expzunian ia LAnnLd. pop ucuyiing uft
in the stadk to the postj cpyusion.

Convet (a6t)/(z*8) into posthe upnO

anbol Action Stack Ouput


PUSH C
Add to otput
Aush (+
Add to cutput
POP
ayt
PUSH
PUSH
ayt
0k Add to autput
PUSH
aytz
ytz
Add to autput
POP
ytzg*/

Result : s +z8 */

You might also like