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

singly_linked_list

Pdf

Uploaded by

chhuhhh6
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 views

singly_linked_list

Pdf

Uploaded by

chhuhhh6
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/ 6

/

/Wr
i
teapr
ogr
amtocr
eat
eal
ink
edl
i
standpe
rfor
min s
ert
i
onsandde
let
i
onsofa
llc
ase
s.
/
/Wr
i
tef
unc
ti
onst
oso
rtan
dfi
nal
l
yde
let
etheent
i
reli
staton
ce.

#i
ncl
ude<
std
io.
h>
#i
ncl
ude<
ma l
l
oc.
h>

s
tru
ctnode
{
in
tdata
;
str
uctnode*
nex
t;
}
;

s
tru
ctn
ode*
star
t=NUL L;
s
tru
ctn
ode*
creat
e_l
l(
str
uc tnod e* )
;
s
tru
ctn
ode*
dis
play
(str
uctn od e* );
s
tru
ctn
ode*
ins
ert
_beg(str
uc tn ode* );
s
tru
ctn
ode*
ins
ert
_end(st
ruc tn ode* );
s
tru
ctn
ode*
ins
ert
_before(
s t
ru ctn ode*);
s
tru
ctn
ode*
ins
ert
_aft
e r
(st
ruc tn ode* )
;
s
tru
ctn
ode*
dele
te_beg(s
truc tn ode* )
;
s
tru
ctn
ode*
dele
te_end(s
tructn ode* )
;
s
tru
ctn
ode*
dele
te_node(str
uc tn ode* )
;
s
tru
ctn
ode*
dele
te_af
ter(
struc tn ode*);
s
tru
ctn
ode*
dele
te_l
is
t(st
ructn od e*)
;
s
tru
ctn
ode*
sort
_l
ist
(st
ructn ode* );

