Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Asignment

4
al! oþesation
Cpvogram to mplement a!
Linklfst.
+indludo stdio,h>
+ incude stb.h>
Stsud hode

int infto ;
stauct node poey* net;

stsut hed, Stt - NULL;


I|Fundtoh to tsaverse
voîd toave xse ()
?f(Stat NULL)

");
pintf ("n ist is Empty
Suetun;
data
J|Paint the ttemb
strudt node
te stot;
(te| = NUL)
while
("adta= 9,d ln"tenp’info);
ptf
pointf
temp -’ net;
tem
int main)

fnt choice
beingin
ILfuncton to fngest in
void tmye st at begeing

Styut node *tem ; (Stuct


Reny (stauct rode *) maloc (size a
tan (".d",data);
tem .hfo =data ;

tundion to insest în End


oid ihaest at end )

nt data;
tout noda * tem þ*trav; (strud no
size
mp= ( staut hode *J malloc (
my next z NULL;
Cant (?. a", 4data );
stast;
NULL
3-f 6tasd is
NULL)
3f stat =
Stat =temb;

Funtion to ingert any þositon


t dita pos,ie2
tzuct node *emb neo node;
hode ’ preu
ewhode Nut NOL;
intf("Enlr þosihon: );
carfl^ .d"Abas):
t main()

nt choice
ile l1)s
"Inlt .1 To see
In |+ fos Lna fion t stortng n';
" t fox Lniertion at any positon lh"
"In t for 9hsesfion at End In");
fox Deletion ol first elemnt In
"In lt
elontnt ln'
In t fo Deletion o Cast
"inlt fox Deleton e pecii psition
"In It To exat ln ");
.a", chaice);
hoice )

sse (0 ;

F t foont ();

at End 0;

t at positon ();

fist();

end l);

þositon ();

1);
tist delete
()
;
clae
st't; teup
þostrtion: tep
* *node steuet
þos,i-
1; tetete ht
þosition) dolit od y
position Dolatafunction t
tte) foee
NULL:
þaew’Net te’
ele
NULL Start
-
NUL) ==het (staot .94
stot = ten
Påntf("C's+s
tmt
NULL) == tat (s 1f
temp ode stut
Endl) delion void
to
End from delete I|fndion
2) ==(þos if elye
þosition
1 134
NULL; next eusnode n
hewnod;
NULL) ==
NULL ==stat J4 I1
"); choice corred ("In intf P
2 positi On)

nsest and deleta the


inblet to
CPrngan to
nkdist.

cludestdio . hs
cludemaloc.hs
But ink

t info
Foud -ink *hest;

nt numbeo;
stat * Bsevious , *nes 1:
-bud ft
funtion (aeale */
stsuct)
oid Caest- Jist l

Guct node * head= mallbc (size a (stout node));

kyt hoda * þte =mallsc (size a(stout node));


tx ink =NULL;
ead = add = beg (head, data)
t head
le (st | NULL)

intf( d",s ’hode J:


stx = pt ’ ink:
# indude <stdio. h
# indudo <stdbih>
yedey stut Node
ikt nunbeg"
stout Noda ht;
Node,
Void Psh (Node t head.Gnt A)

Node *n z malloc (size a), (Nodo));


hhet head;

wd dolote N(Node*hed, int þsition)


Node * tenm;
Node * jaewj
tem*head;
z * head

or(int P-0; i<posthion; it+)

*head (*had) het


foee (tomp;

e(i==oatn - 14 temp)

free (temb;

-(iþsron - 1 4f-teny)
if (psey ==
NOLL)
boeah
temy temp’ net;

UAile Chead)
pointf(E biJ (%, P] ’ P1n,head ’ hunbe, haad, head ’ nsrct;
head = headnet;

pointf ("In h ");


nt main ()

Nodet ist malloe


Bs+ ’ het NULL ;
(size a (Noda) );
push (4 is4,1):
push(4 Ust).
pus h(lst,35:
ooint i'st-(U);
dolete( Usts 1);
pait Us+ (Ue+);
nexto; -lest
NuL, ag >neyt t
node)); lshuct lSize malloc nodet) -(stro
ct t
it4) (<ni li-1; fo
first; lest=
NUL, next
= first-
ALoJ; dta= first
nodel; (strut Csizemalloc firstlStru
t)node ct
last; * 4Node
* truct S
ijint
intAL3,indn) Creadt
l idvo
NuLj *fivst-
ti ney node Struct
data it
Node ct
Stru
Stdib.n <deluirc #
lude<stdio.h +inc
es thotþrognam krite
a
lmen ute dupli all remov
lut ean innom
recorn0;
t(lst); lw
prid
1); ytdalden(
,
þrintlutllot);
3lut,
); þushl
1)llyt,
; Puah
(Nade)
); Csze NULL; hext lut
melloc *st= deNo
mai)n ind
("\nIn'); Printf
Vord display dslicate ( Strucd node tP)
Shoct node * 4: p- nexl;
wile l4: NULL)
34 (P>dta)= q’ data)
P
4 next;

