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

4 Structure

The document discusses structures in C programming. A structure is a user-defined data type that combines logically related data items into a single unit. Structures allow defining new data types with members of different types. Structures can be nested within other structures and pointers to structures are also discussed.

Uploaded by

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

4 Structure

The document discusses structures in C programming. A structure is a user-defined data type that combines logically related data items into a single unit. Structures allow defining new data types with members of different types. Structures can be nested within other structures and pointers to structures are also discussed.

Uploaded by

Parthiban M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

UNI

TIV-STRUCTURES

Str
uctur
e-Nestedstr
uctures-Poi
nterandStructur
es-Arrayof
str
uctur
es-ExampleProgram usi
ngstruct
uresandpointers-Self
ref
erent
ial
str
uctur
es-Dy namicmemor yal
locati
on-Singlyli
nkedli
st
-ty
pedef

St
ruct
ure

Astr
uctureisauserdefi
neddat
atypethatcanbeusedto
combinesameordiffer
entty
pesoflogi
cal
l
yrelat
eddat
aitems
i
ntoasingleuni
t.

• Dat
ait
emsencl
osedwi
thi
nast
ruct
urear
eknownas
members.

•I
tcanbev
iewedasaHet
erogeneoususerdef
ineddat
a
t
ype.

St
ruct
ureDef
ini
ti
on

Key
wor
dst
ructi
susedf
orcr
eat
ingast
ruct
ure.

Sy
ntaxofst
ruct
ure

st
ructst
ruct
ure_
name

dat
a_t
ypemember
1;

dat
a_t
ypemember
2;

...

...

dat
a_t
ypememebern;

}
;
Exampl
e

St
ruct
ureVar
iabl
eDecl
arat
ion

Astr
uctur
evar
iabl
ecaneit
herbedecl
aredwi
thstr
uct
ure
decl
arat
ionorasasepar
atedecl
arat
ionl
i
kebasi
ctypes.

Sy
ntax:

st
ructst
ruct
namev
ar1,
var
2,.
..
.var
n;

Ex:

st
ructst
udents1,
s2;

• st
ruct
uredef
ini
ti
onr
eser
vesspacesf
orst
ruct
urev
ari
abl
es.

Ther
ear
edi
ff
erentway
stodef
inest
ruct
urev
ari
abl
es.

/
/Av
ari
abl
edecl
arat
ionwi
thst
ruct
uredecl
arat
ion.

Sy
ntax1:

st
ructst
ruct
name

{
memberdecl
arat
ions;

}v
ar1,
var
2,.
..v
arn;

Exampl
e:

st
ructst
udent

charname[
20]
;

i
ntr
oll
no;

charbr
anch[
20]
;

}s1,
s2;

/
/Av
ari
abl
edecl
arat
ionl
i
kebasi
cdat
aty
pes

Sy
ntax2:

st
ructst
ruct
name

memberdecl
arat
ions;

}
;

v
oidmai
n()

st
ructst
ruct
namev
ar1,
var
2..
.varn;

Exampl
e:

st
ructst
udent

{
charname[
20]
;

i
ntr
oll
no;

charbr
anch[
20]
;

};

mai
n()

st
ructst
udents1,
s2;

Sy
ntax3:

t
ypedefst
ructst
ruct
name

memberdecl
arat
ions;

}newname;

mai
n()

newnamev
ar1,
var
2..
..v
arn;

Exampl
e:

t
ypedefst
ructst
udent

charname[
20]
;

i
ntr
oll
no;

charbr
anch[
20]
;
}st
ud;

i
ntmai
n()

st
uds1,
s2;

Ot
herway
sofSt
uct
urev
ari
abl
edecl
arat
ion:

1.st
ructst
ruct
name

memberdecl
arat
ions;

t
ypedefst
ructst
ruct
namenewname;

newnamev
ar1,
var
2..
..v
arn;

2.t
ypest
ruct

memberdecl
arat
ions;

}newname;

newnamev
ar1,
var
2,.
..v
arn;

3.st
ruct

{
memberdecl
arat
ions;

}v
ar1,
var
2..
.varn;

Accessi
ngSt
ruct
ureMember
s

Therearet
wot
ypesofoper
ator
susedf
oraccessi
ngmember
sofa
st
ructur
e.

1.
Memberoper
ator
(.
)al
socal
l
eddotoper
ator

2.
Str
uct
urePoi
nterorAr
roworMember
shi
poper
ator
(-
>)

Sy
ntax:
Usi
ngDotoper
ator

St
ruct
ureVar
iabl
e.Member
Name

Exampl
e:

st
udents1,
s2;

s1.
rol
l
no;

s1.
name;

s2.
rol
l
no;

s2.
name;

Sy
ntax:
usi
ngPoi
nteroper
ator

St
ruct
ureVar
iabl
e->Member
Name

Exampl
e:

st
udent*
s1;

s1-
>rol
l
no;

s1-
>name;
St
ruct
ureMemberI
nit
ial
i
zat
ion:

• Str
ucturemember scannotbeini
tial
i
zedal
ongwit
hmember
decl
arati
on.Forexamplethefoll
owingCprogr
am f
ail
sin
compilat
ion.

st
ructst
udent

i
ntr
olno=5;/
l /COMPI
LERERROR:cannoti
nit
ial
i
zemember
s
her
e
charname[
]={
''
Kal
pana","
Kal ;/
am"} /COMPILERERROR:cannot
i
nit
ial
izemembersher
e
}
;

Thereasonforaboveerr
orissi
mple,whenadat
aty
peisdecl
ared,
no
memor yisal
locat
edfori
t.Memoryisall
ocat
edonl
ywhenstr
uctur
e
var
iabl
esarecreated.

• Str
uct
urememberscanbeini
ti
ali
zedusingcur
lybr
aces‘
{}
’.For
exampl
e,f
oll
owi
ngisaval
idini
ti
ali
zat
ion.

1)Usi
ngDot
(.
)oper
ator

v
ar_
name.
memeber
_name=v
alue;

Exampl
e:

st
ructst
udent

i
ntr
oll
no;
charname[
25]
;

charbr
anch[
15]
;

}
;

v
oidmai
n()

st
ructst
udents1

s1.
rol
l
no=123;

s1.
name="
Reema"
;

s1.
branch="
CSE"
;

2)Al
lmember
sval
uesassi
gnedi
nonest
atement

st
ructst
ruct
urenamev
ari
abl
ename=

{
val
uef
ormemeber
1,v
aluef
ormemeber
2…soonf
oral
l
t
hemember
s}

