0% found this document useful (0 votes)
21 views

Data Structures 2

Notes Of Data Structures For BTECH Engineering

Uploaded by

Sandeep Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Data Structures 2

Notes Of Data Structures For BTECH Engineering

Uploaded by

Sandeep Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

gs5IAOSR

ONIT-0
v m ced

STAck Deta shuctuse


in wlhich inseshion
IE is a lineav daba sruchure

aB end called op follows Last


and dele hon hen one

in last out
in first-Out-0xdes (LIFo) (ov) Fivst

the followinq ree basic opeyationm


Mainly
de Pesfovmed in he gtack.

Basic opesahions in Btack

Pusb

Pop
Peek ®) Top
I S Emphy
1oP Push

Loyt in,
frs gt out

Pop
Inseytion and delebion operakiong occuY at one
end
Pusht) opexätion
Adds dn iten in the gtack. t involveg in e

of steps.

checks if the Btack is


Emph or
ful
f he stack is full, Produces an evse and exit-{oest

I F he Btack is
Emply called "undes ffow", Incye
top o point nert emphy 8pae.
Adds data element to shack locaion, where top is
Poinhin
hen eturns Phe xesult 19551AoSR
Puh operakon Vcvd are
ToP

LA stact
stact

data)
void push lint

top topt
Stack [Top) daBa
=

Else
is tull \n)
inkerk data, stack
(" ould not
sin f.

Popt) opevation
Atack is
conent while Xemoving the
the
ACcesing

It involves foll owing seps


known as Pop operfion.
not
step: checks it sBak is emphy 8
T F the Btack is empty, Produces ado esvos and Exit
acceys the
1 F he stack is full called "oveflow",
decYEages
data element at Wlhich top Is Poinin9 and

the top yalue by


Then vefusns the vesult

Top-
6 ToP

sta:e
i n int pop (int data) 551AOS%
vmharcit
ifi s emphyt)

data = sFackl hp1


top - bp-1
reurn data;

else
he daba n");
Printf ( colud not rebieve

Peek opexaio
Peek 1s an opexaion Bn Certaio dbstract dat
stacks
yPes, Specially Bequnial collecions Buch as

and queuy . Which vetums 'Hhe value of the top(4fsent)

o he Colle chion nlithout semoviog the element fsom he

Collecion

CA
when We gave peek operaion, it gives top ofnesttk

PeekC) =C
S EmptyC operation
he items 8re popped in the veveised oxder in

Which they are Pushed. Ie the 3tack i's Empt, hen

is called
undex-flow" condition
Peek B) top e turns top element d stack.
1sEmpb Returns tsue if stack is Empt
Pty elsef

els
Representaion o f a s i c k using Asays S61AoSSS
V m Chand in
stack is a data stsuctuve heut can be representes
A

as an asay of elemede.
Stoe dn Jdeved: list ments.
to
is used
Ao a s r a y
of Stack is one of the
TePsesentahon
tor
an aray
USinq the data
to manaoe
technigues
easy betwe en
an a a y and
diffevence

a majos
But here is

stack
is fiaed
an asvay
Si3e of
no fixed Size
8tack, theve is
While in a number
numbes ofF
of
with
Wi h the
the
is changed
of stack
Since the Si3e
leted to and
fsom it
insestedlo) de
Elenents
be uged to epregent
difference an avay Can
Despite the
fov max kize ; big eough
an avzay
a Btack by takinq
a stack.
to manage

Stack a an aY

40 ToP
3 4 S
13
a3 1 8 1 3 18 400

3 Avay
12
Stack

Foy exarmple

l2,8,21,33,8
We are qiven a 8Back of elemenks:

3 4
S
a a 1 a3 1 4

Avsa
slep Vmch
Push
Push(4)
Top40)
at a(5)
Elemen rs inderted
()
) ( ) (4)
40
Poshl4o)
Step:
+ push (18)

Topl18)
Element is ingeted at a{4
(), ) ) , (3) 4)5)
8 4

push18) ToP

Push (33)
Top33
*lement is insested at a13)
3

3 3 18 40

Push(33) ToP

sep
push (a1)
*Top21
glement is inserted at alaj

a1 33 8 40