else
p>next 4>not;
free lql;

int maln (

Creoc LA,5);
printf" linked dist with duli cste:n";
Quplay (tirstl;
Renove Oupli cot Ctirst);
þritt("liaked dist without dpli cet : n";

Slection, Guick gnd Bubble Solt in the follou)


the Inotion,
60, 24
50,150, 625,2o,58,01, 3, ,31,
Inserdion Sort
L. we Sot by corsiden iA the frst elorno as asharted Sub- ls.
-. we bick the next elunen and inset it intD the Correct bosition in he
3. we rebeat &fep2.Until elthe elnent e ingled.

Shtb1: 50, 15, 62, 5, 20, 58, 91, 3, 8,37, b0,24


Jtep: 15 50|, 62, 5 2o, 58,9, 3, 8,37, bo,24

Sep3: 15, 50, 624 5,20, 58, 9l, 3, &, 37, bo, 24
i : 5, 15, 50, &2| 2058, 91, 3, 6, 31, 60, 29
Stek5:
Sep b:
3, |5 20 50 ,62158, 91,3, 8, 3 ,b0,2Y
5, 15, 20, 50581 62 91,3, 8,37, 60,24
Ditp': 5, 15,20, 50, 5%, 62,91|3,8,37,b0, 24
tepB: 3, 5,15,20,50 58 (b2,91|8 ,37, 60,24
Sp : 3, 5 8 ,5,2o ,50,58, 62., 91] 37,60,24
Se lo: 3. S $, 5, 20 31, 50o,58, 62, 91|60, 29
S : 3, 5, 8, 15, 20,31, 50 58,b0,b2, 91|24
Siepl2.: 3, 5, 8, 15, 20,24,37, 50, 58, b0, b2, 91
Selection Sort
elum
2. Repea
Dtep1: 50, 15, 62, 5, 20,58,91, 3, 8,31, 60,4 llintial lut
3, 1562 5 2o,58,91,50, B,31, 6o,24 lswap 50 and 3
Step3: 3, 5, 62,15 120,58 SI, 50, 8,37, 6o,24 |lsuoap 15 and 5
Sr 31S, 8, 15, 20,S6,9150, 6&,31, 60, 24 |l3uwa b2 and b
Jhe: 315, 8, 15,20, 58 ,91,50, 62, 37, bo,24 ||S8 s hedy in tts correct pusition
Seo. 3,5 8, (5, 20,24, 94, 50, 62 37, 60,58 |lswo 240nd 56
Se: 35,5, 15,20,24, 37,50, b2, 91, 60, 5% |lsu 87 nd 31
Steo: 3 S,$, 15,20, 24,37, So,b2, 91, b0,58 lbo alu eady n s Correct position.
Sleb: 315, 8, 15,20,24, 37, 50, 58, 91, b2, 60 ilsuwao 58 nd b2
Sheblo: 3.5,6,1S(20,24,37,So,58 b0, 91, 62 |lsusap 91 ad b2
Sle l1: 3,5,3,15,2o, 24137, 50 58 b0, b2, 91 || the lut now Sorled.

Bubble Sort
1. Stat ot the beginning gthe lat and Conpon adjacend cementa. Swapthamif
anein the torog otden.
-15,50,5, 20, 58, b2,3, 831, b0, 24, 91
end
2. Continue this broces or each pac adjacent elamedh in the list Uil the
veached. A1 this poin,the longytemed wil be at te end te the l t
- 15, 5, 20, 50,58, 3, 8,31, 24,b62,91
-5 1520 50, 58, 3, 8 37,24, 60, 62, 91
- 5 15 203, 8,37,24, 5058, bo, b2, 9)
5,15,3, 8,20,24,37, 50,58 bo, 62, 9)
3 815, 20 24, 37,
5058,bo,62, 91
peat ste 1 and2, but ony tor the fivst n-lelemnts the lest whuen
gth th lat.Jhis enures thed the lag est element sin ts ina
ositi'on.
3,5,8, I5, 20, 24,37, 50,58) 60 ,62, 91

tree Tin preord.


Ppose the fllousing seqoence lat te neda abinoy
rdes
rsecivdy
Pre Orden: G, 8,, A,C, K, F, , 0,E,R, H
In Orden: ,6, K, C, f, A, G, ,E,0,H R

Jhe first no de in the preordes travn sal is the root the tree. whe
na T lndex o tk root rode in the inorden trovenseu. Jhe nodes to
the root hode in the inorden traveusd reo resed theletsbtree, ond
the Yigt te root nade veprësund Hhe rgt Subtree.
Recursiu d y Cons trud the let Subtree usiy the pre orden ond inor
te nodes in the
the lyt Sotree
Recursi vey Constroct the rgt sob tree wsmg te fore ordes and in
truvcuads ttenods inte rght sulbtree.

You might also like