Exampl
e:

st
ructst
udent

i
ntr
oll
no;

charname[
25]
;

charbr
anch[
15]
;

}
;
v
oidmai
n()

st
ructst
udents1={
5,"
Kal
pana"
,"
CSE"
);

3)Desi
gnat
edI
nit
ial
i
zat
ion

Designat
edInit
ial
i
zati
onal
l
owsst
ruct
uremember
stobe
i
nit
iali
zedinanyorder
.

st
ructst
udent

i
ntr
oll
no,
age

charname;

}
;

i
ntmai
n()

//Examplesofi
nit
ial
i
zti
onusi
ngdesi
gnat
ed
i
nit
ial
i
zati
on
str
uctst
udents1={
.r
oll
no=5,
.age=18,
.name
="
Reema"};

st
ructst
udents2={
.r
oll
no=20}
;

pr
int
f("
Rol
l
no=%d,
Name=%s,
Age=%d\
n",
s1.
rol
l
no,

s1.
name,
p1.
age)
;

pr
int
f("
Rol
l
no=%d"
,s2.
rol
l
no)
;
r
etur
n0;

Out
put
:

Rol
l
no=5,
Name=Reema,
Age=18

Rol
l
no=20

Pr
ogr
am t
oReadandDi
spl
aySt
udentI
nfor
mat
ionusi
ngSt
ruct
ure

#include<st dio.h>
structstudent
{
i
ntrollno;
charname[ 20];
i
ntmar ks;
};
voidmai n()
{
structstudents1, s2;
structstudents3={5, "
Reema" ,
490} ;/*s3DirectInit
ial
i
zation*/
clr
scr ()
;
printf(
"***Pr ogram t oReadandDi splayStudentsDetail**
*\n\n")
;
printf(
"Ent erdataf orst udents1\n" )
;
printf(
"Ent ertheRol lno: ");
scanf ("
%d" ,
&s1.roll
no) ;
printf(
"Ent erName: ");
scanf ("
%s" ,&s1.name) ;
printf(
"Ent erTot alMar ks: "
);
scanf ("
%d" ,
&s1.mar ks) ;
printf(
"\n\t\t*******St udentRepor t****
***")
;
printf(
"\n\nMemberv aluesf orstudents1i sassignedusingscanf
funct i
on");
printf(
"\nRol lno=%d" ,s1.rol
lno);
printf(
"\nName=%s" ,
s1. name);
printf(
"\nTot al mar ks=%d" ,s1.
mar ks);
printf(
"\n\nMemberv aluesf orstudents2i sassignedfrom student
s1\ n")
;
s2=s1;/*s1v al
uesar eassignedtos2* /
pri
nt f
("Roll
no=%d\nName=%s\ nTotal
Mar ks=%d",s2.
rol
l
no,s2.name,s2.marks);
pri
nt f
("\
n\nMemberv aluesforstudents3isi
nit
ial
i
zeddi
rect
lyi
n
program" )
;
pri
nt f
("\
nRol l
no=%d",s3.r
oll
no);
pri
nt f
("\
nname=%s" ,
s3.name);
pri
nt f
("\
nTot almarks=%d",s3.
mar ks)
;
getch();
}

Out
put
:

Nest
edSt
ruct
ures
•I
fast
ructurecontai
nsoneormorestr
uctur
esasi
ts
membersitiscal
l
edasanestedstr
uctur
e.

Exampl
e1:

#include<stdi
o.h>
structdate
{
intday;
charmont h[10]
;
intyear;
};
structperson
{
charname[ 25];
structdatedob;//
Nest
ingofst
ructdat
einst
ructper
son
};

v
oidmai n()
{
str
uctpersonp1;
cl
rscr(
);
pri
ntf(
"EnterName: ")
;
scanf(
"%s%d" ,
&p1.name) ;
pri
ntf(
"EnterDateofBi r
th:");
scanf(
"%d%s%d" ,
&p1. dob.day,
&p1.dob.
month,&p1.dob.y
ear);
pri
ntf(
"PersonNamei s:"
);
pri
ntf(
"%s%d",p1.
name) ;
pri
ntf(
"DateofBirt
hi s:")
;
pri
ntf(
"%d%s%d\ n"
,p1.dob.day,p1.
dob.mont
h,p1.dob.y
ear);
getch(
);
}

Out
put
:

Ent
erName:
Reema

Ent
erDat
eofBi
rt
h:15-
August
-1978
Per
sonNamei
s:Reema

Dat
eofBi
rt
his:
15-
August
-1978

Exampl
e2:

str
uctstudent/ /Mainstr
uct
ure
{
charname[ 25];
structaddress/ /Nest
edstr
uct
ure
{
i
ntdoor_no;
charstr
eet[25];
charcit
y[25];
}a;
}s;

v
oidmai n()
{
cl
rscr(
);
pri
ntf(
"EnterName: ");
scanf(
"%s",&s.name) ;
pri
ntf(
"EnterDoor _No: ")
;
scanf(
"%d" ,
&s.a.door_no);
pri
ntf(
"EnterStreetName: "
);
scanf(
"%s",&s.a.
street);
pri
ntf(
"EnterCity:")
;
scanf(
"%s",s.a.
city)
;
pri
ntf(
"\nName: %s",s.name);
pri
ntf("
\nDoor _No: %d",
s.a.
door_no);
pri
ntf(
"\nStreetName: %s",
s.
a.st
reet)
;
pri
ntf(
"\nCity:%s",s.
a.city)
;
getch(
);
}

Out
put
:
Ar
ray
sofSt
ruct
ures

Ifwewanttohandl
emorerecordswit
hinonestr
uctur
e,weneednot
speci
fynumberofstr
uct
urevar
iabl
e.I
nsuchcaseitspossi
blet
o
creat
eanarraywhoseel
ementsareofstr
uct
uretype.

• Agroupofst
ruct
uremaybeor gani
zedi
nanar
ray
r
esult
ingi
nanarrayofst
ruct
ures.

Exampl
e:Pr
ogr
am f
orEmpl
oyeePay
sli
p

#i
nclude<stdi
o.h>
#i
nclude<coni
o.h>
st
ructemp
{
i
ntempno;
charname[
10];
i
ntbpay,
all
ow,ded,
npay;
}e[
10];/
/e[10]i
sArray(
ofst
uct
ure)v
ari
abl
e

voidmai n()
{
i
nti,n;
clr
scr()
;
pri
ntf("
Ent erthenumberofempl oyees:"
);
scanf("
%d" ,
&n);
for(
i=0;
i<n;i++)
{
pri
ntf("
\nEnt ertheEmpl oyeenumber :"
);
scanf("
%d" ,
&e[i
].empno) ;
pri
ntf("
\nEnt ert heName: "
);
scanf("
%s" ,&e[i
].name) ;
pri
ntf("
\nEnt ert heBasi cpay,Al
lowance&Deduct i
on:"
);
scanf("
%d%d%d" ,&e[i
].
bpay ,
&e[i
].
all
ow,&e[i
].
ded)
;
e[i
].
npay =e[i]
.bpay +e[i
].
all
ow-e[i
].
ded;
}
pri
ntf("
\nEmp_ No\ tName\ tBpay\tAll
ow\tDed\tNpay\n\n"
);
for(
i=0;
i<n;i++)
{
pri
ntf("
%d\ t%s\ t
%d\ t
%d\ t
%d\t%d\n",
e[i
].
empno,e[
i]
.name,e[
i]
.bpay
,e[
i]
.
all
ow,e[i
].ded,e[i
].npay);
}
getch()
;
}

Out
put
Poi
nter
sandSt
ruct
ures

Ast
ruct
ure'
smembercanbeaccesssedt
hroughpoi
nteri
ntwoway
s:

1)Ref
erenci
ngpoi
ntert
oanot
heraddr
esst
oaccessmemor
y

2)Usi
ngdy
nami
cmemor
yal
l
ocat
ion

1)Ref
erenci
ngpoi
ntert
oanot
heraddr
esst
oaccesst
hememor
y

Consi
deranexampl
etoaccessst
ruct
ure'
smembert
hroughpoi
nter
.

Exampl
e1:
#i
ncl
ude<st
dio.
h>
st
ructper
son
{
i
ntage;
i
ntwei
ght
;
}
;
i
ntmai
n()
{
st
ructper
son*
per
sonPt
r,per /
son1;/Poi
nterv
ari
abl
edecl
arat
ion
per
sonPt son1;/
r=&per /Ref
erenci
ngpoi
ntert
omemor
yaddr
ess
of
per
son1
cl
rscr
();
pr
int
f("
Ent
erAge:
");
scanf
("%d"
,&(
*per
sonPt
r).
age)
;
pr
int
f("
Ent
erWei
ght
:")
;
scanf
("%d"
,&(
*per
sonPt
r).
wei
ght
);
pr
int
f("
\nDi
spl
ayi
ng.
..
\n"
);
pr
int
f("
Age=%d\
nWei
ght
=%d"
,(
*per
sonPt
r).
age,
(*
per
sonPt
r).
wei
ght
);
get
ch(
);
r
etur
n0;
}

Out
put
Inaboveexample,
thepoint
ervar
iabl
eoft y
pestr
uctpersonis
r
efer
encedtotheaddressofper
son1.Thenonlyt
hestructur
e
memberthroughpoint
ercancanaccessed.

Exampl
e2:

St
ruct
urepoi
ntermembercanal
sobeaccessedusi
ng-
>oper
ator
.

(
*per
sonPt
r).
agei
ssameasper
sonPt
r-
>age

(
*per
sonPt
r).
wei
ghti
ssameasper
sonPt
r-
>wei
ght

#i
ncl
ude<st
dio.
h>
st
ructper
son
{
i
ntage;
f
loatwei
ght
;
}
;
i
ntmai
n()
{
st
ructper
sonper
son1={
25,
47}
;//
Dir
ectI
nit
ial
i
zat
ion
st
ructper
son*
per
sonPt
r;/
/Poi
nterv
ari
abl
edecl
arat
ion
per
sonPt son1;/
r=&per /Ref
erenci
ngpoi
ntert
omemor
yaddr
essof
per
son1
cl
rscr
();
pr
int
f("Age:
%d\
n",
per
sonPt
r-
>age)
;
pr
int
f("Wei
ght
:%d\
n",
per
sonPt
r-
>wei
ght
);
get
ch(
);
r
etur
n0;
}

Out
put

Exampl
e3:

v
oidmai n()
{
str
uctst udent//Str
uctur
ecanalsobedecl
aredwi
thi
nmai
n
{
introllno;
charname[ 30]
;
charbr anch[5]
;
i
ntmar ks;
}*
s;//Poi nt
ervari
abledecl
arat
ion

cl
rscr
();
pri
ntf
("Ent
erRoll
no:")
;
scanf
("%d",
&s-
>roll
no);
pri
ntf
("Ent
erName: ")
;
scanf
("%s",
&s-
>name) ;
pri
ntf
("Ent
erBranch:
");
scanf
("%s",
&s-
>branch);
pri
ntf
("Ent
ermarks:")
;
scanf(
"%d",&s->marks) ;
pri
ntf(
"\nRoll
no: %d",s->rol
lno);
pri
ntf(
"\nName: %s",s->name) ;
pri
ntf(
"\nBranch:%s" ,s->br
anch);
pri
ntf(
"\nMarks: %d",s->mar ks)
;
getch(
);
}

Out
put

2.Accessi
ngstruct
uremembert
hroughpoi
nterusi
ngdy
nami
c
memor yal
locat
ion

Toaccessstr
uct
urememberusi
ngpoint
ers,memorycanbe
al
locat
eddynami
call
yusi
ngmall
oc(
)functi
ondefi
nedunder"
stdl
i
b.h"
headerfi
l
e.

Sy
ntaxt
ousemal
l
oc(
)

pt
r=(
cast
-t
ype*
)mal
l
oc(
byt
e-si
ze)

Exampletousest
ruct
ure'
smembert
hroughpoi
nterusi
ngmal
l
oc(
)
funct
ion.

#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
st
ructper
son
{
i
ntage;
i
ntwei
ght
;
charname[
30]
;
}
;

i
ntmai
n()
{
st
ructper
son*
ptr
;
i
nti
,num;
cl
rscr
();
pr
int
f("
Ent
ernumberofper
sons:
");
scanf
("%d"
,&num)
;
pt
r=(
str
uctper
son*
)mal
l
oc(
num *si
zeof
(st
ructper
son)
);
/
*Abov
est
atemental
l
ocat
est
hememor
yfornst
ruct
ureswi
th
poi
nterper
sonPt
rpoi
nti
ngt
obaseaddr
ess*
/

f
or(
i=0;
i<num;
++i
)
{
pr
int
f("
Ent
ername,
ageandwei
ghtoft
heper
sonr
espect
ivel
y:
\n"
);
scanf
("%s%d%d"
,&(
ptr
+i)
->name,
&(pt
r+i
)-
>age,
&(pt
r+i
)-
>wei
ght
);
}
pr
int
f("
Displ
ayi
ngI
nfr
omat
ion:
\n"
);
f
or(
i=0;
i<num;
++i
)
pr
int
f("
%s\
t%d\
t%d\
n",
(pt
r+i
)-
>name,
(pt
r+i
)-
>age,
(pt
r+i
)-
>wei
ght
);
get
ch(
);
r
etur
n0;
}
Out
put

t
ypedefi
nSt
ruct
ure

•t
ypedefmakest
hecodeshor
tandi
mpr
ovesr
eadabi
l
ity
.Int
he
abov
edi
scussi
onwehav
eseent
hatwhi
l
eusi
ngst
ruct
sev
ery
t
imewehav
etouset
hel
engt
hysy
ntax,
whi
chmakest
hecode
conf
usi
ng,
lengt
hy,
compl
exandl
essr
eadabl
e.Thesi
mpl
e
sol
uti
ont
othi
sissuei
suseoft
ypedef
.Iti
sli
keanal
i
asof
st
ruct
.

TheCprogr
amminglanguageprovi
desakeywor
dcall
edt
ypedef
,
whichy
oucanusetogiveatypeanewname.Foll
owingi
san
exampl
etodef
ineaterm BYTEforone-
byt
enumbers:

typedefunsi /
gnedcharBYTE;/i
dent
if
ierBYTEcanal
sobel
ower
case
Aft
erthistypedefi
nit
ion,
thei
denti
fi
erBYTEcanbeusedasan
abbrev
iati
onforthetypeunsi
gnedchar.

Youcanuset ypedeftogi
veanametoyouruserdefi
neddatatypes
aswell
.Forexampl e,y
oucanusety
pedefwit
hstruct
uretodefi
nea
newdatatypeandt henuset
hatdat
atypetodefi
nestruct
ure
var
iabl
esdirectl
y.

Codewi
thoutt
ypedef

st
ructhome_
addr
ess

i
ntl
ocal
_st
reet
;

char*
town;

char*
my_
cit
y;

char*
my_
count
ry;

}
;

.
..

st
ructhome_
addr
essv
ar;

v
ar.
town="
Agr
a";

Codeusi
ngt
yepdef

t
ypedefst
ructhome_
addr
ess

i
ntl
ocal
_st
reet
;

char*
town;

char*
my_
cit
y;
char*
my_
count
ry;

}
addr
;

.
.

.
.

addrv
ar;

v
ar.
town="
Agr
a";

I
nsteadofusi
ngthestructhome_
addressev
eryti
meyouneedto
decl
arestr
uctvar
iabl
e,youcansi
mplyuseaddr,
thet
ypedeft
hatwe
havedefi
ned.

Exampl
e

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<st
ri
ng.
h>

t
ypedefst
ructBookDet
ail
s

chart
it
le[
50]
;

charaut
hor
[50]
;

charsubj
ect
[100]
;

i
ntbook_
id;

}Book;

i
ntmai
n()

{
/
Bookcse;/t
ypedef
st
rcpy
(cse.
ti
tl
e,"
CPr
ogr
ammi
ng"
);

st
rcpy
(cse.
aut
hor
,"ReemaThar
eja"
);

st
rcpy
(cse.
subj
ect
,"Pr
ogr
ammi
ngi
nC"
);

cse.
book_
id=6495407;

pr
int
f("
Bookt
it
le:
%s\
n",
cse.
ti
tl
e);

pr
int
f("
Bookaut
hor:
%s\
n",
cse.
aut
hor
);

pr
int
f("
Booksubj
ect:
%s\
n",
cse.
subj
ect
);

pr
int
f("
Bookbook_
id:
%d\
n",
cse.
book_
id)
;

r
etur
n0;

Out
put

Bookt
it
le:
CPr
ogr
ammi
ng

Bookaut
hor:
ReemaThar
eja

Booksubj
ect:
Progr
ammi
ngi
nC

Bookbook_
id:
6495407

UNI
ON

Auni
oni
sadat
aty
pesi
nCwhi
chal
l
owst
heov
erl
ayofmor
e

t
hanonev
ari
abl
eint
hesamememor
yar
ea.

• Everyvar
iabl
eisstar
tedinasepar
atelocati
onanda
resul
teachofthesevari
abl
eshavetheirownaddr
esses.

• Butuni
onpr
ovi
desameansbywhi
cht
hememor
yspace
canbeshar
ed.

Compil
erwi
ll
all
ocatesuffi
cientst
oragefort
heuni
onv
ari
abl
est
o
accommodat
ethelargestel
ementi ntheuni
on.

Ot
herel
ement
soft
heuni
onuset
hesamespace.

Compar
isonBet
weenst
ruct
uresanduni
ons

St
ruct
ure Uni
on

1.Ev
erymemberhasi
tsownmemor
y All
themember s
space usethesame
memor yspace

2.canhandl
eal
lthemember
soraf
ewas Canhandl
eonl
yone
requi
red memberatati
me

3.
Key
wor
d“st
ruct
”isused Keywor
d“Uni
on”i
s
used

4.
May
beI
nit
ial
i
zedwi
thal
li
tsmember
s Onl
yonemember
maybeIni
ti
ali
zed

5.
Anymembercanbeaccessedatany Onlyonemember
ti
mewit
houtl
ossofdat
a canbeaccessedat
ati
me

6.
Mor
est
oragespacei
srequi
red Conser
vat
ionof
memoryispossi
ble
Uni
onExampl
es:
Exampl
e1:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
ri
ng.
h>
uni
onDat
a
{
i
nti
;
f
loatf
;
charst
r[
20]
;
}
;
i
ntmai
n()
{
uni
onDat
adat
a;
dat
a.i
=10;
dat
a.f=220.
5;
st
rcpy
(dat
a.st
r,"
CPr
ogr
ammi
ng"
);
pr
int
f("
dat
a.i
:%d\
n",
dat
a.i
);
pr
int
f("
dat
a.f:
%f\
n",
dat
a.f
);
pr
int
f("
dat
a.st
r:%s\
n",
dat
a.st
r);
r
etur
n0;
}
Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
he
f
oll
owi
ngr
esul
t−
Out
put
dat
a.i
:1917853763
dat
a.f:
4122360580327794860452759994368.
000000
dat
a.st
r:CPr
ogr
ammi
ng
Here,wecanseethatthevaluesofi andfmember sofunion
gotcorr
uptedbecausethefi
nal val
ueassignedtothevar
iable
hasoccupi
edthememor yl
ocationandt hi
sisther
easont hat
theval
ueofstrmemberi sgetti
ngprintedverywel
l
.
Nowlet'
slooki
ntot
hesameexampl
eonceagai
nwherewewill
useonevari
abl
eatati
mewhichi
sthemai
npurposeofhav
ing
uni
ons−
Exampl
e2:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
ri
ng.
h>
uni
onDat
a
{
i
nti
;
f
loatf
;
charst
r[
20]
;
}
;
i
ntmai
n()
{
uni
onDat
adat
a;
dat
a.i
=10;
pr
int
f("
dat
a.i
:%d\
n",
dat
a.i
);
dat
a.f=220.
5;
pr
int
f("
dat
a.f:
%f\
n",
dat
a.f
);
st
rcpy
(dat
a.st
r,"
CPr
ogr
ammi
ng"
);
pr
int
f("
dat
a.st
r:%s\
n",
dat
a.st
r);
r
etur
n0;
}
Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
he
f
oll
owi
ngr
esul
t−
Out
put
dat
a.i
:10
dat
a.f:
220.
500000
dat
a.st
r:CPr
ogr
ammi
ng
Her
e,al
lthemembersar
egett
ingpr
int
edv
erywel
lbecauseone
memberisbei
ngusedatat
ime.

Dy
nami
cmemor
yal
l
ocat
ion
Csuppor
tst
hreeki
ndofmemor
yal
l
ocat
ion:
1.
Aut
omat
ical
l
ocat
ion
2.
Stat
ical
l
ocat
ion
3.
Dynami
cal
l
ocat
ion
Di
ff
erencebet
weenSt
ati
candDy
nami
cmemor
yal
l
ocat
ion

Dy
nami
cmemor
yal
l
ocat
ion
Thepr
ocessofal
l
ocat
ingmemor
ydur
ingpr
ogr
am execut
ionoratr
un
t
imeiscal
leddy
nami
cmemor
yal
l
ocat
ion.
Dynami
cmemor
y
management
r
efer
stomanual
memor
ymanagement
.Thi
sal
l
owsy
out
oobt
ain
more
memor
ywhenr
equi
redandr
eleasei
twhennotnecessar
y.
Dy
nami
cmemor
yal
l
ocat
ionf
unct
ions:
Funct
ionUseofFunct
ion
mall
oc(
)Al
l
ocat
esr
equest
edsi
zeofby
tesandr
etur
nsapoi
nterf
ir
st
by
te
ofal
l
ocat
edspace
cal
loc(
) Al
l
ocatesspaceforanar
rayel
ement
s,i
nit
ial
i
zest
ozer
o
andthen r et
urnsapoint
ertomemory
f
ree(
) deal
l
ocat
ethepr
evi
ousl
yal
l
ocat
edspace
r
eal
l
oc(
)Changet
hesi
zeofpr
evi
ousl
yal
l
ocat
edspace

Di
ff
erencebet
weenmal
l
oc()andcal
l
oc():
Real
l
oc(
)
real
l
oc()funct
ionmodi
fi
esorreall
ocat
esthememor
ysi
zeal
l
ocat
ed
bymalloc()andcal
l
oc()funct
ionstonewsi
ze.
i
e.toincr
easeordecr
easet
heal
l
otedspaceatal
aterst
agei
na
pr
ogram.
Ifenoughspacedoesn’texi
sti
nmemor yofcurr
entblocktoextend,
newbl ocki
sall
ocat
edf orthef
ull
sizeofreall
ocati
on,thencopiesthe
exist
ingdat
atonewbl ockandthenfreestheoldblock.Fi
rstby
te
addressofnewlyal
lot
tedspaceisassignedtothepointer

Sy
ntax:
poi
nter
_var
iabl
e=r
eal
l
oc(
poi
nter
_var
iabl
e,
new_
size)
;
Ex:
r
eal
l
oc(
ptr
,100*
sizeof
(char
));

f
ree(
):
f
ree()functi
onfreesorrel
easesthememoryal
l
ocatedby
mall
oc(),call
oc(),r
eal
loc()f
uncti
onsandr
etur
nsthememor yt
o
t
hesy st
em. I
tisgoodprati
cetorel
easet
hememoryjustbef
orethe
t
erminati
onofpr ogr
am.

Sy
ntax:
f
ree(
poi
nter
_var
iabl
e);
Ex:
f
ree(
ptr
);
Exampl
eusi
ngmal
l
oc(),
real
l
oc()andf
ree()f
unct
ions
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
ri
ng.h>
#i
ncl
ude<st
dli
b.h>
i
ntmai n()
{
char*mem_
all
ocat
ion;

/
*memor yisal
locateddy namically*/
mem_ all
ocati
on=mal loc(20*si zeof(char));
i
f(mem_ al
l
ocation==NULL)
{
printf(
"Coul
dn'tablet oall
ocater equestedmemor y
\n")
;
}
else
{
str
cpy (
mem_ allocat
ion,"Fi
rstYearCSE" );
}
print
f("Dynamicallyal
locatedmemor ycontent:"\
"%s\n"
,mem_
all
ocat
ion)
;

/*memor yisreall
ocated*/
mem_ all
ocat i
on=real
loc(mem_ al
locati
on,100*
sizeof
(char)
);
i
f(mem_ al
locati
on==NULL)
{
pri
ntf(
"Couldn'
tablet oal
locaterequestedmemor y\n"
);
}
el
se
{
str
cpy(mem_ al
locati
on,"
spacei sextendedupto"\"100
charact
ers");
}
pri
ntf(
"Resizedmemor y:%s\n",mem_ all
ocati
on);

/
*memor yreleased*
/
free(
mem_ all
ocati
on)
;
getch()
;
retur
n0;
}

Out
put
Exampl
eusi
ngcal
l
oc()f
unct
ion

#incl ude<st dio.h>


#incl ude<mal loc.h>
voidmai n()
{
char* cp;
i
nti ;
clr
scr (
);
cp=( char* )call
oc( 5,1);
printf("Thest arti
ngaddr essofmemor y=%u",
cp)
;
printf("\nEnt erast ringhav i
ng5char acter
\n"
);
for(i=1;i<=5; i
++)
{
*cp++=get char (
);
printf("\nchar ater%di s%c\ n",
i
,*(
cp-1))
;
printf("I
ti sst oredi naddr ess%u",(
cp-1));
}
get ch();
}

Out
put
Sel
fRef
erent
ial
Str
uct
ure

Astruct
uret
hatcont
ainspoi
nterstoast
ruct
ureofi
tsownt
ypei
s
knownasself
-r
efer
enti
alst
ructur
e.

Asel
f-
ref
erent
ialstruct
urei
soneofthedatastr
uctur
eswhichr
efert
o
t
hepoint
er(points)toanot
herst
ruct
ureofthesametype.

Thesetypesofdat
ast
ruct
uresarehelpf
uli
nimplement
ingdat
a
st
ructur
eslikel
i
nkedl
i
sts,
stack,
queuesandtr
ees.

I
nstr
uct
ures,member scanbeofanydatatype.Ifweincl
udea
memberi
nt hestr
uctur
e,whichi
sapointert
ot heparentst
ruct
ure
t
ype,
suchast r
uctur
eiscall
edsel
f-
ref
erenti
alstr
ucture.

Sampl
e:

st
ructt
ag
{

member1;

member2;

…………

…………

st
ructt
ag*
name;

}
;

Thestructur
eoftypetagcont
ainsamember
,‘
name’whichisa
point
ertostruct
ureofty
petag.Thust
agisasel
fref
erent
ial
str
ucture.

Sy
ntax

st
ructst
ruct
ure_
name

dat
aty
pev
alue;

st
uctst
ruct
ure_
name*poi
nter
_name;

}
;

Exampl
e

st
ructnode

i
ntdat
a;

st
ructnode*
next
;

}
;
I
nt heaboveexample,
thenodei
saself-
ref
erent
ial
str
uct
ure–
becausethe*nexti
softhety
pestr
uctnode.

Si
ngl
yLi
nkedLi
st

Likearr
ays,Li
nkedListisali
neardatastr
ucture.Unl
i
kear
ray
s,l
inked
l
istelementsarenotstoredatcont
iguouslocati
on;t
heel
ement
sar e
l
inkedusingpoint
ers.

NODE

WhyLi
nkedLi
st?

Arr
ayscanbeusedtostor
eli
neardat
aofsi
mil
art
ypes,
butar
ray
s
havef
oll
owingl
imi
tati
ons.

1)Thesi
zeofthearr
aysi
sfixed:Sowemustknowt heupperl
i
miton
thenumberofel
ementsi
nadv ance.Also,
gener
all
y,t
heall
ocat
ed
memoryisequalt
otheupperli
mitirr
especti
veoft
heusage.
2)Inser
ti
nganewel ementinanarr
ayofelementsi
sexpensiv
e,
becauseroom hastobecreat
edforthenewelementsandtocreat
e
room exi
sti
ngelementshavetoshi
ft
ed.

Forexampl
e,i
nasy
stem i
fwemai
ntai
nasor
tedl
i
stofI
Dsi
nanar
ray
i
d[]
.

i
d[]=[
1000,
1010,
1050,
2000,
2040]
.

Andi
fwewantt
oinser
tanewI D1005,t
hentomaintai
nthesor
ted
or
der
,wehav
etomov eal
ltheel
ementsaft
er1000(excl
udi
ng1000).

3)Deletioni
salsoexpensi
vewi
thar
ray
sunt i
lunl
esssomespeci
al
techniquesar
eused.Forexampl
e,t
odelet
e1010i ni
d[]
,ev
ery
thi
ng
after1010hastobemov ed.

Li
nkedLi
stAdv
ant
agesov
erar
ray
s:

1)Dy
nami
csi
ze

2)Easeofi
nser
ti
on/
del
eti
on

Dr
awbacks:

1)Random accessisnotallowed.Wehav et
oaccessel
ements
sequent
ial
lystart
ingfrom t
hefir
stnode.Sowecannotdobi
nary
searchwit
hlinkedli
sts.