Push21) ToP
ster5
* Push (o8)
Top 8
*Element is inketed at ala
33 s 40
push (&)
ToP Vmchandana
step 6
Push (1a)
* Tbp = 1

element is
insested at ai63
3

12 333 1g 4
Pusha) ToP

Display
12 2133 1 4

Alglihm tos push( ) operion


then wsite
I Top > = 5i3e - I
(max
stack is ovexfow"

Top Topt1
stack Top] =X
Algthm for pop ) operahion

I Top ==-) hen write

Stack is undesftous'.

Rekuxn sBack[top]
Top Top-
Poqsam fp Repsesentakion of tack using o
#include stdio. h>

Include x chyperh
t define
void Pgh (int).tot pop(void):
Void display (void):

in topz -i, stfma]


void push (intx)
(top -mas-!
Printf ( stack is ovntow") VmCh
else
St[+tbp]eX,

int Pop (void)

if (top- -

Pinhf (shack is underHow");


else
ehnstitbp--});

void display(void)

if (top : -1)
Printf (sack is empty "
e lse

Prinbf (stack now! | \n");


for (int i- to p; i>=0;
1--)
Printf d", stfi);

int main t)

int ch,X

do
Printf (*\n Push \t a. pop \t 3.display ")
Scan f (d', Zch);
Switch (ch)
Scanf (" Yd ", 8 x); Vmelhan.rioe
ush (x)

Cae X Pop);
element 1s d "x);
Printf ( popped
break
Case 3 displayt
love ak
Coye 4 Exit (o)

lhile ( ch): 4);


e turn 0

IpUsh 2 . pop 3. display


an element 5
Enter

1
Push 2Pop 3. display
Enter an Element 3

Push 2Pop 3.display

Entex an element 9

Push 2pop 3.displaya


PoPPed element is
push! PoP 3.display 3
stack now!! :
5 3
Paoqom fv» eVeye of n c k 951Ao
include stdioh> Vm:ndodae
deine ma 5

void push (int);


int Pop (void );
-12
int st fma1, top:
Void push ( intX)

if (top- ma1-1))
printf (" stack is ovesflou")

else
stf++top3 - X;

int pop (void)

if (top=-1)
Printf (" stack is undevflow');
else
veturn (s+ ltop--]);

int main t )

int al 5, 43,2, 1y,in;


Si3e of
(a)/si3e of (aton);
tos
(i-o; izn, i+t)
Push (ati3);
print( The bst in veveyse alder');
for (i-o; ikn; i++)
Psintf( Id, Popt )
Yetuin 0,
o/pr The isin everse ade a 3 45
APPlications o tack
Revevsing aist Gmchanda
Facovial calculalion

I n x to postfix ConvesSion (or) Tsänsfovmation


Avithemäkc Expsession
Evalua ing

Back Hacking
Mem31y Manaqement

Convession
Inh o posix
When
of the torm a op b.
The Expresion
Ini Expression

in between every par of opesänds.


is
-

an opex ator
of the form
ab op
The expression
Posth Expsession
toi every Pair of opevands.
an
ã n opesator is followed

step Algbithm
consider the nextelement in the input
step1
stpa 1f it is operänd, display it
insest it into
step 3; If it is opening paventhe sis C,
Stack
Shep 4 F it is an opesaBor , then if Btack is
emp
insest the opevatov on stack.

f he top of slack is openinq Parenthesis, insert he

operator on the 'gBack

f i t has hiehes pxiðity than the top of 3tack, insert+


the opesai on tack.

Else, delete Hhe opesatos fsom the stack ancd dapla

it, xepeat Btep 4


step 5 I i t is a clo sing paven thesis,dele te the

fsom stack and display them un il a Opo


opening
and ehs
Pasen thesis is encoun texed Delefe
peninq Paventhesis

Step f there is'mde input, 9o to sep

Step 1f ere is no mde input, de lete he Temain

o pesatos to oútput.(1 5

EXample Convexsion of intfi to Posthi

Example s Suppose, we axe Convesting 3»3/(4-1)+ 6*3


tosm.
£x pressioo into Posi

Epsession 3tack Output

Emphy - highe pi
=next priovty

lowey
3 3 Prioviy

33*

33*

33t4
/
33* 4
-
33*4I

33 41-
334-
3341-l6
6
+* 33*41 /63
33*1-/6
paaxaY

handan
#include Stedio hs
include * chype-h»
chav stack 1o o3
int top -1;
void Push (charx

Stack ++to P]=x;

chay poP C)

if Ctop-z -1)
etusn 12
else
vetuvn. stack [ top-

int Priovity (chas x)

ir (x =(')
selusn o

veun 1

seuzna;
yen 0

int moinl)

cha exP {1oo];


cha xe, X;
Psintf ( Entes the expression: ")
Scanf (" /S", exp)
e exp
While (e '10) 5iAt S
CV m Cha
if (is alnum(ve))
Pintyc"*e);
else tf Cre =e "(')

push(e)
else if ( x e = = ' ) ' )

While (x= Popt)) 4= "(C)


Priotf (c",x )

e lse

while Cpriovity (stack l toP3) >= Priovih)

pin (Xe ", Popt));


Push(*e);

e++

While ttop 1- -1)

Prinf ( ye ", Popt))

Cotes he expoesion 33 * 4 - 6a
ResultIS
EValuahng Axithematic Ep session 1955/oSS3
ochardao
The stäck 3igani s lion is vevy effechve in evaluaking

ithemaic expses si
ons EXpyessions ase sualy ve prexgnted
not ion I n lhich ¬ach operatoY
in ulhat Is Known aß inkiz

is i t t e n blw huo opesands (ie, a+b)


distinquish behween
l i t this notakion, ule must
elthes paventhegis())
a tb)*e and a+ (b*c) by usinq

Some opesator psecedence


Convession

Poish notahon (Pvek1 notakon)


the operatðs
to the notaion in wWhich
IE vefers
Heve no
Paventhesis
two operands
is Placed befove it's
ase vequised.
+ab

Reveyse polish notakon (postfx notaHon)


I efers to the än3logous notahon in klhich the
mo
operator is placed aftes it's two opevands Agai'n
Paxenthesis is sequived

ab+
The psoeduse fos getting the rezult in tolouing Bteps.

Convest he expression in 3evense poish nofah'on(Pastti


push the opes ands into the sBack in the 0der hey
ase appeaT
When any operato opr
encountey then pop tio atopmost

opesands for esecuinq the opexaion


4 Ater he execuion Push the vesult obtained

into the 8tack


execuh'on of apsession
5 Aftes the Compleien
stack.
fina esult vem ins on the top h e

(6+4)
Infs nodakon (3-+4) *
+46 +*
Pest-k notahion 4
Result: 6o

The stack epevaions fov this expsession va


is Ahown below-
4

6 6o

Alqaithm
Add) b postfir Expression

3Re ad Posh kx expresicn lefB to ziqhh until 3

encountered
End
3 F ope3and is Encoun teved, Push it into St

4 I operaov is encounteved, Pop tuo opexards

)A Top elemet

3 B neat Element

3 Evaluak B ofrrator A

5 Set vesult- pop


snd
VmcnArel3n
include < stdio.hb>
# include chypehs
deine max 10
int tr-;

int stmav 3;
void push lint x)

if (t =ma1-1)
Printf oves Flous);
else
stS++t3=X;

in Pop (void)

Ct-1)
Printf (undexftow")
else
selusn (st[t--3);

int eval (chas expt 3)

int i-0, oP1, 0P2,

chas c
while (expti7!='\0')

C expti3
it (is digit Cc))
pushCc-48)
else

op2 PopO,
OPl- Pop);
Switchle)
$5 53
GVm ha
(oPI+0P2); break;
Coge'4' Push
break;
Cae push (op1-òp)
(oPI6p2); break
Cose': push
break
Case' Push (opl/opr) i
loPIaop), brek
Case'a': Push
defat Pzintf t" nvakd expsession )

ve hun CPOPt));

int main ()

chay exp f263;


Printf (" Entes an Postfix £xpremion")
seant Ys", exp);
Paintf ("Result= Zdn", eval(exp));
setusn o,

Enter the ezpression (244)* (4+6)

Result6o

You might also like