i
ntmain(
)
{
i
ntopt
ion;
do
{
pri
nt
f(
"\n\n* *
***MAI NMENU* *
***"
);
pri
nt
f(
"\n1: Createal i
st"
);
pri
nt
f(
"\n2: Dis
playt heli
st"
);
pri
nt
f(
"\n3: Addan odea ttheb e
ginni
ng")
;
pri
nt
f(
"\n4: Addan odea ttheend");
pri
nt
f(
"\n5: Addan odebe foreag i
vennode")
;
pri
nt
f(
"\n6: Addan odea ft
erag i
venn ode"
);
pri
nt
f(
"\n7: Del
e tean odefromt hebe gi
nni
ng"
);
pri
nt
f(
"\n8: Del
e tean odefromt heen d"
);
pri
nt
f(
"\n9: Del
e teagi ve
nn ode")
;
pri
nt
f(
"\n10 :Deletean odea ft
erag i
vennode")
;
pri
nt
f(
"\n11 :Deletethee nti
reli
st
")
;
pri
nt
f(
"\n12 :Sortthelist
")
;
pri
nt
f(
"\n13 :EXIT");
pri
nt
f(
"\n\nEn t
e ryouropt i
on: "
);
scanf
("
%d", &opti
on )
;
swi
tch(
option)
{
case1: st
art=c r
eate_l
l(
star
t)
;
print
f("
\nL I
NKEDL I
STCREATED" )
;
break ;
case2: st
art=di spl
ay(star
t)
;
break ;
case3: st
art=in ser
t_beg(st
art
);
break ;
case4: st
art=in ser
t_end(st
art
);
break ;
c
ase5:
star
t=inser
t_bef
ore(
star
t)
;
break
;
c
ase6:
star
t=inser
t_a
fter
(st
art
);
break
;
c
ase7:
star
t=delet
e_beg(st
art
);
break
;
c
ase8:
star
t=delet
e_end(s
tart
);
break
;
c
ase9:
star
t=delet
e_node(s
tart
);
break
;
c
ase10:
star
t=delet
e_aft
er(
star
t)
;
break
;
c
ase11:
star
t=delet
e_li
st
(st
art
);
pri
nt
f("
\nLINKEDL I
STDEL
ETED"
);
break
;
c
ase12:
star
t=sort
_li
st(
sta
rt)
;
break
;
}
}whi
l
e(opt
i
on!
=13)
;
r
etur
n0;
}

s
truc
tnode*crea
te_l
l(st
ruc tnod e*star
t)
{
s
truc
tnode*new_node ,*ptr;
i
ntnum;
pri
nt
f("
\nEnter-1t oe nd")
;
pri
nt
f("
\nEntertheda ta: "
);
scanf
("
%d",&num) ;
whil
e(n
um!=-1)
{
new_node=( s t
ructn ode*)mal
loc
(si
zeof
(s
truc
tnod
e))
;
new_node- >da ta=num;
i
f(
star
t==NUL L)
{
new_n ode- >n ext=NUL L;
st
a r
t=n ew_ node ;
}
el
se
{
ptr
=start;
wh i
l
e(ptr-
>n ext!
=NUL L)
ptr
=ptr->next;
ptr
->ne x
t=n ew_ node;
new_n ode->n e
x t
=NUL L;
}
pri
nt
f(
"\nEn terthed ata: "
);
scanf
("
%d", &num) ;
}
r
etur
nst
a r
t;
}

st
ruc
tnode*di
splay
(st
ruc
tnode*s
tar
t)
{
st
ruc
tnode*ptr
;
pt
r=sta
rt;
whil
e(pt
r!
=NUL L)
{
pri
nt
f(
"\t%d"
,ptr-
>dat
a);
ptr=pt
r->n
ext
;
}
r
etur
nst
art
;
}

s
truc
tnode*i
nsert
_beg(
str
uctnode*st
art
)
{
s
truc
tnode*new_node;
i
ntnum;
pri
nt
f("
\nEnte
rthedata:")
;
scanf
("
%d",&num);
new_node=(str
uctnode*)mal
l
oc(
size
of(
str
uctn
ode
));
new_node->data=num;
new_node->next=star
t;
sta
rt=new_node;
r
etur
nstar
t;
}

s
truc
tnode* i
nsert_
e nd(st
ructnode*st
art
)
{
s
truc
tnode* pt
r,*new_ node;
i
ntnum;
pri
ntf
("\nEn tertheda ta:"
);
scanf
("%d" ,&nu m);
new_n od e=( st
ructn ode*)mal
l
oc(s
izeof
(s
truc
tnode
));
new_n od e->d ata=n um;
new_n od e->n ext=NUL L;
ptr=sta rt
;
wh i
l
e(ptr- >next!=NUL L)
pt r=pt r->n e
x t
;
ptr-
>n ext=n ew_ node;
r
etur
nstar
t;
}

s
tru
ctnode* i
nsert_befo
re(st
ructnode*st
art
)
{
s
tru
ctnode* n
ew_ n
o de,*
ptr,*
preptr
;
i
ntnum,val
;
pri
ntf
("\nEn tertheda t
a:")
;
scanf
("%d" ,&nu m);
pri
ntf
("\nEn terthev a
luebe f
orewhic
hthed at
ahast
ob ei
nse
rte
d:"
);
scanf
("%d" ,&va l
);
new_n ode=( str
uctnode*)ma l
loc
(si
zeof
(s
truc
tnode
));
new_n ode- >d ata=n um;
ptr=sta r
t;
wh i
l
e(ptr->d a
ta!=val)
{
pr eptr=pt r;
pt r=pt r->next;
}
prept
r- >n ext=n ew_node;
new_n ode- >n ext=ptr;
ret
urns tar
t;
}

s
tru
ctn
ode*
ins
ert
_af
ter
(st
ruc
tnode*
sta
rt)
{
s
truc
tnode* n
e w_ node,*
ptr
,*pr
e pt
r;
i
ntnum,val
;
pri
ntf
("\nEn terthedata:")
;
scanf
("%d" ,&nu m);
pri
ntf
("\nEn terthevalu
ea f
terwhic
hthedatahast
ob ei
nse
rte
d:"
);
scanf
("%d" ,&val);
new_n ode=( st
ructnode*)mal
loc(
si
zeof
(s
truc
tnode)
);
new_n ode- >d ata=num;
ptr=sta r
t;
prept
r=pt r;
wh i
l
e(pr ept
r- >data!
=v al
)
{
pr eptr=pt r;
pt r=pt r->next;
}
prept
r- >n ext=new_node;
new_n ode- >n ext=ptr
;
r
etur
nstar
t;
}

s
truc
tnode*del
ete_
beg(s
truc
tnode*
sta
rt)
{
s
truc
tnode*ptr
;
ptr=sta
rt;
star
t=star
t->next
;
fre
e(pt
r)
;
r
etur
nstar
t;
}

s
truc
tnode* del
et
e _
e n
d(st
ruc
tnode*
sta
rt)
{
s
truc
tnode* pt
r,*
preptr
;
ptr=s t
art
;
wh i
l
e(ptr-
>n ext!
=NUL L
)
{
preptr=ptr;
ptr=ptr->n e
xt;
}
preptr-
>n ext=NUL L
;
fre
e(ptr
);
r
etur
nstar
t;
}

s
tru
ctn ode*delet
e_node(s
truc
tnode*
sta
rt)
{
s
tru
ctn ode*ptr
,*prept
r;
i
ntval
;
prin
tf(
"\nEn t
ertheval
ueoftheno
dewh i
chha
stob
ede
let
ed:
")
;
scan f
("
%d",&val
);
ptr=s ta
rt;
if
(ptr->data==v a
l)
{
sta
rt=de l
ete_
beg(st
art
);
r
e t
urnstar
t;
}
else
{
whil
e(pt
r->dat
a!=va l
)
{
prept
r=ptr;
ptr=ptr-
>ne x
t;
}
preptr-
>next=ptr->nex
t;
fr
ee(ptr
);
ret
urnstar
t;
}
}

s
tru
ctno de* de l
ete_afte
r(st
ruc
tnode*st
art
)
{
s
tru
ctno de* pt r
,*preptr
;
i
ntval
;
pri
n t
f("\nEn terthev al
ueaft
erwhi
chthen
odeha
stode
let
ed:
")
;
sca nf("
%d" ,&val)
;
ptr=s t
art;
prept r=pt r;
wh il
e(pre pt
r- >da t
a!=val
)
{
pre ptr=pt r
;
ptr=pt r->n e
x t
;
}
prept r->n ext=ptr->next
;
free(ptr
);
returns tart
;
}

s
truc
tnode* del
ete_l
i
st(
str
uctnode*s
tar
t)
{
s
truc
tnode* ptr
;
if
(s
tart
!=
NUL L)
{
ptr=st
art;
}
wh i
l
e(pt
r!=NUL L)
{
pri
n t
f(
"\n%distobede l
et
ednext
",
ptr-
>da
ta)
;
start=delet
e_beg(
ptr
);
ptr=s t
art
;
}
r
etur
nstar
t;
}

s
tru
ctnode*sor
t_l
i
s t
(str
uctnode*st
art
)
{
s
tru
ctnode*ptr
1,*ptr
2;
i
ntt
emp;
pt
r1=s t
art
;
whi
le(pt
r1->next!=NULL )
{
ptr
2=ptr1- >next
;
whil
e(
ptr
2! =NUL L)
{
i
f(
ptr1->data>ptr2-
>da t
a)
{
te
mp=ptr1->dat
a;
ptr
1->d
ata=ptr
2->da
ta;
ptr
2->d
ata=temp;
}
pt
r2=pt
r2-
>ne
xt;
}
pt
r1=pt
r1-
>ne
xt;
}
r
etur
nst
art
;
}

You might also like