2)Extr
amemor
yspacef
orapoi
nteri
srequi
redwi
theachel
ementof
thel
ist
.

Li
nkedLi
stRepr
esent
ati
oni
nC

Ali
nkedl
istisrepr
esent
edbyapoi nt
ert
othef
irstnodeofthel
inked
l
ist
.Thefi
rstnodeiscal
ledhead.I
fthel
i
nkedl
istisempty,
thenvalue
ofheadi
sNULL.

Eachnodei
nal
i
stconsi
stsofatl
eastt
wopar
ts:

1)dat
a

2)poi
ntert
othenextnode
I
nC,wecanrepresentanodeusi
ngstr
uct
ures.Bel
owi
sanexampl
e
ofal
i
nkedl
istnodewi t
hanint
egerdat
a.

st
ructnode

i
ntdat
a;

st
ructnode*
next
;

}
;

Ty
pesofLi
nkedLi
st
Si
mpl
eLi
nkedLi
st−I
tem Nav
igat
ioni
sfor
war
donl
y.
Doubl
yLi
nkedLi
st−I
temscanbenav
igat
edf
orwar
dandbackwar
d
way.
Cir
cularLi
nkedList−Lasti
tem cont
ainsli
nkofthefi
rstel
ementas
nextandandfir
stelementhasli
nktolastelementasprev
.

