0% found this document useful (0 votes)
25 views16 pages

DS Unit III

The document discusses Abstract Data Types (ADTs) focusing on stacks and queues, explaining their operations and implementations. It describes stack operations such as push and pop, and queue operations like enqueue and dequeue, along with examples and code snippets. Additionally, it highlights the differences between stacks and queues, their applications, and the advantages of circular queues over linear queues.

Uploaded by

pooja
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)
25 views16 pages

DS Unit III

The document discusses Abstract Data Types (ADTs) focusing on stacks and queues, explaining their operations and implementations. It describes stack operations such as push and pop, and queue operations like enqueue and dequeue, along with examples and code snippets. Additionally, it highlights the differences between stacks and queues, their applications, and the advantages of circular queues over linear queues.

Uploaded by

pooja
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/ 16

Subjet.

S.E.

UNIT:3 Stack and ueque


Q) what iS ADT Exp1ain stack as an ADT
ApT CAbstrat Datatype)
It specifles what
operations Qre Ao be performed
but no hou these operations win be ImpleMented
his abstraltidn allouoS programmers { USe a
dototupe without needing to knoo the internal
workings at how it is represented in mnemony or
managedinternally
Sxomplei- stak
Stak as
as Cn ADT
A stok is aa specific type ofOf ADT hat Opectes on
Last in first out (LIF o)prinple
operations on gtak
Push
Addson elementtothe topofStcK*

Rernoves the elenent Erom the top of the stotk


returns it

Checks whether thestocK is Emnpty

check S whether the stack is fulL.

Returns the element at the top of the StocK


without removing it
ViSi2e
Returns the nUmbe of elenents in the stQck
write a strvcture for stoK USing orrgy write PusH A pop
Aunctionfor stacr
Using array (or
t inude <stdio-h>

Struct stock
int arr[MAX]
int top
Noid initiali2e (Struet staks){
S tops

Void push (struct stacrtsintvalue)

print£ ("stack oyerfouwln")

else
s’top +t
VOlse
printfled pushed into stack \n" vlue);

int pop(strut statk4s)


iF (s >top -)
printf (statk vnderfou\n";
returny
else
int popped. yalue Es’ arr (S’ top)
5 ’ top
eturn poppedvelue

it main ()

push (45, 4D);


push ( ts, 20)
push ( s30)

printf(popped elenent hln", pop(s))


printf("popped e ementi9lod\n" Pop (RS))

pushed into stok.


20 pushed into Statk
3o pushed into sto K
popped element 3o
Dopped element i 2o
what is Queue Explain InsertiDO 4 Delesion operation ia
cOueue uwith Suitoble Diag ram [Gm]
CGm
’ ueue is a lin ear data
data
strUcture that fonows the first in
StVUture
First Out (EIfO) prinipl e i e first eleNent added to the gueue
wil) be fhe irst one to be removed t st added element
to +he queue ill be remove list fo
Engueue Insertion opero tion)
Enqueue 0 opera tion io Queue adds (or stores) an elemen
to the end Of the
queue
The fougwing steps Shouia be toxen t0 enqueue cinsert)
data into o queue
check iÀ the gueue is Fu)
step2 If the queue
Step 31 11f the queu e is not ful incremmt the
the ear pb inter
to pojnt the next empty sp0te
Step 4! Add the data element to the Queue location uhere

steps
the ner is
Return
pointing
Sucess

mpty space

engueue first e1emet:

1
cngueue second element
engueur third element
s>

engueue Fourth element.

engueue iCth element

Degueue (Deletion operotion)


Remoyes (Or acee) the first elenent from the
Steps
ueue 4he fooing Steps Qre taken to perform
operotion
the
dequeueCheck if the queue is empty
step 1
Step 2 1E the queue 1S empty ettbrn the underflo)
error fexit
Step 3! If he queu 1s no+ empty access the
dota uohere the front is pointing
Step 4 \rernent the Front pointer to point to the
next aNailable data elenent
Step 5 Retum Sucess
Initial ueue

pequeue firs t element


J

L
Dequeue second elemnent

t
Dequeue thind element

5) o
Dequeue fourth element.
4
empty ueue
write (functions for: CGMJ
) Enqueue inlinear queue
i) Dequeue in ciculor
Cirular queue
i)
inchude <stdioh
tt
define MAx 100
typedef struct
int items CMAX]
int front, rear
Queue
void int cOueue ( Queue*g)
9frontrhj