Basi
cOper
ati
ons

i
nser
ti
on
Del
eti
on
Sear
ch
Tr
aver
se
Di
spl
ay

Si
ngl
yLi
nkedLi
stOper
ati
ons

Twoi
mpor
tantpoi
ntst
oremember
:

1.
headpoi
ntst
othef
ir
stnodeoft
hel
i
nkedl
i
st

2.
nextpoi
nterofl
astnodeisNULL,soi
fnextofcur
rentnodei
sNULL,
wehavereachedendofli
nkedl
ist
.

I
nalloft
heexampl
es,wewil
lassumethatt
heli
nkedl
i
sthast
hree
nodes1--
->2-
--
>3wi
thnodestr
uctur
easbelow:

st
ructnode

i
ntdat
a;

st
ructnode*
next
;

}
;

Cr
eat
enode
ty
pedefst
ructLi
nkedLi
st*node;//
Def
inenodeaspoi
nterofdat
aty
pe
str
uct Li
nkedList

nodecr
eat
eNode(
)

nodet
emp;
//decl
areanode

t
emp=(
node)
mal
l
oc(
sizeof
(st
ructLi
nkedLi
st)
);/
/al
l
ocat
ememor
y
usi
ng
mal
l
oc(
)
t
emp- /
>next=NULL;/makenextpoi
ntt
oNULL
r
etur
nt /
emp;/r
etur
nthenewnode
}