intis UL(Queuef)

int is Ernpty ( ueue * )

q ’ Front > q’reor

Void engueue(oueyety, int yalue)

lprint (" Queue is fu \n"; eturn;


if (is empBy (92)
1 q ’ front o}
An". vave);
printf("olo d engu eued to queue,
t inude <stdio:h>
inlde <stdib h
define MAX lO0
type def strvct
lint items [MAX1;
int font,eor
Jcircular Queue
Void int crculor Queue (circular Queuet9)

9> ront -)

ìnt is Ermpty CCirtulQY Queue *9)


reiuro
int is FuIlirCulor Qu eue *9)
teiurn (qsreGT t)oAAXg ’ front;
int dequeue (cirt ulaY Queue tg) i
iiF Cis Ernpiy (9)
i pintf ( cQueue is eriptyin);
retwrn

int vaiue tq ’ jterms I q front


a frot =-1
4’ear =-}

else

qTeturn vane
Evaiuote the fol10uoing postfix expression witn the
help tostack.
5 3 + G9* 35* +

stack Operation Evaluation.


Reoding
3
3
5

D
24

24

Thus the rerut Of postfix enpresion 53+6 2/*3 st+


i5 39.
(onwert the foliowing infix eapression to potfix Vsing
stack Cshbw all steps properly ): a4h* (cld ta) /b sr
Inpt string 8tack

ab
ab
ab
+* abc
.+*( abc
abcd
+*c
abcd
abcda
obcda!
abc da$L
b 4*| abc d a b

final pofix Expres sion 's abc do b t t


stace coueue

Parameters StacK Queue


Basics Tt is a lineor dato
Struture The objects struLtUre.The objec ts
emoved
aTe removed Inserted Cre rermoved Inyerted
at the samne end faom different ends

working lt folows the Last In +folows he Eirst ln


Principle first oUt priniple irst ou+ (FIfD) prin1ple
(UFO)
Operotions Stack Uses push f Oueue vses enqueue
pop S dequeue
Operations. Hs Operaions

Visuai2ation Visualize a
the staeK as a vertical Queue as
Collectien ColleiOn
Tmple Simpler in stacK MOre Comple* in
menta+i on
Queve than stacK
Define Queue. usat are coditionsfbr CQueue
empty
\Oueue fun' when gueu e is inplenented using Array
Defnation
Queue is a dato-steture in whichdditionof on
elennent is alowed at neend 4renovalof an
elenent ts Qlloue at other end is (aled Cs"Queue

(onditions for oueue empty!0ueue fu' when queue


sImplemented Using ATay
int queue (s7;
int frDit -ly
int eor =-L

nEpqueue operation
if (si2e) =MAR SIZ)
prìntf("Oueue fUI \n);?

rear (regrt 1) °o MAX-S)2:


queuerar] element
Size t t ;

) Dequeue. cperation.
printf("Queue Empt yln"):}
Front (ront 1) /o MA% S12E
Si2e--}
yi9 whot aree applicatien of stack C3M]
Represen4 statk for decima) to binary conversion (s4), to()
’)AOC eII0ng function- cais
2) Memory Manogement
4) Recursiv e funtion cals.

Decima1 to Binory conversion(5c)oto(-),


Decimcal_No Ouotient Remainder stocK)

Pop the stock


Nou popping the stat gives
Resut
Thus S6Di is (t1000),:
what are the disodvont ag es Of lìneaY queue Sug9est a
SUitable method to overcome them.

Oisodvontages
) Onee- through traversal
s)MemeTY wastoige
3) can't engueue more elements jf lat poition is Octupie
4) ueu e is full when cear is at ast array posihion,
Even if there are enpty Spaces in the queue
it's ful when the reor is at the lást array poiition

A Crculorqueue can vertorme hesr disadyontagea


Save mnemory
Flexible insertion tdele \On
"Saves memory

Convert the given infix expreion to o postfix epresS0On


Uçing statki (a^b)*cd<d.

Reoding StacK oVtput

(A
ab
) ab

ab^c
Qb^c
ab^c*d
" inal PostEix Evesrion

You might also like