Addnodet
otheLi
nkedl
i
st

nodeaddNode(
nodehead,
intv
alue)

nodet
emp,
p;/
/decl
aret
wonodest
empandp
t
emp=creat
eNode(
);/
/cr
eat
eNodewi
llr
etur
nanewnodewi
th
dat
a=val
ueandnextpoi
nti
ngt
oNULL.
t
emp-
>dat
a=v
al /
ue;/addel
ement
'sv
aluet
odat
apar
tofnode
i
f(
head==NULL)

emp; /
head=t /whenl
i
nkedl
i
sti
sempt
y
}

el
se
{

/
p=head;/assi
gnheadt
op
whi
l
e(p-
>next!
=NULL)

p=p->next
;//
trav
erset
hel
i
stunt
ilpi
sthel
astnode.
Thel
ast
nodeal
wayspoint
stoNULL.
}

p-
>next=t /
emp;/Poi
ntt
hepr
evi
ousl
astnodet
othenewnode
cr
eat
ed.
}

r
etur
nhead;

Tr
aver
seal
i
nkedl
i
st

Di
spl
ayi
ngthecont
entsofali
nkedli
sti
sverysi
mpl
e.Wekeep
movi
ngthetempnodetothenextoneanddi
spl
ayi
tscont
ent
s.

WhentempisNULL,weknowthatwehav
ereachedt
heendofl
i
nked
l
istsowegetoutoft
hewhil
eloop.

st
ructnode*
temp=head;

pr
int
f("
\n\
nLi
stel
ement
sar
e-\
n")
;

whi
l
e(t
emp!
=NULL)

pr
int
f("
%d-
--
>",
temp-
>dat
a);
t
emp=t
emp-
>next
;

Addel
ement
stol
i
nkedl
i
st

Youcanaddel
ement
stoei
therbegi
nni
ng,
middl
eorendofl
i
nkedl
i
st.

Addt
obegi
nni
ng
1.
All
ocat
ememor
yfornewnode

2.
Stor
edat
a

3.
Changenextofnewnodet
opoi
ntt
ohead

4.
Changeheadt
opoi
ntt
orecent
lycr
eat
ednode

st
ructnode*
newNode;

newNode=mal
l
oc(
sizeof
(st
ructnode)
);

newNode-
>dat
a=4;

newNode-
>next=head;

head=newNode;

Addt
oend
1.
All
ocat
ememor
yfornewnode

2.
Stor
edat
a

3.
Trav
erset
olastnode

4.
Changenextofl
astnodet
orecent
lycr
eat
ednode
st
ructnode*
newNode;

newNode=mal
l
oc(
sizeof
(st
ructnode)
);

newNode-
>dat
a=4;

newNode-
>next=NULL;

st
ructnode*
temp=head;

whi
l
e(t
emp-
>next!
=NULL)

t
emp=t
emp-
>next
;

t
emp-
>nex
t=newNode;

Addt
omi
ddl
e
1.
All
ocat
ememor
yandst
oredat
afornewnode

2.
Trav
erset
onodej
ustbef
oret
her
equi
redposi
ti
onofnewnode

3.
Changenextpoi
nter
stoi
ncl
udenewnodei
nbet
ween

st
ructnode*
newNode;

newNode=mal
l
oc(
sizeof
(st
ructnode)
);

newNode-
>dat
a=4;

st
ructnode*
temp=head;

f
or(
inti
=2;
i<posi
ti
on;
i++)

{
i
f(
temp-
>next!
=NULL)

t
emp=t
emp-
>next
;

newNode-
>next=t
emp-
>next
;

t
emp-
>nex
t=newNode;

Del
eteanodef
rom al
i
nkedl
i
st

Youcandel
eteei
therf
rom begi
nni
ng,
endorf
rom apar
ti
cul
ar
posi
ti
on.

Del
etef
rom begi
nni
ng
1.
Poi
ntheadt
othesecondnode

head=head-
>next
;

2.
Del
etef
rom end

3.
Trav
erset
osecondl
astel
ement

4.
Changei
tsnextpoi
ntert
onul
l

st
ructnode*t
emp=head;

whi
l
e(t
emp-
>next
->next
!=NULL)

t
emp=t
emp-
>next
;

}
t
emp-
>nex
t=NULL;

Del
etef
rom mi
ddl
e
1.
Trav
erset
oel
ementbef
oret
heel
ementt
obedel
eted

2.
Changenextpoi
nter
stoexcl
udet
henodef
rom t
hechai
n

f
or(
inti
=2;
i<posi
ti
on;
i++)

i
f(
temp-
>next
!=NULL)

t
emp=t
emp-
>next
;

t
emp-
>nex
t=t
emp-
>next
->next
;

I
NSERTI
ON

St
ep1:
St
ep2:

St
ep3:

DELETI
ON

St
ep1:
St
ep2:

St
ep3:

St
ep4:

Pr
ogr
am f
orSi
ngl
yLi
nkedLi
st
#i
ncl
ude<stdi
o.h>
#i
ncl
ude<stdli
b.h>
#i
ncl
ude<conio.
h>
#i
ncl
ude<process.
h>
str
uctnode
{
i
ntdata;
str
uctnode*next
;
}*
start
=NULL,*
q,*t
;

v
oidmai n(
)
{
i
ntch;
v
oidinsert_
beg();
v
oidinsert_
end();
i
nti
nsert_pos();
v
oiddispl
ay ()
;
v
oiddelete_beg()
;
v
oiddelete_end()
;
i
ntdelet
e_pos();

while(
1)
{
pri
ntf
("\n\n-
---Singl
yLinkedList
(SLL)Menu--
--
");
pri
ntf
("\n1.I
nser t
\n2.
Display\
n3.Del
ete\
n4.
Exi
t\n\n"
);
pri
ntf
("Entery ourchoi
ce(1-4)
:"
);
scanf(
"%d" ,
&ch) ;

switch(ch)
{
case1:
print
f("
\n--
--I
nsertMenu- --
-")
;
print
f("
\n1.I
nsertatbeginning\n2.
Inser
tatend\
n3.
Inser
tat
specifi
edposi t
ion\n4.Exi
t"
);
print
f("
\n\nEnteryourchoice(1-4)
:"
);
scanf("
%d" ,
&ch);

switch(ch)
{
case1: inser
t_beg(
);
break;
case2: inser
t_end(
);
break;
case3: i
nsert
_pos();
break;
case4: exi
t(
0);
default
:pri
ntf
("WrongChoi
ce!
!"
);
}
break;

case2:di
spl
ay(
);
br
eak;

case3: pri
ntf("
\n-
---Delet
eMenu- --
-"
);
pri
ntf(
"\n1.Del
et ef
rom beginni
ng\n2.
Delet
efr
om
end\
n3.Delet
ef r
om speci fi
edposit
ion\n4.
Exit
");
pri
ntf(
"\n\nEnteryourchoi
ce(1-4)
:"
);
scanf(
"%d" ,
&ch);

switch(ch)
{
case1: delete_beg();
break;
case2: delete_end();
break;
case3: delete_pos();
break;
case4: exit(
0);
default:pr
intf(
"Wr ongChoice!
!")
;
}
break;
case4: exit(0)
;
default:pri
ntf("
Wr ongChoice!!
")
;
}
}

v
oi di
nsert_beg()
{
i
ntnum;
t=(
str
uctnode* )mal
loc(
sizeof
(st
ructnode)
);
pri
ntf
("Enterdat
a:"
);
scanf
("%d"
,&num)
;
t-
>dat
a=num;

i
f(
start==NULL) //
Ifl
i
sti
sempt
y
{
t-
>next=NULL;
star
t=t;
}
el
se
{
t-
>next=st
art
;
star
t=t;
}
}

v
oi dinsert_end()
{
i
ntnum;
t=(str
uctnode* )mal
loc(
sizeof
(st
ructnode)
);
pr i
ntf
("Enterdat
a:"
);
scanf (
"%d" ,
&num);
t->data=num;
t->next=NULL;

i
f(
star t
==NULL) / /Ifl
i
sti
sempt
y
{
start=t;
}
el
se
{
q=start;
while(q->next
!=NULL)
q=q->next ;
q->next=t;
}
}

i
ntinser
t_pos()
{
i
ntpos,i
,num;
i
f(
st art
==NULL)
{
print
f("
Listi
sempt
y!!
")
;
retur
n0;
}

t=(str
uctnode* )mall
oc(sizeof
(st
ructnode)
);
pr i
ntf
("Enterdata:
");
scanf("%d" ,
&num);
pr i
ntf
("Enterposit
iontoinsert
:"
);
scanf("%d" ,
&pos);
t->data=num;

q=start;
for
(i
=1; i
<pos-1;
i++)
{
i
f(q->next==NULL)
{
print
f("
Therearel
essel
ement
s!!
")
;
retur
n0;
}

q=q-
>next
;
}

t->next
=q->next
;
q->next=t
;
return0;
}

v
oi ddispl
ay()
{
i
f(st
art==NULL)
{
pri
ntf(
"Li
sti
sempty!!
")
;
}
else
{
q=start
;
pri
ntf(
"Thel
inkedl
i
stis:
\n"
);
whil
e(q!=NULL)
{
pri
ntf(
"%d->"
,q-
>dat
a);
q=q->next
;
}
}
}

v
oi ddel ete_beg()
{
i
f(st
ar t==NULL)
{
printf("
Thel i
sti
sempty!
!"
);
}
else
{
q=st art
;
star t
=start-
>next
;
printf("
Deletedel
ementi
s%d",
q->dat
a);
fr
ee( q);
}
}

v
oi ddelete_end()
{
i
f(st
art==NULL)
{
pri
ntf("Thelisti
sempt y
!!
")
;
}
else
{
q=start;
while(q->next-
>next
!=NULL)
q=q->next ;

t=q->next;
q->next=NULL;
printf
("Del
etedel
ementi
s%d"
,t
->dat
a);
free(t
);
}
}

i
ntdelet
e_pos(
)
{
i
ntpos,
i;

i
f(
st art
==NULL)
{
print
f("
Listi
sempt
y!!
")
;
retur
n0;
}

pri
ntf
("Ent
erposi
ti
ont
odel
ete:
")
;
scanf
("%d",
&pos)
;

q=start;
for
(i
=1; i
<pos-1;
i++)
{
i
f(q->next==NULL)
{
print
f("
Therearel
essel
ement
s!!
")
;
retur
n0;
}
q=q->next;
}

t=q->next;
q->next=t-
>next
;
printf
("Del
etedel
ementi
s%d"
,t
->dat
a);
free(t
);

r
etur
n0;
}

Out
put

—-Singl
yLi
nkedLi
st(
SLL)Menu—-
1.
Insert
2.
Di spl
ay
3.
Del et
e
4.
Exi t
Enteryourchoice(1-
4):
1—-Inser
tMenu—-
1.
Insertatbeginning
2.
Insertatend
3.
Insertatspecifi
edposit
ion
4.
Exi t
Enteryourchoice(1-
4):
1
Enterdata:
51

—-Si nglyLinkedList(
SLL)Menu—-
1.
Inser t
2.
Di splay
3.
Del ete
4.
Exi t
Entery ourchoice(1-
4):
1
1.
Inser tatbeginning
2.
Inser tatend
3.
Inser tatspecifi
edposit
ion
4.
Exi t
Entery ourchoice(1-
4):
2
Enterdat a:
53

—-Si nglyLinkedList(
SLL)Menu—-
1.
Inser t
2.
Di splay
3.
Del ete
4.
Exi t
Entery ourchoice(1-
4):
1
1.
Inser tatbeginning
2.
Inser tatend
3.
Inser tatspecifi
edpositi
on
4.
Exi t
Entery ourchoice(1-
4):
3
Enterdat a:
52
Enterposi t
iontoinsert:
2

—-SinglyLi
nkedLi
st(
SLL)Menu—-
1.
Inser
t
2.
Displ
ay
3.
Delet
e
4.
Exit
Ent
eryourchoi
ce(
1-4)
:2
51-
>52->53

—-Si nglyLinkedList(
SLL)Menu—-
1.
Inser t
2.
Di splay
3.
Del ete
4.
Exi t
Entery ourchoice(
1-4):3
1.
Del etefrom beginning
2.
Del etefrom end
3.
Del etefrom specifi
edposi
ti
on
4.
Exi t
Entery ourchoice(
1-4):3
Enterposi t
iontodelete:2

—-Si nglyLi
nkedLi
st(SLL)Menu—-
1.
Inser t
2.
Di splay
3.
Del ete
4.
Exi t
Entery ourchoi
ce(
1-4):
2
51->53
ht
tp:
//bt
echsmar
tcl
ass.
com/
DS/
U1_
T10.
html

You might also like