You are on page 1of 93

For More Notes and questions log on to www.technicalsymposium.

com
INTRODUCTION TO C
C has emerged as the most widely used programming language for software
development. Its features allow the development of well-structured programs. Most
computers directly support its data types and control structures, resulting in the
construction of efficient programs. It is independent of any particular machine
architecture or operating system, which makes it easy to write portale programs. It is
this contriution of rich control structure and data types, portaility and conciseness that
has contriuted to the popularity of C.
History of C
C programming language is asically developed for !NI" #perating $ystem.
!NI" was developed in %&'& at ell telephone laoratories. It was entirely written on
()( * assemly language. +fter !NI" has een implemented ,en -hompson
implemented a compiler for a new language called . used for transporting !NI" onto
other machines. . was heavily influenced y .C(/ 0.asic Camridge (rogramming
/anguage1 written for writing system software. . was latter modified y )ennis 2itchie
who was also working at ell /as. 3e named the successor C. !ni4 was later rewritten
in C y )ennis 2itchie, -hompson and others y %&*5.
C Program Structure
+ asic fact aout computer programming is that all programs can e written
using a comination of only three control structures6 $equential, $elective and repetitive.
-he sequential structure consists of a sequence of program statements that are e4ecuted
one after another in order, the selective structure consists of a test for a condition
followed y alternative paths that the program can follow, and the repetitive structure
consists of program statements that are repeatedly e4ecuted while some condition holds.
-he sequential structure can e pictorially represented as follows
Entry
Statement 1
Statement 2
Statement 3
Exit
+ll C programs are made up of one or more functions, each performing a particular task.
7very program has a special function named main. It is special ecause the e4ecution of
any program starts at the eginning of its main function.
www.technicalsymposium.com %
For More Notes and questions log on to www.technicalsymposium.com
+ typical C program has following sections
%. (reprocessor )irectives
8. 9loal :ariale )eclarations
5. Functions
In a C program, preprocessor directive, if present, should come first followed y gloal
variale definition if any.
aria!"e Dec"aration in C
%. -he variale can e 5% characters long.
8. -he variale can e any of a-;, +-<, =-& and the underscore.
5. $hould not e a keyword.
>. First character must e an alphaet
?. -he variale is case sensitive
Data Ty#es
7very programming language has its own data type. -he asic data types in C are
Int - an integer
Float @ a single precision floating point numer
Char - a character in C character set
)oule @ a doule precision floating point numer
aria!"es
:ariales are data oAects that are manipulated in a program. Information can e
stored in a variale and recalled later. :ariales must e declared efore they can e used
in a program.
Constants
+ constant is an entity whose value does not change during program e4ecution.
Constants are of five different types
%. Integer Constants
8. Floating point Constants
5. Character Constants
>. $tring Constants
www.technicalsymposium.com 8
For More Notes and questions log on to www.technicalsymposium.com
C O#erators
-he operators in C include
%. +rithmetic
8. +ssignment
5. 2elational
>. Increment and )ecrement
?. .it
'. /ogical or .oolean
*. Conditional e4pression
INPUT $ OUTPUT
-he important aspects of C programming language are its aility to handle input and
output 0IB#1. + program using input B output functions must include the standard header
file 0stdio.h1 in it using the directive.
Printf functions %CONIO&H' STDIO&H(
printf @ sends formatted output to stdout
fprintf @ sends formatted output to a stream
cprintf @ sends formatted output to the te4t window on the screen
Scanf )unction
$canf - reads data from stdin
Fscanf @ reads data from stream
-he 97-C3+2 and (!-C3+2 Function
*etc+ar' #utc+ar %STDIO&+(
- getchar is a macro that gets a character from stdin
- putchar is a macro outputs a character on stdout
T+e *ETCH an, *ETCHE )unction
- getch gets a character from console ut does not echo to the screen
- getche gets a character from console and echoes to the screen
gets' #uts
gets01 - gets a string from stdin
puts01 @ outputs a string to stdout
www.technicalsymposium.com 5
For More Notes and questions log on to www.technicalsymposium.com
CONDITION-. ST-TE/ENTS
If %con,ition( Statement
Chen an if statement is encountered in a program, condition is evaluated, if its value is
true, then the following statements are e4ecuted.
-he if statement allows conditional e4ecution of a group of statements.
If0e"se Statement
$DN-+"
If condition
$tatement %E
7lse
$tatement 8E
If the condition is true then statement % is e4ecuted else statement 8 is e4ecuted 0if it
e4ists1. 7lse part is optional.
.OOPS IN C
1HI.E .OOP
Chile loop provides the mechanism for looping as long as a specified condition is met.
-he while loop should e used in applications that do not require the modification of any
variales at each iteration.
$DN-+"
1+i"e %con,ition(
Statements
-he statement may e a single statement or a lock of statements that is to e repeated.
-he condition may e any e4pression, with true eing any non-;ero value. -he statements
are e4ecuted while the condition is true. Chen the condition ecomes false, program
control passes to the line after the loop code.
)OR .OOP

-his is used when the statements are to e e4ecuted more than once. -his is the most
widely used iteration construct. -he for loop supported y C is much more powerful than
its counterpart in any other programming language.
www.technicalsymposium.com >
For More Notes and questions log on to www.technicalsymposium.com
$DN-+"
)or %ex#12ex#22ex#3(
3
statements2
44444&
5
9enerally e4p% is an initiali;ation, e4p8 is condition checkingE e4p5 is either an
increment or decrement statement.
-he initiali;ation is usually an assignment statement that is used to set the loop control
variale. -he condition is a relational e4pression that determines when the loop will
terminate. -he increment determines how the loop control variale change each time the
loop is repeated.
1& 1rite a C #rogram to ,etermine t+e sum of o,, an, e6en num!ers&
F includeGstdio.hH
F includeGconio.hH
main01
I
int n, i, sevenJ=, soddJ=E
int aK8?LE
clrscr01E
printf06Mn 7nter the total numer to e entered6N1E
scanf0OPdN,Qn1E
printf0OMn 7nter the valuesN1E
for0iJ=EiGnEiRR1
I
if0aK iLP8JJ=1
sevenJsevenRaKiLE
else
soddJsoddRaKILE
S
printf0OMn -he $um of 7ven numer is PdN,seven1E
printf0OMn -he $um of #dd numer is PdN,sodd1E
getch01E
S
www.technicalsymposium.com ?
For More Notes and questions log on to www.technicalsymposium.com
2& 1rite a C #rogram to count t+e num!er of #ositi6e' negati6e an, 7ero
num!er in t+e gi6en "ist of num!ers&
F include Gstdio.hH
F include Gconio.hH
main01
I
int n, i, nposJ=, nnegJ=, n;eroJ=E
int aK8?LE
clrscr01E
printf06Mn 7nter the total numer to e entered6N1E
scanf0OPdN,Qn1E
printf0OMn 7nter the valuesN1E
for0iJ=EiGnEiRR1
I
if0aK iLH=1
nposJnposR%E
if0aKILG=1
nnegJnnegR%E
else
n;eroJn;eroR%E
S
printf0OMn -he numer of positive value is PdN,npos1E
printf0OMn -he numer of negative value is PdN,nneg1E
printf0OMn -he numer of ;eros is PdN,n;ero1E
getch01E
S
3& 1rite a C #rogram for tem#erature con6ersion&
FincludeGstdio.hH
FincludeGconio.hH
main01
I
int faren,cenE
clrscr01E
printf0OMn 7nter the farenheit value 6N1E
scanf0OPdN,Qfaren1E
cenJ0faren-581R?B&E
printf0OMn -he equivalent Centigrade value is PdN,cen1E
getch01E
S
www.technicalsymposium.com '
For More Notes and questions log on to www.technicalsymposium.com
8& 1rite a C #rogram to c+ec9 :+et+er t+e num!er is #rime or not&
FincludeGstdio.hH
FincludeGconio.hH
main01
I
int n, iE
clrscr01E
printf06Mn 7nter the total numer to e entered6N1E
scanf0OPdN,Qn1E
for0iJ8EiGJnB8EiRR1
I
if0nPiJ J=1
printf0OMn the given numer is not primeN1E
reakE
S
if0nPi1
printf0OMn the given numer is primeN1E
getch01E
S
;& 1rite a C #rogram to fin, :+et+er t+e gi6en num!er is #a"in,rome or not&
FincludeGstdio.hH
FincludeGconio.hH
main01
I
int n, iE
int p,s,eE
clrscr01E
printf06Mn 7nter the numer 6N1E
scanf0OPdN,Qn1E
eJ=E
pJnE
while0pTJ=1
I
sJpP%=E
eJ0eU%=1RsE
pJpB%=E
S
www.technicalsymposium.com *
For More Notes and questions log on to www.technicalsymposium.com
if0eJ J n1
printf0OMn the given numer is palindromeN1E
else
printf0OMn the given numer is not a palindromeN1E
getch01E
S
<& 1rite a C #rogram to fin, t+e sum of ,igits&
FincludeGstdio.hH
FincludeGconio.hH
main01
I
int n,q,r,sJ=E
clrscr01E
printf0OMn 7nter the noV1E
scanf0OPdN,Qn1E
while0nTJ=1
I
qJnB%=E
rJn-qU%=E
sJsRrE
nJqE
S
printf0OMn the sum of digits 6PdN,s1E
getch01E
S
=& 1rite a #rogram to fin, :+et+er t+e gi6en num!er is #erfect or not&
FincludeGstdio.hH
FincludeGconio.hH
main01
I
int a J =E
int mE
printf0O7nter a numer to check whether it is a perfect numer or not MnN1E
printf0O 7nter a numer MnN1E
scanf0OPldN,Qn1E
for 0mJ=EmGnEmRR1
www.technicalsymposium.com W
For More Notes and questions log on to www.technicalsymposium.com
I
if 0n P m J J = 1
a J a R mE
S
if 0a J J n1
printf0Othe given numer is perfect numer MnN1E
else
printf0Othe given numer is not a perfect numer MnN1E
getch01E
>& 1rite a #rogram to fin, :+et+er t+e gi6en num!er is -rmstrong or not&
FincludeGstdio.hH
FincludeGconio.hH
main01
I
int s J =E
int cJ =E
int m,n,E
printf0O7nter a numer to check whether it is a perfect numer or not MnN1E
printf0O 7nter a numer MnN1E
scanf0OPldN,Q1E
n J E
while 0H=1
I
c J P %=E
s J s R 0cUcUc1E
J B %=E
S
if 0s J J n1
printf0Othe given numer is armstrong numer MnN1E
else
printf0Othe given numer is not a armstrong numer MnN1E
getch01E
S
?& 1rite a C #rogram to fin, t+e gi6en num!er using "inear searc+ met+o,&
FincludeGstdio.hH
FincludeGconio.hH
main01
I
int n,aK5=L,sea,flagE
clrscr01E
www.technicalsymposium.com &
For More Notes and questions log on to www.technicalsymposium.com
printf0OMn 7nter the numer of terms 6N1E
scanf0OPdN,Qn1E
printf0OMn 7nter the values6N1E
for0iJ=EiGnEiRR1
scanf0OPdN,aKiL1E
printf0OMn 7nter the numer to e searched 6N1E
scanf0OPdN,Qsea1E
for0iJ=EiGnEiRR1
I
if0aKiL J J sea1
I
flagJ%E
reakE
S
else
flagJ=E
S
if0flagJ J %1
printf0OMn -he given numer Pd is present in the position numer
PdN,sea,i1E
else
printf0OMn -he given numer is not presentN1E
getch01E S
1@& 1rite a C #rogram to fin, t+e gi6en num!er using !inary searc+ met+o,&
FincludeGstdio.hH
FincludeGconio.hH

main01
I
int n,aK5=L,sea,flag,4,y,tE
int low,high,midE
clrscr01E
printf0OMn 7nter the numer of terms 6N1E
scanf0OPdN,Qn1E
printf0OMn 7nter the values6N1E
for0iJ=EiGnEiRR1
scanf0OPdN,aKiL1E
for04J=E4Gn-%E4RR1
for0yJ4R%EyGnEyRR1
I
if0aK4LHaKyL1
I
www.technicalsymposium.com %=
For More Notes and questions log on to www.technicalsymposium.com
tJaK4LE
aK4LJaKyLE
aKyLJtE
S
S
printf0OMn -he sorted numers are 6N1E
for04J=E4GnE4RR1
printf0OPdMnN,aK4L1E
printf0OMn 7nter the numer to e searched 6N1E
scanf0OPdN,Qsea1E
lowJ=E
highJnE
while0lowGJhigh1
I
midJ0lowRhigh1B8E
if0tGaKmidL1
highJmid-%E
if0tHaKmidL1
lowJmidR%E
if0tJ J aKmidL1
I
printf0OMn the numer Pd is present in the position PdN,t,mid1E
flagJ=E
reakE
S
if0mid J J% X X midJ J n1
reakE
S
if0flag1
printf0OMn -he given numer is not presentN1E
getch01E
S
11& 1rite a #rogram to #rint fi!onacci series using functions
Finclude G$-)I#.3H
Finclude GC#NI#.3H
void main01
I
int nE
void fio0int1E
www.technicalsymposium.com %%
For More Notes and questions log on to www.technicalsymposium.com
clrscr01E
printf0OMtMt (2#92+M -# (2IN- -37 FI.#N+CCI $72I7$ MnN1E
printf0OMn 7nter the numer of terms to e in the series Mn O1E
scanf0OPdN,Qn1E
fio0n1E
getch01E
S
void fio0int num1
I
int IJ%,ct,ft,stE
ft J =E
st J %E
printf0OMt Pd Mt PdN,ft,st1E
while0IGJnum-81
I
ct J ft R stE
ft J stE
st J ctE
printf0OMtPdN,ct1E
IRRE
S
S
12& Program to #erform t+e matrix a,,itions
Finclude Gstdio.hH
Finclude Gconio.hH
void main01
I
int aK%=LK%=L,K%=LK%=L,cK%=LK%=L,row,col,r,co,I,A,kE
clrscr01E
printf0OMtMt Matri4 +dditionMnN1E
printf0O7nter 2ow order of Matri4 + 6 O1E
scanf0OPdN,Qrow1E
printf0O7nter Column order of Matri4 + 6 O1E
scanf0OPdN,Qcol1E
printf0O7nter 2ow order of Matri4 . 6 O1E
scanf0OPdN,Qr1E
printf0O7nter Column order of Matri4 . 6 O1E
scanf0OPdN,Qco1E
if 00rowTJr1 XX 0col TJ co1 1
I
printf0OMatri4 Multiplication is impossileMnN1E
getch01E
S
www.technicalsymposium.com %8
For More Notes and questions log on to www.technicalsymposium.com
else
I
printf0O7nter First Matri4 7lements 6 O1E
for 0IJ=EIGrowEIRR1
for 0AJ=EAGcolEARR1
scanf0OPdN,QaKILKAL1E
printf0O7nter $econd Matri4 7lements 6 O1E
for 0IJ=EIGrEIRR1
for 0AJ=EAGcoEARR1
scanf0OPdN,QKILKAL1E
for 0IJ=EIGrowEIRR1
for 0AJ=EAGcolEARR1
cKILKAL J aKILKAL R KILKALE
printf0O-he resultant matri4 is MnN1E
for 0IJ=EIGrowEIRR1
I
for 0AJ=EAGcolEARR1
I
printf0OPtPdN,cKILKAL1E
S
pritnf0OMnN1E
S
S
13 & Program to #rint t+e factoria" num!er
Finclude Gstdio.hH
Finclude Gconio.hH
void main01
I
int nE
clrscr01E
printf0Oprogram to print the factorialMnN1E
printf0OMnMn 7nter the numer 6 O1E
scanf0OPdN,Qn1E
factorial0n1E
getch01E
S
void factorial0doule 41
I
doule fact J %E
for0IJ%EIGJnEIRR1
I
fact J fact U IE
printf0O-he factorial of a given numer is PdMnN,fact1E
S
www.technicalsymposium.com %5
For More Notes and questions log on to www.technicalsymposium.com
18& Program to im#"ement t+e To:er of Hanoi
Finclude Gstdio.hH
Finclude Gconio.hH
void main01
I
void transfer0int,char,char,char1E
int nE
clrscr01E
printf0OMtMt -#C72$ #F 3+N#I MnN1E
printf0O3ow many disks Y O1E
scanf0OPdN,Qn1E
transfer0n,Z%Z,ZrZ,ZcZ1E
getch01E
S
void transfer0int n, char from, char to, char temp1
I
if0nH=1
I
transfer0n-%,from,temp,to1E
printf0OMove disk Pd from Pc to Pc MnN,n,from,to1E
transfer0n-%,temp,to,from1E
S
returnE
S
1;& Program to count t+e num!er of 6o:e"s' consonants' ,igits' :+ite s#ace
c+aracters an,
in a "ine of text using #ointers
Ainclude Gstdio.hH
main01
I
char lineKW=LE
int vowels J =E
int cons J =E
int digits J =E
int ws J =E
int other J =E
void scan[line0char lineKL, int Upv, int Upc, int pd, int Upw, int Upo1E
printf0O7nter a line of te4t MnN1E
scanf0OPK\MnL,line1E
scan[line0line, Qvowels, Qcons, Qdigits, Qws, Qother1E
www.technicalsymposium.com %>
For More Notes and questions log on to www.technicalsymposium.com
printf0OPd Pd Pd Pd PdN,vowels,cons,digits,ws,other1E
return0=1E
S
void scan[line0char lineKL, int Upv, int Upc, int Upd, int Upw,int Upo1
I
char cE
int count J =E
while00c J toupper0lineKcountL11 TJ ]M=Z1
I
if 0c J J ]+Z X X c J J ]7Z X X c J JZIZ XX c J J ]#Z XX c J J ]!Z1
RR UpvE
else if 0c H J ]+Z QQ c G J ]<Z1
RR UpcE
else if 0 c H J ]=Z QQ c G J ]&Z1
RR Upd E
else if 0c J J ] ] X X c J J ]M=Z1
RR UpwE
else
RR UpoE
RR countE
S
returnE
S
1<& Program to im#"ement to )"oy,s Triang"e
Finclude Gstdio.hH
void main01
I
int n,i,A,4J%E
clrscr01E
printf0VMtMtMtFloyds -riangleMnV1E
printf0VMtMtMtJJJJJJJJJJJJJJJMnV1E
printf0V7nter the no of /ines6V1E
scanf0VPdV,Qn1E
for0iJ=EiGnEiRR1
I
for0AJ=EAGJiEARR1
I
printf0VP>dV,41E
4RRE
S
printf0VMnV1E
S
getch01E S
www.technicalsymposium.com %?
For More Notes and questions log on to www.technicalsymposium.com
1=& Program to im#"ement to Pasca" Triang"e
Finclude Gstdio.hH
void main01
I
int iJ%,A,k,m,nE
clrscr01E
printf0VMtMtMt(ascal -riangleMnV1E
printf0VMtMtMtJJJJJJJJJJJJJJJMnV1E
printf0V7nter the no of /ines6V1E
scanf0VPdV,Qn1E
for0AJ=EAGnERRA1
I
for0kJ5?-8UAEkH=Ek--1
printf0V V1E
for0mJ=EmGJAERRm1
I
if00mJJ=1XX0AJJ=11
iJ%E
else
iJ0iU0A-mR%11BmE
printf0VP>dV,i1E
S
printf0VMnV1E
S
getch01E
S
1>& Program to im#"ement sine series
Finclude Gstdio.hH
Finclude Gmath.hH
void main01
I
float d,4,sumJ=,fact0int1E
int terms,signJ%,iE
clrscr01E
printf0VMtMtMt $ine $eries MnV1E
printf0VMtMtMt JJJJJJJJJJJ MnV1E
printf0VMn7nter the " value6V1E
scanf0VPfV,Qd1E
printf0VMn7nter the numer of terms6V1E
scanf0VPdV,Qterms1E
4J5.%>B%W=UdE
www.technicalsymposium.com %'
For More Notes and questions log on to www.technicalsymposium.com
for0iJ%EiGJtermsEiRJ81
I
sumJsumRsignUpow04,i1Bfact0i1E
signJ-signE
S
printf0VMn-he value of sine0P>.8f1is PW.>fV,d,sum1E
getch01E
S
float fact0int n1
I
float fJ%E
int iE
for0iJ%EiGJnERRi1
fUJiE
return0f1E
S
1?& Programs on /ani#u"ations on strings
Finclude Gstdio.hH
void main01
I
int ch,i,A,l,m,sign,c,l%,kE
char nameKW=L,name%KW=L,name8KW=L,namerKW=L,nameffKW=L,ansJ^y^E
clrscr01E
printf0VMtMtMtManipulations on $tringsMnV1E
printf0VMtMtMtJJJJJJJJJJJJJJJJJJJJJJJJMnV1E
printf0V%.ConcatenationMnV1E
printf0V8.2everseMnV1E
printf0V5.FindMnV1E
printf0V>.2eplaceMnV1E
printf0V?./engthMnV1E
printf0VChoice6V1E
scanf0VPdV,Qch1E
switch0ch1
I
case %6
I
printf0VMtMtConcatenationMnV1E
printf0VMtMtJJJJJJJJJJJJJMnV1E
printf0V7nter the first string MnV1E
scanf0VPsV,name1E
printf0V7nter the second string MnV1E
scanf0VPsV,name%1E
iJAJ=E
while0nameKiLTJ^M=^1
www.technicalsymposium.com %*
For More Notes and questions log on to www.technicalsymposium.com
I
name8KiLJnameKiLE
iRRE
S
while0name%KALTJ^M=^1
I
name8KiLJname%KALE
iRRE
ARRE
S
name8KiLJ^M=^E
printf0V2esultant $tring in name8 isPsV,name81E
reakE
S
case 86
I
printf0VMtMt2everseMnV1E
printf0VMtMtJJJJJJJMnV1E
printf0V7nter the string MnV1E
scanf0VPsV,name1E
iJAJ=E
while0nameKiLTJ^M=^1
iRRE
while0--iHJ=1
name%KARRLJnameKiLE
name%KALJ^M=^E
printf0VMn-he reversed $tring isPsV,name%1E
reakE
S
case 56
I
printf0VMnMtMtFindMnV1E
printf0VMtMtJJJJMnV1E
printf0VMn7nter first string6V1E
scanf0V PK\MnLV,name1E
printf0V7nter search string6V1E
scanf0V PK\MnLV,name%1E
lJstrlen0name1E
l%Jstrlen0name%1E
for0iJ=EiGlERRi1
I
cJ=E
if0nameKiLJJname%KcL1
I
mJiE
signJ=E
www.technicalsymposium.com %W
For More Notes and questions log on to www.technicalsymposium.com
while0name%KcLTJ^M=^QQsignTJ%1
I
if0nameKmLJJname%KcL1
I
mRRE
cRRE
S
else
signJ%E
S
if0signJJ=1
I
printf0V-he given string is presentV1E
printf0VMnIts starting position isPdV,iR%1E
e4it0%1E
kJ-%E
S
S
if0kG=1reakE
S
if0signTJ=1
printf0V-he given string is not presentV1E
reakE
S
case >6
I
iJ=E
AJ=E
strcpy0nameff,V V1E
puts0V7nter the string6V1E
scanf0V PK\MnLV,name1E
fflush0stdin1E
puts0V7nter find stringV1E
scanf0V PK\MnLV,name%1E
fflush0stdin1E
puts0V7nter replace string6V1E
scanf0V PK\MnLV,namer1E
fflush0stdin1E
lJstrlen0name1E
strcat0namer,V V1E
while0iGl1
I
AJ=E
for0kJ=EkGW=ERRk1
name8KkLJ^ ^E
www.technicalsymposium.com %&
For More Notes and questions log on to www.technicalsymposium.com
while0nameKiLTJ^ ^QQnameKiLTJ^M=^1
I
name8KALJnameKiLE
RRiE
RRAE
S
name8KALJ^M=^E
RRiE
if00strcmp0name8,name%11JJ=1
I
strcat0nameff,V V1E
strcat0nameff,namer1E
S
else
I
strcat0nameff,V V1E
strcat0nameff,name81E
S
S
puts0Vstring after replacementV1E
puts0nameff1E
reakE
S
case ?6
I
iJ=E
printf0V7nter $tring6V1E
scanf0V PK\MnLV,name1E
while0nameKiLTJ^M=^1
iRRE
printf0VMn-he length of the given string isPdV,i1E
reakE
S
S
getch01E
S
www.technicalsymposium.com 8=
For More Notes and questions log on to www.technicalsymposium.com
Data Structures
-n intro,uction to CB
)ennis 2itchie at +- Q - .ell laoratory, Murray 3ill, New _ersey,
developed the programming language C in %&*8. -he languages .C(/ and . mainly
influenced it. It was named as C to present it as the successor of . language which was
)esigned earlier y ,en -hompson in %&*= for the first !NI" system on the )7C()(-*
Computer.
Ho: to run C #rogramB
%. From the Ms )os prompt start C y typing ]tcZ.
8. #pen a file y selecting File X #pen X File name from the I)7 menu. #r press
F5 ,ey
5. 2un the program y selecting 2un X 2un, #r press
CtrlRF& ,ey
>. -o see the programZs output select Cindow X !ser screen or press
+ltRF? ,ey.
Ce may compile and run the programs from the )os command
/ine like tcc Filename G7nterH.
+fter the program is compiled, we may run it and view the output y typing
Filename G7nterH
Pro!"em so"6ing using com#uter6
-o solve a prolem using a computer, the following steps are required 6
+ program is developed using a high level programming language 0program
development1
-he developed program is entered into a commuter 0(rogram editing1.
-he edited program is translated and is produced as an e4ecutale machine code.
-he 74ecutale machine code is run in the computer to carry out the actual task
0e4ecution1.
-o implement the aove steps, the programmer develops a program and the
developed program is entered and edited with the help of an editor. Normally the editor is
provided along with the compiler. +fter editing the program, the compilation commands
us used for the translation process. -hen the e4ecution command is used to run the
program to get the desired output.
www.technicalsymposium.com 8%
For More Notes and questions log on to www.technicalsymposium.com
Com#i"ation 6
3igh-level languages allow some 7nglish @like words and mathematical
e4pressions that facilitate the etter understanding of the logic involved in a program.
3igh-level languages are machine independent. $ince a computer system cannot follow
programs written in a high language, high language programs are translated into low-
level language programs and then e4ecuted.
-ranslation of a high-level language program to allow level language
program is done y software known as Compiler. #Aect code is an intermediate code
etween the source code and the e4ecutale code.
.in9ing6
/inker performs the linking of liraries with the oAect code, to make the
generated oAect code into an e4ecutale machine code. -hus the oAect code ecomes an
input to the linker, which produces an e4ecutale machine code. $ometimes programs are
divided into modules and these modules are compiled separately and then linked y the
linker and e4ecuted.
Chen running a program, the following files will e created automatically.
#._ 0#Aect file1
7"7 074ecutale file1
.ak 0.ackup file1
$C( 0$wap file1
Data Structures Definition
)ata $tructure is a speciali;ed format for storing data so that the
dataZs can e organi;ed in an efficient way.
C"assification
Primiti6e Non C Primiti6e
74ample6 -
Integer
2eal
Character .inear Non C .inear
(ointer 74ample6 - 74ample6 -
/ogical /inear /ist 9raph
$tack -ree
`ueue
www.technicalsymposium.com 88
For More Notes and questions log on to www.technicalsymposium.com
-rray
+n array is a finite collection of similar elements stored in contiguous location.
-he operations done on an array are6 -
Insertion
)eletion
Changing a particular element
.in9e, .ist
-here are three types of linked lists. -hey are6 -
$ingle /inked /ist
)ouly /inked /ist
$ingly Circular /inked /ist
)ouly Circular /inked /ist
Sing"e .in9e, .ist
Node structure
-he data field contains the data elements that have to e stored in the list. -he
pointer will point the ne4t node in the list.
-he operations done on a list are6 -
Insertion
)eletion
Insertion
Insertion in t+e +ea, no,e
-o insert a node in the head node, Aust change the pointer field of the new
node to point to the head node. /et the new node e ]Temp and the head node e
Head, then the insertion is
Temp data = X;
Head next = head
www.technicalsymposium.com 85
Data Pointer
)ie", )ie",

For More Notes and questions log on to www.technicalsymposium.com
Insertion in t+e mi,,"e no,e
-o insert in the middle node we need to change two pointers. /et the new
node e ]Temp and the present node is Present and, the ne4t node to the
present node is future. -he pointers used are ]data for the data field, next
to the pointer field, the data to e inserted is ]X then the insertion is
Temp data = X
Present next = temp
Temp next = future
Insertion in t+e "ast no,e
-o insert an element in the last position Aust change the pointer field of the present
last node to point to the new node, then set the pointer field of the new node to
NULL. /et the new node e ]Temp and the present node is Present. -he
pointers used are ]data for the data field, next to the pointer field, the data to e
inserted is ]X then the insertion is
Present next =Temp
Temp next =null
Temp data = X
)eletion
De"etion in t+e +ea, no,e
-o delete a node in the head node, Aust point the head node as the second node.
/et the head node e Head, and then the deletion is
Head next = head
De"etion in t+e mi,,"e no,e
-o delete a node in the middle we need to change two pointers. /et the node to e
deleted is ]Temp and the node previous to the node to e deleted is Present
and, the ne4t node to the present node is future. -he pointers used are ]data for
the data field, next to the pointer field, the data to e inserted is ]X then the
insertion is
Present next = future
De"etion in t+e "ast no,e
-o delete an element in the last position Aust change the pointer field of the
previous node to the last to null. /et the last node e ]Temp and the previous
node is Present. -he pointers used are ]data for the data field, next to the
pointer field, the data to e inserted is ]X then the insertion is
www.technicalsymposium.com 8>
For More Notes and questions log on to www.technicalsymposium.com
Previous next =NULL
Sing"y Circu"ar .in9e, .ist
-he advantage of using Circular /inked /ist is the last null pointer is replaced and the
pointer field of the last node points to the first node, due to this circular arrangement the
traversing ecome quite easier. -he insertion and deletion in the first and middle are same
as singly linked list e4cept the last node.
Insertion
Insertion in t+e "ast no,e
-o insert a node in the last position, insert the new node after the current last node,
and then change the pointer field of the new node to point to the first node. /et the
last node e last, the new node to e inserted to e ne, the first node in the list to e
first. -he pointers used are ]data for the data field, next to the pointer field, the data
to e inserted is ]X then the insertion is
Last next = ne
Ne next =first
)eletion
De"etion in t+e "ast no,e
-o delete a node in the last position, change the pointer field of the
previous node to the current last to point the first node. /et the last node
e last, the previous node to the current last node to e pre, the first node
in the list to e first. -he pointers used are ]data for the data field, next
to the pointer field, the data to e inserted is ]X then the deletion is
Prev next = first
Stac9
+n important suclass of lists permits the insertion and deletion of an
element to occur only at one end. + linear list of this type is known as ]sta!"Z. -he
insertion is referred to as ]push. -he deletion is referred to as ]pop. -he two
pointers used for accessing is top Q #ottom pointer.
(!$3 @ $toring the element into the stack.
Check topGJ allowed si;e if yes increment the top position and
store the value in the top position.
(#( - )eleting the element from the stack. If topGJ we can not delete.
#therwise decrement the top y one and return the topR% element.
Dueue
-he information in this list is processed in the same order as it was received, that is
first in first out order 0FIF#1 or a first @ come first @ served 0FCF$1 asis. -his type of
www.technicalsymposium.com 8?
.ottom (ointer
For More Notes and questions log on to www.technicalsymposium.com
frequently used list is known as $ueue. Ce have two pointers to access the queue. -hey
are
%. Front 0used for deletion1
8. 2ear 0!sed for insertion1
Insertion B
if rearHn queue overflow
else
increment the rear pointer and insert the value in the rear position.
De"etion B
If front J= then queue underflow
7lse
Increment the front pointer and return the front-% value
Tree
+n important class of digraph, which involves for the description of hierarchy. +
directed tree is an acyclic digraph which has one node called root with in degree =, while
other nodes have in degree %. 7very directed tree must have at least one node. +n isolated
node is also called as directed tree. -he node with out degree as = is called as leaf. -he
length of the path from root to particular node level of the node. If the ordering of the
node at each level is prescried then the tree is called as ordered tree.
Einary Tree
If a tree has at most of two children, then such tree is called as .inary tree. If the
elements in the inary tree are arranged in the following order
.eft e"ement is "esser t+an t+e root
Rig+t e"ement is greater t+en t+e root
No ,u#"ication of e"ements
-hen such inary tree is called as Einary Searc+ Tree
O#erations #erforme, in a !inary tree areB
o Inserting a node
o )eleting a node
o -raversing the tree
Tra6ersing /et+o,s
%. (re @ order method
8. In @ order method
5. (ost @ order method
>. Converse (re @ order method
?. Converse In @ order method
'. Converse post @ order method
www.technicalsymposium.com 8'
For More Notes and questions log on to www.technicalsymposium.com
Pre C or,er met+o,
-his method gives the tree key value in the following manner6 -
%. (rocess the root
8. -raverse the left su tree
5. -raverse the right $u tree
In C or,er met+o,
-his method gives the tree key value in the following manner6 -
%. -raverse the left su tree
8. (rocess the root
5. -raverse the right $u tree
Post C or,er met+o,
-his method gives the tree key value in the following manner6 -
%. -raverse the left su tree
8. -raverse the right $u tree
5. (rocess the root
Sorting
$orting is, without dout, the most fundamental algorithmic prolem
%. $upposedly, 8?P of all C(! cycles are spent sorting
8. $orting is fundamental to most other algorithmic prolems, for e4ample inary
search.
5. %an& different approaches lead to useful sorting algorithms, and these ideas can
e used to solve many other prolems.
Chat is sortingY It is the prolem of taking an aritrary permutation of n items and
rearran'in' them into the total order,
Issues in Sorting
Increasing or Decreasing Order? - -he same algorithm can e used y oth all we need
do is change to in the comparison function as we desire.
What about equal keys? @ May e we need to sort on secondary keys, or leave in the
same order as the original permutations.
www.technicalsymposium.com 8*
For More Notes and questions log on to www.technicalsymposium.com
What about non-numerical data? - +lphaeti;ing is sorting te4t strings, and liraries
have very complicated rules concerning punctuation, etc. Is (ron)*illiams efore or
after (ron +meri!a efore or after (ron, ,ohnY
Ce can ignore all three of these issues y assuming a !omparison fun!tion which depends
on the application. -ompare .a,#/ should return aaG^^, aaH^^, or ^^J^^.
-##"ications of Sorting
#ne reason why sorting is so important is that once a set of items is sorted, many
other prolems ecome easy.
Hea#s
+ heap is a complete inary tree with values stored in its nodes such that no child has a
value igger than the value of the parent.
.elow is a heap.
&
B M
W 8
B M
' >
+ heap provides a representation for a priorit& $ueue. 74ample6 messages processed y
priority at a server
messages given priority weighting, higher numers give etter service
highly dynamic, messages coming and going frequently
need efficient insert new message and remove highest priority message
2emoval causes heap to e reheapified. For e4ample if we remove &
&
B M
W 8
B M
' >
then we reheapify y copying rightmost leaf to root 0> ecomes the root1
>
B M
W 8
B
'
www.technicalsymposium.com 8W
For More Notes and questions log on to www.technicalsymposium.com
and then we recursively reestalish the heap property as follows6 if the parent is greater
than a child, swap the parent with the highest priority child. ,eep swapping until no more
swaps are possile. $o in the aove tree, first we would swamp > with W.
W
B M
> 8
B
'
-hen we would swap > with '.
W
B M
' 8
B
>
-he final swap yields a heapT
-he cost of removing an item 0reheapifiying after removing the item1 is #0log n1. -he
algorithm Aust traverses one path in the tree, which is #0log n1 in length. For each node
on that path it performs at most two comparisons and one swap 05 operations -H constant
time1. $o overall the algorithm has a worst case time comple4ity of #0log n1.
$pace comple4ity is #0n1 since a sequential array representation can e used.
Duic9 sort
is a very efficient sorting algorithm invented y C.+.2. 3oarer. It has two
phases6
-he partition phase and
-he sort phase.
+s we will see, most of the work is done in the partition phase - it works out where to
divide the work. -he sort phase simply sorts the two smaller prolems that are generated
in the partition phase.
-his makes `uick sort a good e4ample of the ,i6i,e an, conFuers strategy for solving
prolems. 0Dou^ve already seen an e4ample of this approach in the inary search
procedure.1 In quick sort, we divide the array of items to e sorted into two partitions and
then call the quick sort procedure recursively to sort the two partitions, i.e. we divide the
prolem into two smaller ones and !on$uer y solving the smaller ones. -hus the conquer
part of the quick sort routine looks like this6
www.technicalsymposium.com 8&
For More Notes and questions log on to www.technicalsymposium.com
quicksort0 void Ua, int low, int
high 1
I
int pivotE
BU -ermination conditionT UB
if 0 high H low 1
I
pivot J partition0 a, low, high 1E
quicksort0 a, low, pivot-% 1E
quicksort0 a, pivotR%, high 1E
S
S
Initial $tep - First (artition
$ort /eft (artition in the same way
For the strategy to e effective, the partition phase must ensure that all the items in one
part 0the lower part1 and less than all those in the other 0upper1 part.
-o do this, we choose a pivot element and arrange that all the items in the lower part are
less than the pivot and all those in the upper part greater than it. In the most general case,
we don^t know anything aout the items to e sorted, so that any choice of the pivot
element will do - the first element is a convenient one.
DiG9straHs -"gorit+m
)Aikstra^s algorithm 0named after its discover, 7.C. )iAkstra1 solves the prolem of
finding the shortest path from a point in a graph 0the sour!e1 to a destination. It turns out
that one can find the shortest paths from a given source to all points in a graph in the
same time, hence this prolem is sometimes called the sing"e0source s+ortest #at+s
prolem.
*ra#+ Tra6ersa"
$ystematic traversals of graph are similar to preorder and post order traversal for trees.
-here are two graph traversals, depth-first and readth-first search. Frequently the graph
searches start at an aritrary verte4. -he searches are efficient if they are done in #0n 0
m1, where n is the numer of vertices and m the numer of edges.
9raph traversal can e used to determine the general characteristic of the graph, or to
solve a specific prolem on a particular graph, for e4ample6
2outing phone calls, or packets
(lanning a car trip
/ocate particular vertices, for e4ample a win position in a game.
www.technicalsymposium.com 5=
For More Notes and questions log on to www.technicalsymposium.com
De#t+0first Searc+
Ce start the graph traversal at aritrary vertices, and go down a particular ranch until we
reach a dead end. -hen we ack up and go as deep possile. In this way we visit all
vertices, and all edges.
Ereat+0)irst Searc+
.readth-first search visit all adAacent vertices efore going deeper. -hen we go deeper in
one of the adAacent vertices.
S#arse /atrix B
+ matri4 consists of more numer of ;eros is called sparse matri4. #nce the matri4
is stored as it is then there is wastage of memory. For an efficient memory utili;ation the
sparse matri4 can e stored in a linear form. -he linear form can e of array type or
linked list type.
D-T- STRUCTURES
DefinitionB
)ata structure is collection of data elements organi;ed in a specified manner and
accessing functions are defined to store and retrieve individual data elements. )ata
structures are sometimes called )ata types.
C"assification of Data StructureB
+ data type may e defined as a set and the elements of the set are called the
values of the type. -here are four asic or atomic or primitive data types in C. -hey are
int, float, char and doule. -he $imple data types uilt from primitives are arrays ,
pointers, strings and records with which we can uild new types called structured or
composite types such as stacks, queues, and trees etc. -he structured data types can e
categori;ed as linear and non-linear. -he linear data structures are stacks, queues and
linked lists. -he non-linear data structures are trees and graphs.
Stac9s
DefinitionB
+ stack is an ordered collection of items into which new items may e inserted and
from which items may e deleted at one end, called the top of the stack. -he first e4ample
of stack, which permits the selection of only its end element , is a pile of coins.
$econd e4ample could e a pile of trays or a ooks lying one aove the other.
/et us draw a stack containing integers as in the following figure.
top
www.technicalsymposium.com
?
&
%
5
*
5%
For More Notes and questions log on to www.technicalsymposium.com
3ere, ? is the current of the stack. If we add any element in the stack, it will e placed on
top of ? , and if we delete an element , it will e ?, which is on top of the stack.
O#erations on Stac9sB
+ssociated with the stack , there are several primitives operations. Ce can define
the following necessary operations on stack.
a1 create0s1 - -o create s as an empty stack.
1 push0s,i1 - -o insert the element I on top of the stack s.
c1 pop0s1 - -o remove the top element of the stack and to return the removed
element as a function value.
d1 top0s1 - -o return the top element of stack0s1.
e1 empty0s1 - -o check whether the stack is empty or not. It returns true if stack is
empty and returns false otherwise.
If a stack is empty and it contains no element, it is not possile to pop the stack.
-herefore, efore popping an element, we must ensure that the stack is not empty.
PUSH I POP OPER-TIONSB
1+en :e a,, an e"ement to a stac9' :e stay t+at :e #us+ it on t+e
stac9 an, if :e ,e"ete an e"ement from a stac9' :e say t+at :e #o# it from t+e stac9&
.et us see +o: stac9 s+rin9s or gro:s :+en :e #o# or #us+ an e"ement in t+e
fo""o:ing figures&
Pus+ %>( on t+e stac9
top
(ush 0>1 on to stack
top
www.technicalsymposium.com
W
?
&
%
5
*
>
W
?
&
%
5
*
58
For More Notes and questions log on to www.technicalsymposium.com
(op an element from the stack
-op (opped element J >
(op an element from the stack

-op (opped element J W
Ce may notice that the last item pushed onto a stack is always the first that will e
popped from the stack. -hat is why stack is called last in, first out or /IF# in short.
Im#"ementation of Stac9s

T+ere are t:o :ays to im#"ement stac9s' one using arrays an, ot+er is
using "in9e, "ist&
-rrayB
$ince the elements of the stack are ordered , an ovious choice would e an array
as a structure t contains a stack. Ce can fi4 one end of the array as ottom of the stack.
-he other end of the array may e used as a top of the stack, which keeps shifting
constantly as items are popped and pushed. Ce must store the inde4 of the array
containing the top element.
www.technicalsymposium.com
W
?
&
%
5
*
?
&
%
5
*
55
For More Notes and questions log on to www.technicalsymposium.com
Ce can , therefore, declare a stack as a structure containing two fields- an array to hold
the elements of the stack, and an integer top to indicate the position of the current top of
the stack within the array.
F define M+" ?=
struct stackI

int topE
int elements K?LE
SE
struct stack sE
3ere s is defined to e a stack containing elements of type integer . -he ma4imum
numer of elements in the stack is defined to e ?=. 7lements K=L contain the first element
so that the value of top is =. If there are five elements in the stack, the value of top will e
four and the top element is in elementsK>L.
+ stack is empty when it contains no elements we can indicate this y making top as @%.
Ce can write our function clearstack as
clearstack0ts1
struct stack UtsE
I
ts-Htop J -%E
S
+nother operation is to check whether the stack is empty. -o do this we must check
whether s.top J J -%.
/et us now consider the (!$3 operation . -o push or add an element we must perform
the two steps6
i. increment top indicator
ii. put the new element at the new top.
Ce might code the (!$3 Q (#( operations as follows6
push0ts,41
$truct stack UtsE
Int 4E
I
if 0fullstack0ts11I
printf0 O PsN, O $tack overflowN1E
e4it0%1E
www.technicalsymposium.com 5>
For More Notes and questions log on to www.technicalsymposium.com
S
else
ts-HelementsKRR0ts-Htop1L J 4E
returnE
S
-his routine increments the top y % and puts 4 into array s.elements at the new top
position. In this routine we use another routine Full $tack which checks whether the stack
is full, efore we push an element onto stack. + stack is full when ts-Htop J J M+"-%.
Full $tack routine as follows6
fullstack 0ts1
struct stack UtsE
I
if 0 ts-Htop J J M+"-%1
return0%1E
else
return0=1E
S
-o remove an element or pop an element from the stack, we must first check
the possiility of underflow as it is quite possile that someody tries to pop an element
from an empty stack. -herefore, we can write function (#( as,
(op0ts1
struct stack UtsE
I
if 0empty0ts11
printf0 O P sN , O stack underflowN1E
return0=1E
else
return0ts-HelementsKts-Htop--L1E
S
Ce can write function empty 0s1 that returns % if the stack is empty and = if it is not
empty as follows6
empty0ts1
struct stack UtsE
I
if 0 ts -H top J J -%1
return 0%1E
else
return0=1E
S
www.technicalsymposium.com 5?
For More Notes and questions log on to www.technicalsymposium.com

$tack as a /inked /ist 0 !sing (ointers16
!sing this representation we are using the pool of availale nodes and we
will never have to test whether a particular stack is full. Ce can declare such as a stack as
follows.
Node structure6 7ach node has two fields. i.e. )ata and Ne4t field

)ata field Ne4t field

$tack- Node representation6
+ . C )
$tack 7nd node
-op element
)eclaration 6 0 !sing CRR1
F include Giostream.hH
F include G process.hH
class staI
struct node I
int dataE
node U ne4tE
S Ustack E
pulic 6
void push01E
void pop01E
void disp01E
S
PUSH OPER-TIONB
:oid sta 66 push01
I
int nE
node tempE
temp J new nodeE 5
cout GG O (ush the element O GG endlE 0 First node of
www.technicalsymposium.com 5'
For More Notes and questions log on to www.technicalsymposium.com
temp the stack1.
cin HH temp-HdataE
temp-Hne4tJN!//E stack
if0stackJ J N!//1
stackJtempE
else 5
I
temp-Hne4tJstackE stack
stackJtempE >
S >
temp
S
> 5
stack

POP O#erationB
stack 8 > 5
:oid sta 66 pop01
I temp
node UtempE
if 0stackJ J N!//1
cout GG O $tack is empty O GG endlE
else I stack
tempJ stackE
stackJ stack-Hne4tE temp
cout GG O(opped element O GG endlE
cout GG temp-HdataE
delete tempE
S
S

TREE TR-ERS-.B
Chen traversing a inary tree, we want to treat each node and its su trees in the
same fashion. If we let /, :, and 2 stand for moving left, visiting the node, and moving
right when at a node, then there are si4 possile cominations of tree traversal6 /:2,
/2:, :/2, :2/, 2:/, and 2/:. If we adopt the convention that we traverse left efore
right, then only three traversals remain 6 /:2, /2: and :/2. -o these we assign the
names inorder, postorder, and preorder, respectively, ecause of the position of the : with
respect to the / and the 2.
www.technicalsymposium.com 5*
For More Notes and questions log on to www.technicalsymposium.com
Proce,ure for Preor,erB
%. :isit the root node.
8. -raverse the /eft su tree in preorder.
5. -raverse the 2ight su tree in preorder.
Exam#"eB
)ig&1


-he result is 6 R + .
-"gorit+mB
void preorder0node Unodeptr1
I
if 0 nodeptr TJ N!//1
I
printf0OPdMnN, nodeptr-Hdata1E BU visit the root node UB
preorder0nodeptr-Hleft1E BU -raverse the left su tree UB
perorder0nodeptr-Hright1E BU -raverse the right su tree UB
S
S
Proce,ure for Inor,erB
%. -raverse the /eft su tree in inorder.
8. :isit the root node
5. -raverse the 2ight su tree in inorder.
)ig&2
www.technicalsymposium.com 5W
For More Notes and questions log on to www.technicalsymposium.com

-he result is 6 + R .

void inorder0 node Unodeptr1
I
if 0 nodeptr TJ N!//1
I
inorder0nodeptr-Hleft1E BU -raverse the left su tree UB
printf0OPdMnN, nodeptr-Hdata1E BU :isit the root node UB
inorder0nodeptr-Hright1E BU -raverse the right su tree UB
S
S
Proce,ure for Postor,erB
%. -raverse the /eft su tree in postorder.
8. -raverse the 2ight su tree in postorder.
5. :isit the root node.
)ig&3

-he result is 6 + . R

void postorder0 node U nodeptr1
I
if 0nodeptr TJ N!//1
I
postorder0nodeptr-Hleft1E BU -raverse the left su tree UB
postorder0nodeptr-Hright1E BU -raverse the right su tree UB
printf0OPdMnN, nodeptr-Hdata1E BU :isit the root node UB
S
www.technicalsymposium.com 5&
For More Notes and questions log on to www.technicalsymposium.com
S
Fig.>
(27 #2)72 6 +,.,),C,7,+N) F
IN #2)72 6 .,),+,7,F,C
(#$-#2)72 6 ),.,F,7,C,+N) +
Fig.?.
www.technicalsymposium.com >=
For More Notes and questions log on to www.technicalsymposium.com
(27#2)72 6 U R + . B C )
IN#2)72 6 + R . U C B )
(#$-#2)72 6 + . R C ) B U
EIN-RJ SE-RCH TREES
DefinitionB
+ inary search tree is a inary tree. It may e empty. If it is not empty then
it satisfies the following properties6
%. 7very element has a key and no two elements have the same key.
8. -he keys in the left su tree are smaller than the key in the root.
5. -he keys in the right su tree are larger than the key in the root.
>. -he left and right su trees are also inary search trees.
It has two operations. -hey are,
%. Insertion
8. )eletion
Exam#"e )ig&

www.technicalsymposium.com >%
For More Notes and questions log on to www.technicalsymposium.com
-o construct 0Insertion1 the .inary search tree for the following elements6
8?, %?, 8*, %5, %*, 8', 8&, 8W




www.technicalsymposium.com >8
For More Notes and questions log on to www.technicalsymposium.com


www.technicalsymposium.com >5
For More Notes and questions log on to www.technicalsymposium.com
-o delete a particular node from the .inary search tree6
1& .eaf no,e
)eletion of a leaf node is quite easy. -o delete 8W from the
elow tree the left child field of its parent is set to = and the node disposed. -o delete the
%* from this tree, the right-child field of %? is set to = , and the node containing %* is
disposed.
-o delete a leaf node 8W

-o delete a leaf node %*

2& Non0"eaf no,eB
www.technicalsymposium.com >>
For More Notes and questions log on to www.technicalsymposium.com
-he deletion of a non-leaf element or node that has only one child is also easy. -he
node containing the element to e deleted is disposed, and the single-child takes the place
of the disposed node.
$o, to delete the element %? from the aove tree, we simply change the pointer from the
parent node 08?1 to the single-child node0%51.
3& Root no,eB
Chen the element to e deleted is in a non-leaf node that
has two children, the element is replaced y either the largest element in its left su tree
or the smallest one in its right su tree. -hen we proceed to delete this replacing element
from the su tree from which it was taken.
www.technicalsymposium.com >?
For More Notes and questions log on to www.technicalsymposium.com
If we wish to delete the element with key 8? from the aove tree, then we replace it y
either the largest element, %* , in its left su tree or the smallest element , 8' , in its right
su tree. $uppose we opt for the largest element in the left su tree. -he %* is moved in
to the root and the following tree is otained.
HE-PS
Priority DueueB
www.technicalsymposium.com >'
For More Notes and questions log on to www.technicalsymposium.com
-he priority queue is a data structure in which the intrinsic ordering of the
elements does determine the results of its asic operations. -here are two types of priority
queues6
+n ascending priority queue and a descending priority queue.
+n ascending priority queue is a collection of items into which items can e inserted
aritrarily and from which only the smallest item can e removed. + descending priority
queue is similar ut allows deletion of only the largest item.
Hea#s DefinitionB
+ ma4 0min1 heap is a tree in which the key value in each node is no smaller
0larger1 than the key values in its children 0if any1. + ma4 heap is a complete inary tree
that is also a ma4 tree. + min heap is a complete inary tree that is also a min tree.
/ax& Hea#
/in& Hea#
www.technicalsymposium.com >*
For More Notes and questions log on to www.technicalsymposium.com
DUEUE
DefinitionB
+ queue is an ordered collection of items from which items may e deleted at one
end 0 called the front of the queue1 and into which items may e inserted at the other end
0 called rear of the queue1. -his data structure is commonly known as FIF# or first-in-
first-out.
)ig&1
5 '
)ig&2
5 ' W
OPER-TION S ON DUEUESB
It has two operations. -hey are
www.technicalsymposium.com >W
For More Notes and questions log on to www.technicalsymposium.com
Insertion
)eletion
Insertion an element is popularly known as 7N` and deleting an element is known as
)7`. + minimum set of useful operations on queue includes the following.
i. C27+-7`0`1 @ which creates ` as an empty `ueue.
ii. 7N`0i1 @ which adds the element I to the rear of a queue and returns the new
queue.
iii. )7`0`1- which removes the element at the front end of the queue and returns
the resulting queue as well as the removed element.
iv. 7M(-D0`1- It checks the queue whether it is empty or not and returns true if
it is empty and returns false otherwise.
v. F2#N-0`1- which returns the front element of the queue without changing
the queue.
vi. `!7!7$I<70`1-which returns the numer of entries in the queue.
Ce can otain the queue y the following sequence of operations. Ce assume that the
queue in initially empty.
7N`0q,W1

? * W
7N`0q,&1
? * W &

7N`0q,>1
www.technicalsymposium.com >&
For More Notes and questions log on to www.technicalsymposium.com
? * W & >
4 J)7`0q1 7lement ? is deleted
* W & >
4 J)7`0q1 7lement * is deleted
W & >
I/P.E/ENTIN* THE DUEUE
-here are two ways to implement queue, one using arrays, and
another is using /inked list.
i& -rray B
/et us implement the queue within an array so that the array holds the
elements of the queue. -here are two variales front and rear to indicate the positions of
the first and last element of the queue within the array.
/et the si;e of the array e >. Initially let us assume that the queue is empty which means
front J = and rear J -%.
www.technicalsymposium.com ?=
For More Notes and questions log on to www.technicalsymposium.com
Em#ty DueueB

qK=L qK%L qK8L qK5L

front J =0array position1 rear J -% 0 N!//1
InsertionB
-here are two variales front and rear to indicate the positions of the first and
last element of the queue within the array. /et the si;e of the array e >. Initially let us
assume that the queue is empty which means front J = and rear J -%.+fter we have added
three elements to the queue rear ecomes 8 and front ecomes =. Now if we add one
more elements to the queue from the rear, the value of rear changes to 5. Now the queue
ecomes full.
7N`0q,51

5
qK=L qK%L qK8L qK5L

front J = rear J=
7N`0q,?1


5 ?
qK=L qK%L qK8L qK5L
front J = rear J %

7N`0q,*1
5 ? *
qK=L qK%L qK8L qK5L
front J = rear J 8
www.technicalsymposium.com ?%
For More Notes and questions log on to www.technicalsymposium.com
7N`0q,&1 K ` is full L
5 ? * &
qK=L qK%L qK8L qK5L
front J = rear J 5
De"etionB
+t this point, we delete one element. -he element which is deleted is 5. -his
leaves a hole in the first position. -o delete this element we must increment front, to
indicate the true first element of the queue and assign the value of that slot to 4. -o check
whether queue is empty or not, we must check whether front J rear.
-o add an element we must increment rear so that it points to the location ne4t to the rear
and place an element in that slot of the array. If we wish to add another element, and we
increment rear y %, rear ecomes equal to front, which indicates that the queue is full.
"J )7`0q1
? * &
qK=L qK%L qK8L qK5L
front J % rear J 5
4J)7`0q1

* &
qK=L qK%L qK8L qK5L
front J 8 rear J 5
4J)7`0q1
&
qK=L qK%L qK8L qK5L
front J 5 rear J 5
www.technicalsymposium.com ?8
For More Notes and questions log on to www.technicalsymposium.com
4J)7`0q1 K `ueue is emptyL
qK=L qK%L qK8L qK5L
front J rear J -%0N!//1
-herefore, the condition for full queue is that the ne4t slot of rear is equal to front and the
condition for empty queue is that front J rear. .efore we )7` an element from queue we
must make sure that queue is not empty and efore we 7N` an element we must ensure
that the queue is not full.
`ueue implementations in +22+D using CRR
class quI
(ulic 6
Int front, rear, n , qK%=LE

void get01I
coutGG O 7nter the `ueue si;e O GG endlE
cinHH nE
front J rear J-%E
S
void enq01E
void deq01E
SE

int I, aK%=LE
void qu 66 enq01I
int itemE
if 0 rear HJ n1
I
cout GG O `ueue is full MnNE
returnE
S
else
I
cout GG O 7nter the item to e insertedN GGendlE
cinHHitemE
rear J rearR%E
www.technicalsymposium.com ?5
For More Notes and questions log on to www.technicalsymposium.com
qKrearL J itemE
iRRE
S
S
void qu 66 deq01
I
int tE
if 0 front HJ rear1
I
cout GG O `ueue is 7mptyN GG endlE
returnE
S
else
I
front J fornt R%E
t J qKfrontLE
cout GG O -he deleted element 6 O GG t GG endlE
S
S
Im#"ementation of Dueue as .in9e, "ist
+nother way of implementing queues is as a linked list.
/et us have two pointers, front to the first element of the list and rear to the last element
of the list.

>
front
rear
.
class que I
struct node
I
int dataE
node Une4tE
S U front, UrearE

www.technicalsymposium.com
'
W
?>
For More Notes and questions log on to www.technicalsymposium.com
pulic6
void insq01E
void delq01E
que01I
front J rear J N!//E
S
SE
void que 66 insq01
I temp
int nE
node UtempE
temp J new nodeE front
cout GG O Insert the element O GG endlE
cin HH nE
temp-data J nE
temp-Hne4t J N!//E
if 0 front J J N!//1
front J rearJtempE
else
I
rear-Hne4t J tempE front
rearJ rear-Hne4tE rear
S
S
void que 66 delq01
I
node UtempE
temp J frontE temp
if 0 front J J N!//1 front
cout GG O `ueue is empty O GG endlE
else
I rear
front J front-Hne4tE
cout GG temp-HdataE
delete tempE
S
S

DEDUEB
www.technicalsymposium.com
>
>
?
>
'
W
??
For More Notes and questions log on to www.technicalsymposium.com
+ single queue ehaves in a FIF# manner in the sense that each deletion removes the
oldest remaining item in the structure. + doule ended queue or deque, in short is a linear
list in which insertions and deletions are made to or from either end of the structure.
)eletion Insertion
Insertion )eletion
Front 2ear
Ce can have two variations of a deque, namely, the input-restricted deque and the output
@restricted deque. -he output-restricted deque allows deletion from only one end and
input-restricted deque allows insertions at only one end.
Dueue -##"icationsB
-he most useful application of queues is the simulation of a real world
situation so that it is possile to understand what happens in a real world in a particular
situation without actually oserving its occurrence.
`ueues are also very useful in a time-sharing computer system where many users share
the system simultaneously. Chenever a user requests the system to run a particular
program, the operating system adds the request at the end of the queue of Aos waiting to
e e4ecuted. Chenever the C(! is free, it e4ecutes the Ao, which is at the front of the
Ao queue. $imilarly there are queues for sharing IB# devices. 7ach device maintains its
own queue of request.
+nother useful application of queues is in the solution of
prolems involving searching a nonlinear collection of states. `ueue is used for finding a
path using readth-first-search of graphs.
.INKED .IST
DefinitionB
+ collection of node is called list. 7ach node or item in a linked list must
contain at least two fields, an information field or data field and the ne4t address field.
-he first, field contains the actual element on the list which may e a simple integer, a
character, a string or even a large record. -he second field, which is a pointer, contains
the address of the ne4t node in the list used to access the ne4t node. + node of a linked
list may e represented y the following figure.

/ist
0 74ternal pointer1
www.technicalsymposium.com
)ata or
Info
Ne4t
?'
For More Notes and questions log on to www.technicalsymposium.com
-he entire linked list is accessed from an e4ternal pointer /ist pointing to the first node in
the list. Ce can access the first node through the e4ternal pointer, the second node
through ne4t pointer of the first node, the third node through the ne4t pointer of the
second node till the end of the list.
-he ne4t address field of the last node contains a special value, known as the N!//
value. -his is not a valid address. -his only tells us that we have reached the end of the
list. Ce will draw linked lists as an ordered sequence of nodes with links eing
represented y arrows.
/ist
OPER-TIONS ON .INKED .IST
-here are five asic types of operations associated with the list data
astraction6
%. -o determine if the list is empty. 2eturns true
if the list contains no elements.
8. +dd new elements any in the list
5. -o check if a particular element is present in
the list.
>. -o delete a particular element from the list
placed anywhere in the list.
?. -o print all the elements of the list.
Ce will introduce some notations to e used in algorithms6
If p is a pointer to a node, then
node0p1 refers to the node pointed to y p
info0p1 refers to the data part of that node
ne4t0p1 refers to the address part of that node
info0ne4t0p11 refers to the data part of the ne4t node which node, which follows node0p1
in the list if ne4t0p1 is not null.
Ce can initiali;e the list y making the e4ternal pointer null.
/ist J null
+lso, we can check whether the list is empty y checking whether the e4ternal pointer is
null.
if list J null
then return0true1
www.technicalsymposium.com
> ? '
?*
For More Notes and questions log on to www.technicalsymposium.com
else return0false1
-his routine will return true if the list is empty, otherwise it will return false.
-o traverse or to print the elements of a linked list, we need to use a
temporary pointer, p known as a traversal pointer.
( J list
while list G H null do
egin
print0 info0p11
pJ ne4t0p1
end
Inserting into a .in9e, "ist
-o add a new node containing data value 4 in the eginning of the list we need
to follow the step6
i. -o get a new node which is not in use.
ii. -o set the data field of the new node to 4
iii. -o set the ne4t field of the new node to point to list
iv. -o set pointer list point to the new node.
-o do this we can write the following algorithm6
getnode0p1
info0p1 J 4
ne4t0p1 J list
list J p
Ce are assuming that the operation getnode0p1 otains an empty node and sets the
contents of a variale named p to the address of that node.
p

9etnode0p1
p

Info041 J p
p /ist
www.technicalsymposium.com
4
4 ? '
?W
For More Notes and questions log on to www.technicalsymposium.com
ne4t0p1 J list
/ist
p
list J p
Sam#"e #rograms B
Ex& No& 1 Stac9
ProgramB
FincludeGstdio.hH
FincludeGconio.hH
int topJ%E
int aK8=LE
void main01
I
int n,4,,c,i,tempE
clrscr01E
printf0V7nter the no of elementsMnV1E
scanf0VPdV,Qn1E

do
I
printf0V%.(!$3MnV1E
printf0V8.(#(MnV1E
printf0V5.)I$(/+DMnV1E
printf0V>.7"I-MnV1E
reakE printf0Venter your choiceMnV1E
scanf0VPdV,Q1E
switch01
I
case %6
printf0Venter the numerMnV1E
if0topHJnR%1
printf0VMnstack is overflowMnV1E
else
scanf0VPdV,Q41E
aKtopLJ4E
topJtopR%E
www.technicalsymposium.com
> ? '
?&
For More Notes and questions log on to www.technicalsymposium.com
reakE
case 86
if 0topGJ=1
printf0Vstack is underflowMnV1E
else
I
topJtop-%E
tempJaKtopLE
S
reakE
case 56
for0iJ%EiGtop-%EiRR1
printf0VPd--HV,aKiL1E
printf0VPdV,aKtop-%L1E
reakE
case >6
e4it0=1E
S
printf0VMndo you want to continue0%B=1MnV1E
scanf0VPdV,Qc1E
S
while0cJJ%1E
getch01E
S
Ex& No& 2 Dueue
ProgramB
FincludeGstdio.hH
FincludeGconio.hH
int n,4,,c,i,rJ=,fJ=,teE
int qK8=LE
void main01
I
clrscr01E
printf0V7nter the no of elementsMnV1E
scanf0VPdV,Qn1E
do
I
printf0V%.insertionMnV1E
printf0V8.deletionMnV1E
printf0V5.displayMnV1E
printf0V>.e4itMnV1E
www.technicalsymposium.com '=
For More Notes and questions log on to www.technicalsymposium.com
printf0Venter your choiceMnV1E
scanf0VPdV,Q1E
switch01
I
case %6
insert01E
display01E
reakE
case 86
delet01E
display01E
reakE
case 56
display01E
reakE
case >6
e4it0=1E
S
printf0VMndo you want to continue0%B=1MnV1E
scanf0VPdV,Qc1E
S
while0cJJ%1E
getch01E
S
insert01
I
if0rHJn1
printf0VMnqueue is overflowMnV1E
else
I
printf0Venter the numerMnV1E
scanf0VPdV,Q41E
rJrR%E
qKrLJ4E
S
if0fJJ=1
fJ%E
return0=1E
S
int delet01
I
int teE
if 0fJJ=1
www.technicalsymposium.com '%
For More Notes and questions log on to www.technicalsymposium.com
printf0Vqueue is underflowMnV1E
else if 0fJJr1
I
fJ=ErJ=E
S
else
I
teJqKfLE
fJfR%E
S
return0te1E
S
display01
I
if0rJJ=1
I
printf0V queue is emptyV1E
S
else
I
for0iJfEiGrEiRR1
printf0VPd--HV,qKiL1E
printf0VPdV,qKrL1E
S
return0=1E
S
Ex& NoB 3 Sing"y .in9e, "ist
ProgramB
FincludeGstdio.hH
FincludeGconio.hH
Fdefine null =
int a,sE
struct node
I
int dataE
struct node UlinkE
SE
struct node Uhead,Ufirst,Uprevious,UtempE
void main01
I
firstJnullE
headJnullE
www.technicalsymposium.com '8
For More Notes and questions log on to www.technicalsymposium.com
clrscr01E
do
I
printf0V%.creationMnV1E
printf0V8.displayMnV1E
printf0V5.insert firstMnV1E
printf0V>.insert lastMnV1E
printf0V?.insert middleMnV1E
printf0V'.delete firstMnV1E
printf0V*.delete lastMnV1E
printf0VW.delete middleMnV1E
printf0Venter your choiceV1E
scanf0VPdV,Qa1E
switch0a1
I
case %6
create01E
display01E
reakE
case 86
display01E
reakE
case 56
insfirst01E
display01E
reakE
case >6
inslast01E
display01E
reakE
case ?6
insmiddle01E
display01E
reakE
case '6
delfirst01E
display01E
reakE
case *6
dellast01E
display01E
reakE
case W6
delmiddle01E
www.technicalsymposium.com '5
For More Notes and questions log on to www.technicalsymposium.com
display01E
reakE
case &6
e4it0=1E
S
printf0VMndo you want to continue0%B=1MnV1E
scanf0VPdV,Qs1E
S
while0sJJ%1E
S
create01
I
int sE
sJsi;eof 0struct node1E
firstJ0struct nodeU1 malloc 0s1E
printf0Venter the dataV1E
scanf0VPdV,Qfirst-Hdata1E
first-HlinkJnullE
if0headJJnull1
headJfirstE
else
I
previousJheadE
while0previous-Hlink TJnull1
previousJprevious-HlinkE
S
previous-HlinkJfirstE
previousJfirstE
return0=1E
S
display01
I
if0headJJnull1
printf0Vnull firstV1E
else
tempJheadE
while0tempTJnull1
I
printf0VPd-HV,temp-Hdata1E
tempJtemp-HlinkE
S
printf0VnullMnV1E
return0=1E
S
www.technicalsymposium.com '>
For More Notes and questions log on to www.technicalsymposium.com
insfirst01
I
int sE
if 0headJJnull1
printf0Vlist is nullV1E
else
I
sJsi;eof 0temp1E
tempJ0struct nodeU1 malloc0s1E
printf0Venter dataMnV1E
scanf0VPdV,Qtemp-Hdata1E
temp-HlinkJheadE
headJtempE
S
return0=1E
S
delfirst01
I
int sE
if0headJJnull1
printf0Vlist is nullV1E
else
headJhead-HlinkE
return0=1E
S
inslast01
I
int sE
struct node Utemp,UlastE
if 0headJJnull1
printf0Vlist is nullV1E
else
I
sJsi;eof 0last1E
lastJ0struct nodeU1 malloc0s1E
printf0Venter the dataMnV1E
scanf0VPdV,Qlast-Hdata1E
last-HlinkJnullE
tempJheadE
while0temp-HlinkTJnull1
tempJtemp-HlinkE
temp-HlinkJlastE
S
www.technicalsymposium.com '?
For More Notes and questions log on to www.technicalsymposium.com
return0=1E
S
dellast01
I
int s,mE
struct node Upre,Une4tE
if0headJJnull1
printf0Vlist is nullV1E
else
I
ne4tJheadE
ne4tJhead-HlinkE
preJheadE
while0ne4t-HlinkTJnull1
I
ne4tJne4t-HlinkE
preJpre-HlinkE
S
pre-HlinkJne4t-HlinkE
S
return0=1E
S
insmiddle01
I
int s,f,countE
struct node Une4t,Upre,Une4E
if 0headJJnull1
printf0Vlist is nullV1E
else
I
sJsi;eof 0temp1E
tempJ0struct nodeU1 malloc0s1E
preJheadE
ne4tJpre-HlinkE
countJ8E
printf0Venter the position of the elementV1E
scanf0VPdV,Qf1E
printf0Venter the dataMnV1E
scanf0VPdV,Qne4-Hdata1E
while00countGf1 QQ 0ne4t-HlinkTJnull11
I
ne4tJne4t-HlinkE
preJpre-HlinkE
countJcountR%E
S
www.technicalsymposium.com ''
For More Notes and questions log on to www.technicalsymposium.com
if00countGf1 QQ 0ne4t-HlinkJJnull11
I
printf0Vnot possile to insert. the list is contains Pd elementsV,count1E
S
else
I
pre-HlinkJne4E
ne4-HlinkJne4tE
S
S
return0=1E
S
delmiddle01
I
int s,f,countE
struct node Une4t,Upre,Une4E
if 0headJJnull1
printf0Vlist is nullV1E
else
I
sJsi;eof 0temp1E
tempJ0struct nodeU1 malloc0s1E
preJheadE
ne4tJpre-HlinkE
countJ8E
printf0Venter the position of the elementV1E
scanf0VPdV,Qf1E
while00countGf1 QQ 0ne4t-HlinkTJnull11
I
ne4tJne4t-HlinkE
preJpre-HlinkE
countJcountR%E
S
if00countGf1 QQ 0ne4t-HlinkJJnull11
I
printf0Vnot possile to insert. the list is contains Pd elementsV,count1E
S
pre-HlinkJne4t-HlinkE
S
return0=1E
S
www.technicalsymposium.com '*
For More Notes and questions log on to www.technicalsymposium.com
Ex& No& 8 ,ou!"y "in9e, "ist
ProgramB
8& 1rite a C #rogram to im#"ement t+e Dou!"e .in9e, .ist&
FincludeGconio.hH
FincludeGstdio.hH
FincludeGstdli.hH
struct student
I
int rollnoE
struct student UprevE
struct student Une4tE
SE
typedef struct student listE
void add0list Uhead,int rollno1
I
list Unew[elt,UtempJheadE
new[eltJ0list U1malloc0si;eof0list11E
new[elt-HrollnoJrollnoE
new[elt-Hne4tJN!//E
while0temp-Hne4tTJN!//1
tempJtemp-Hne4tE
new[elt-HprevJtempE
temp-Hne4tJnew[eltE
S
void insert0list Uhead,int rollno,int position1
I
int iE
list Unew[elt,UadA[elt,UtempJheadE
new[eltJ0list U1malloc0si;eof0list11E
new[elt-HrollnoJrollnoE
for0iJ%EiGpositionEiRR1
tempJtemp-Hne4tE
adA[eltJtemp-Hne4tE
adA[elt-HprevJnew[eltE
www.technicalsymposium.com 'W
For More Notes and questions log on to www.technicalsymposium.com
new[elt-Hne4tJadA[eltE
new[elt-HprevJtempE
temp-Hne4tJnew[eltE
S
int find0list Uhead,int rollno1
I
list UtempJhead-Hne4tE
int foundJ=,iJ%EE
while0tempTJN!//1
I
if0temp-HrollnoJJrollno1
I
foundJiE
reakE
S
iRRE
tempJtemp-Hne4tE
S
return foundE
S
void remove7lt0list Uhead,int rollno1
I
list Udel[elt,Usuccessor,Upredecsor,UtempJhead-Hne4tE
int i,foundE
foundJfind0head,rollno1E
if0foundTJ=1
I
while0temp-HrollnoTJrollno1
tempJtemp-Hne4tE
del[eltJtempE
predecsorJdel[elt-HprevE
successorJdel[elt-Hne4tE
predecsor-Hne4tJdel[elt-Hne4tE
successor-HprevJdel[elt-HprevE
free0del[elt1E
printf0VMn#ne 7lement is deletedV1E
S
else
printf0VMn7lement has not FoundTCann^t perform )eletionTV1E
S
void print[list0list Uhead1
I
if0head-Hne4tTJN!//1
www.technicalsymposium.com '&
For More Notes and questions log on to www.technicalsymposium.com
I
list UtempJhead-Hne4tE
printf0VMn-he /ist6MnV1E
while0tempTJN!//1
I
printf0VPd--H V,temp-Hrollno1E
tempJtemp-Hne4tE
S
printf0VNullV1E
S
else
printf0VMn -he /ist is 7mptyV1E
S
void make[emptylist0list Uhead1
I
head-HprevJN!//E
head-Hne4tJN!//E
printf0VMn-he /ist has een deletedTV1E
S
void main01
I
list UheadE
int position,rollno,optionE
headJ0list U1malloc0si;eof0listU11E
head-HprevJN!//E
head-Hne4tJN!//E
clrscr01E
while0%1
I
printf0VMnMn%.+ddMn8.Insert a ItemMn5.2emove a ItemMn>.FindMn?.(rint the
/istMn'.)elete the /istMn*.74itV1E
printf0VMn7nter your Choice6V1E
scanf0VPdV,Qoption1E
switch0option1
I
case %6
printf0VMn7nter 2ollno of the New 7lement6V1E
scanf0VPdV,Qrollno1E
add0head,rollno1E
reakE
case 86
printf0VMn7nter 2ollno of 7lement to e Inserted6V1E
scanf0VPdV,Qrollno1E
printf0VMn7nter (osition to insert6V1E
www.technicalsymposium.com *=
For More Notes and questions log on to www.technicalsymposium.com
scanf0VPdV,Qposition1E
insert0head,rollno,position1E
reakE
case 56
printf0VMn7nter the 2ollno of the element to 2emoved6V1E
scanf0VPdV,Qrollno1E
remove7lt0head,rollno1E
reakE
case >6
printf0V7nter rollno of Item to e found6V1E
scanf0VPdV,Qrollno1E
positionJfind0head,rollno1E
if0positionTJ=1
printf0VMn7lement has een foundT(ositionJPdV,position1E
else
printf0VMn7lement has not found in the /istTV1E
reakE
case ?6
print[list0head1E
reakE
case '6
make[emptylist0head1E
reakE
case *6
e4it0=1E
S
S getch01E S
7x& NoB ; Circu"ar Sing"y .in9e, "ist
Program B
FincludeGstdio.hH
FincludeGconio.hH
int a,sE
struct node
I
int dataE
struct node UlinkE
SE
struct node Uhead,Ufirst,Uprevious,Ulast,UtempE
void main01
I
www.technicalsymposium.com *%
For More Notes and questions log on to www.technicalsymposium.com
firstJN!//E
headJN!//E
previousJN!//E
clrscr01E
do
I
printf0V%.creationMnV1E
printf0V8.displayMnV1E
printf0V5.insert firstMnV1E
printf0V>.insert lastMnV1E
printf0V?.insert middleMnV1E
printf0V'.delete firstMnV1E
printf0V*.delete lastMnV1E
printf0VW.delete middleMnV1E
printf0Venter your choiceV1E
scanf0VPdV,Qa1E
switch0a1
I
case %6
create01E
display01E
reakE
case 86
display01E
reakE

case 56
insfirst01E
display01E
reakE
case >6
inslast01E
display01E
reakE
case ?6
insmiddle01E
display01E
reakE
case '6
delfirst01E
display01E
reakE
case *6
dellast01E
www.technicalsymposium.com *8
For More Notes and questions log on to www.technicalsymposium.com
display01E
reakE
case W6
delmiddle01E
display01E
reakE
case &6
e4it0=1E
S
printf0VMndo you want to continue0%B=1MnV1E
scanf0VPdV,Qs1E
S
while0sJJ%1E
S
create01
I
int sE
sJsi;eof 0struct node1E
firstJ0struct nodeU1 malloc 0s1E
printf0Venter the dataV1E
scanf0VPdV,Qfirst-Hdata1E
first-HlinkJfirstE
if0headJJN!//1
I
headJfirstE
previousJfirstE
S
else
I
previousJheadE
while0previous-Hlink TJhead1
previousJprevious-HlinkE
previous-HlinkJfirstE
previousJfirstE
S
lastJfirstE
return0=1E
S
display01
I
if0headJJN!//1
printf0Vlist is nullV1E
else
I
www.technicalsymposium.com *5
For More Notes and questions log on to www.technicalsymposium.com
tempJheadE
while0tempTJlast1
I
printf0VPd-HV,temp-Hdata1E
tempJtemp-HlinkE
S
if0tempJJlast1
I
printf0VPd -HV,temp-Hdata1E
tempJtemp-HlinkE
S

S
return0=1E
S
insfirst01
I
int sE
if 0headJJN!//1
printf0Vlist is nullV1E
else
I
sJsi;eof 0temp1E
tempJ0struct nodeU1 malloc0s1E
printf0Venter dataMnV1E
scanf0VPdV,Qtemp-Hdata1E
temp-HlinkJheadE
headJtempE
firstJtempE
last-HlinkJtempE
S
return0=1E
S
delfirst01
I
int sE
if0headJJN!//1
printf0Vlist is nullV1E
else
headJhead-HlinkE
last-HlinkJheadE
if0last-HlinkJJhead1
headJN!//E
www.technicalsymposium.com *>
For More Notes and questions log on to www.technicalsymposium.com
return0=1E
S
inslast01
I
int sE
struct node Ulast%,Ufirst%E
if 0headJJN!//1
printf0Vlist is nullV1E
else
I
sJsi;eof 0last%1E
last%J0struct nodeU1 malloc0s1E
printf0Venter the dataMnV1E
scanf0VPdV,Qlast%-Hdata1E
last%-HlinkJN!//E
tempJheadE
while0temp-HlinkTJhead1
tempJtemp-HlinkE
temp-HlinkJlast%E
last%-HlinkJheadE
lastJlast%E
S
return0=1E
S
dellast01
I
int s,mE
struct node Upre,Une4tE
if0headJJN!//1
printf0Vlist is nullV1E
else
I
if0headJJlast1
headJlastJN!//E
else
I
ne4tJheadE
while0ne4t-HlinkTJlast1
ne4tJne4t-HlinkE
ne4t-HlinkJheadE
lastJne4tE
S
www.technicalsymposium.com *?
For More Notes and questions log on to www.technicalsymposium.com
S
return0=1E
S
insmiddle01
I
int s,f,countE
struct node Une4t,Upre,Une4E
if 0headJJN!//1
printf0Vlist is nullV1E
else
I
sJsi;eof 0temp1E
tempJ0struct nodeU1 malloc0s1E
preJheadE
ne4tJpre-HlinkE
countJ8E
printf0Venter the position of the elemantV1E
scanf0VPdV,Qf1E
printf0Venter the dataMnV1E
scanf0VPdV,Qne4-Hdata1E
while00countGf1 QQ 0ne4t-HlinkTJhead11
I
ne4tJne4t-HlinkE
preJpre-HlinkE
countJcountR%E
S
if00countGf1 QQ 0ne4t-HlinkJJhead11
I
printf0Vnot possile to insert. the list is contains Pd elementsV,count1E
S
else
I
pre-HlinkJne4E
ne4-HlinkJne4tE
S
S
return0=1E
S
delmiddle01
I
int s,f,countE
struct node Une4t,Upre,Une4E
if 0headJJN!//1
printf0Vlist is nullV1E
else
www.technicalsymposium.com *'
For More Notes and questions log on to www.technicalsymposium.com
I
sJsi;eof 0temp1E
tempJ0struct nodeU1 malloc0s1E
preJheadE
ne4tJpre-HlinkE
countJ8E
printf0Venter the position of the elemantV1E
scanf0VPdV,Qf1E
while00countGf1 QQ 0ne4t-HlinkTJhead11
I
ne4tJne4t-HlinkE
preJpre-HlinkE
countJcountR%E
S
if00countGf1 QQ 0ne4t-HlinkJJhead11
I
printf0Vnot possile to insert. the list is contains Pd elementsV,count1E
S
pre-HlinkJne4t-HlinkE
S
return0=1E
S
Ex& NoB < O#erations On Einary Trees
Program B
FincludeGstdio.hH
FincludeGmalloc.hH
struct node
I
int dataE
struct node UlchildE
struct node UrchildE
S
void creation01
I
printf0VMn 7nter the value of the root node MnV1E
tJ0struct nodeU1malloc0si;eof0struct node11E
www.technicalsymposium.com **
For More Notes and questions log on to www.technicalsymposium.com
scanf0VPdV,Qt-Hdata1E
t-HlchildJN!//E
t-HrchildJN!//E
S
void insert0int n,struct node Ua1
I
struct node U4E
if0a-Hdata TJ=1
I
if0a-HdataHn1
insert0n,a-Hlchild1E
else
insert0n,a-Hrchild1E
S
4J0struct nodeU1 malloc0si;eof0struct node11E
4-HdataJnE
if0a-HdataHn1
I
if00a-Hlchild1JJN!//1
I
a-HlchildJ4E
a-Hlchild-HlchildJN!//E
a-Hlchild-HrchildJN!//E
S
S

else
if0a-HrchildJJN!//1
I
a-HrchildJ4E
a-Hrchild-HlchildJN!//E
a-Hrchild-HrchildJN!//E
S
S
void inorder0struct nodeU a1
I
if0aTJN!//1
I
inorder0a-Hlchild1E
printf0V P%=d V,a-Hdata1E
inorder0a-Hrchild1E
S
S
www.technicalsymposium.com *W
For More Notes and questions log on to www.technicalsymposium.com
void preorder0struct node Ua1
I
if0aTJN!//1
I
printf0V P%=dV,a-Hdata1E
preorder0a-Hlchild1E
preorder0a-Hrchild1E
S
S
void postorder0struct node Ua1
I
if0aTJN!//1
I
postorder0a-Hlchild1E
postorder0a-Hrchild1E
printf0V P%=dV,a-Hdata1E
S
S
void main01
I
int num,cE
clrscr01E
creation01E
doI
printf0VMn %. Insertion Mn8. Inorder Mn5. preorder Mn>.postorder V1E
printf0VMn for e4it give choice greater than fourV1E
printf0V Mn7nter your choice V1E
scanf0VPdV,Qc1E
switch0c1
I
case %6printf0V Mn )ata to e inserted V1E
scanf0VPdV,Qnum1E
insert0num,t1E
reakE
case 86 inorder0t1E
reakE
case 56 preorder0t1E
reakE
case >6 postorder0t1E
reakE
S
Swhile0cG?1E
S
www.technicalsymposium.com *&
For More Notes and questions log on to www.technicalsymposium.com
Ex& No& = !inary tree o#eration
ProgramB
*. Crite a C program to implement the inary tree operations @ insert, delete, and search.
FincludeGconio.hH
FincludeGstdio.hH
FincludeGstdli.hH
struct stree
I
int dataE
struct stree UleftE
struct stree UrightE
SE
struct stree Uroot,UtempE
struct streeU insertst0struct stree Uroot,int 41
I
if0 root JJ N!//1
I
rootJmalloc0si;eof0struct stree11E
root-HdataJ4E
root-HleftJroot-HrightJN!//E
S
else if04 G root-Hdata1
root-HleftJinsertst0root-Hleft,41E
else if04 H root-Hdata1
root-HrightJinsertst0root-Hright,41E
return rootE
S
struct streeU findMin0struct stree Ut1
I
if0tJJN!//1
return N!//E
else
if0t-Hleft TJ N!//1
return findMin0t-Hleft1E
else
return tE
S
struct streeU findMa40struct stree Ut1
I
www.technicalsymposium.com W=
For More Notes and questions log on to www.technicalsymposium.com
if0tJJN!//1
return N!//E
else
if0t-Hright TJ N!//1
return findMa40t-Hright1E
else
return tE
S
int find0struct stree Utemp,int key1
I
if 0tempJJN!//1
I
return N!//E
S
else
I
if0key G temp-Hdata1
return find0temp-Hleft,key1E
else if 0key H temp-Hdata1
return find0temp-Hright,key1E
else
return tempE
S
S
int find[min0struct stree Utemp1
I
if0temp-Hleft JJ N!//1
return temp-HdataE
else
return find[min0temp-Hleft1E
S
int find[ma40struct stree Utemp1
I
if0temp-Hright JJ N!//1
return temp-HdataE
else
return find[ma40temp-Hright1E
S
struct streeU remove7lt0struct stree Ut,int key1
I
struct stree UtempE
www.technicalsymposium.com W%
For More Notes and questions log on to www.technicalsymposium.com
if 0tJJN!//1
printf0VMnNode is not availaleV1E
else if0key G t-Hdata1
t-HleftJremove7lt0t-Hleft,key1E
else if0key H t-Hdata1
t-HrightJremove7lt0t-Hright,key1E
else if0t-Hleft TJ N!// QQ t-Hright TJ N!//1
I
tempJfindMin0t-Hright1E
t-HdataJtemp-HdataE
t-HrightJremove7lt0t-Hright,t-Hdata1E
S
else
I
tempJtE
if0t-HleftJJN!//1
tJt-HrightE
else if0t-HrightJJN!//1
tJt-HleftE
free0temp1E
S
return tE
S
void print[list0struct stree Uroot1
I
if0rootTJN!//1
I
if0root-Hleft TJ N!//1
print[list0root-Hleft1E
printf0V PdV,root-Hdata1E
if0root-Hright TJ N!//1
print[list0root-Hright1E
S
S
void main01
I
struct stree Unew[eltE
int option,rollno,infoE
rootJN!//E
clrscr01E
www.technicalsymposium.com W8
For More Notes and questions log on to www.technicalsymposium.com
while0%1
I
printf0VMnMn%.Insert a ItemMn8.2emove a ItemMn5.FindMn>.Find Minimum
:alueMn?.Find Ma4imum :alueMn'.(rint the /istMn*.74itV1E
printf0VMn7nter your Choice6V1E
scanf0VPdV,Qoption1E
switch0option1
I
case %6
printf0VMn7nter 2ollno of 7lement to e Inserted6V1E
scanf0VPdV,Qrollno1E
rootJinsertst0root,rollno1E
reakE
case 86
printf0VMn7nter the 2ollno of the element to 2emoved6V1E
scanf0VPdV,Qrollno1E
rootJremove7lt0root,rollno1E
reakE
case 56
printf0V7nter rollno of Item to e found6V1E
scanf0VPdV,Qrollno1E
infoJfind0root,rollno1E
if0infoTJ=1
printf0VMn7lement has een foundT at (ositionJPdV,info1E
if0info JJ =1
printf0VMn7lement has not found in the /istTV1E
reakE
case >6
if0root JJ N!//1
printf0VMn-ree is emptyV1E
else
I
infoJfind[min0root1E
printf0VMn -he Minimum :alue is6PdV,info1E
S
reakE
case ?6
if0root JJ N!//1
printf0VMn-ree is emptyV1E
else
I
infoJfind[ma40root1E
printf0VMn -he Ma4imum :alue is6PdV,info1E
S
www.technicalsymposium.com W5
For More Notes and questions log on to www.technicalsymposium.com
reakE
case '6
if0root JJ N!//1
printf0VMn-ree is emptyV1E
else
print[list0root1E
reakE
case *6
e4it0=1E
S
S
getch01E
S
Ex& No& > Duic9 Sort
ProgramB
Finclude Gstdio.hH
FincludeGconio.hH
int n,aK5=L,passJ%E
void quicksort0int low, int high1
I
if0lowGhigh1
I
int k,iE
kJpartition0low,high1E
printf0Vpass PdMnV,passRR1E
for0iJ%EiGJnEiRR1
printf0VP'dV,aKiL1E
printf0VMnV1E
quicksort0low,k-%1E
quicksort0kR%,high1E
S
S
int partition0int low,int high1
I
int v,i,tE
vJaKlowLE
iJlowE
do
I
while0aKiLGJv1
iRRE
while0aKhighLHv1
high--E
www.technicalsymposium.com W>
For More Notes and questions log on to www.technicalsymposium.com
if0iGhigh1
I
tJaKiLE
aKiLJaKhighLE
aKhighLJtE
S
S
while0iGhigh1E
aKlowLJaKhighLE
aKhighLJvE
return highE
S
void main01
I
int low,high,iE
clrscr01E
printf0V 7nter the array si;e MnV1E
scanf0VPdV,Qn1E
printf0V7nter the elements MnV1E
for0iJ%EiGJnEiRR1
scanf0VPdV,QaKiL1E
lowJ%E
highJnE
quicksort0low,high1E
getch01E
S
Ex& No& ? Hea# Sort
ProgramB
FincludeGconio.hH
FincludeGstdio.hH
int 4K8=L,n,n%E
void heapsort01E
void adAust01E
void heapify01E
void main01
I
int iE
clrscr01E
printf0V7N-72 -37 N#. #F 7/7M7N-$ 6V1E
scanf0VPdV,Qn1E
n%JnE
www.technicalsymposium.com W?
For More Notes and questions log on to www.technicalsymposium.com
printf0O7N-72 -37 7/7M7N-$MnN1E
for0iJ%EiGJnEiRR1
scanf0VPdV,Q4KiL1E
clrscr01E
printf0V Mn +22+D .7F#27 $#2- 6V1E
for0iJ%EiGJnEiRR1
printf0VPdMtV,4KiL1E
heapsort01E
getch01E
S
void heapsort01
I
int i,t,AE
heapify01E
for0iJnEiHJ8Ei--1
I
tJ4KiLE
4KiLJ4K%LE
4K%LJtE
adAust0%,i-%1E
printf0VMnpass Pd MnV,n-0i-%11E
for0AJ%EAGJn%EARR1
printf0V PdMtV,4KAL1E
S
S
void adAust0int i,int n1
I
int AE
int tE
AJ8UiE
tJ4KiLE
while0AGJn1
I
if0AGn QQ 4KAL G 4KAR%L1
ARRE
if0tHJ4KAL1
reakE
4KAB8LJ4KALE
AJ8UAE
S
4KAB8LJtE
www.technicalsymposium.com W'
For More Notes and questions log on to www.technicalsymposium.com
S
void heapify01
I
int i,AE
for0iJnB8EiHJ%Ei--1
adAust0i,n1E
S
Ex& No& 1@ De#t+ first searc+
ProgramB
%=. Crite a C program to implement the )epth First $earch.
FincludeGconio.hH
FincludeGstdio.hH
int graphK%=LK%=LE
int visitedK%=LE
void Intili;e[9raph0int no[v1
I
int i,AE
for0iJ%EiGJno[vEiRR1 BBIntiali;e the 9raph
I
visitedKiLJ=E
for0AJ%EAGJno[vEARR1
graphKiLKALJ=E
S
S
void print[graph0int no[v1
I
int i,AE
printf0VMn-he 9raph0Matri4 2epresentation16MnV1E
for0iJ%EiGJno[vEiRR1
printf0VMt:PdV,i1E
for0iJ%EiGJno[vEiRR1
I
printf0VMn:PdV,i1E
for0AJ%EAGJno[vEARR1
I
printf0VMtPdV,graphKiLKAL1E
S
S
S
www.technicalsymposium.com W*
For More Notes and questions log on to www.technicalsymposium.com
void )F$0int start[v,int no[v1
I
int iE
visitedKstart[vLJ%E
printf0VPd--HV,start[v1E
for0iJ%EiGJno[vEiRR1
I
if0iTJstart[v QQ visitedKiLJJ= QQ graphKstart[vLKiLJJ%1
I
)F$0i,no[v1E
S
S
S
void main01
I
int no[v,no[eE
int s[v,e[v,start[vE
int i,AE
clrscr01E
printf0VMn7nter the No of :ertices in the 9raph6V1E
scanf0VPdV,Qno[v1E
printf0V7nter the no of 7dges in the 9raph6V1E
scanf0VPdV,Qno[e1E
printf0VMn7nter the +dAacent vertices of each edgeV1E
for0iJ%EiGJno[eEiRR1
I
printf0VMn7dge6Pd--H$tart[: Q 7nd[:6V,i1E
scanf0VPd PdV,Qs[v,Qe[v1E
graphKs[vLKe[vLJ%E
graphKe[vLKs[vLJ%E
S
print[graph0no[v1E
printf0VMn)epth First $earch6Mn7nter the $tarting :erte4 for $earching6V1E
scanf0VPdV,Qstart[v1E
printf0V)epth First $earch -ree6MnV1E
)F$0start[v,no[v1E
getch01E
S
www.technicalsymposium.com WW
For More Notes and questions log on to www.technicalsymposium.com
Ex& No& 11 s+ortest #at+ of t+e gra#+
ProgramB
%%. Crite a C program to find the shortest path of a graph.
FincludeGconio.hH
FincludeGstdio.hH
int start[:K8=L,end[:K8=L,weightK8=LE
int no[v,no[eE
struct :erte4Info
I
int knownE
int distanceE
int prev[verte4E
Sverte4K8=LE
void Intili;e[:erte4Info01
I
int i,AE
for0iJ%EiGJno[vEiRR1 BBIntiali;e the 9raph
I
verte4KiL.knownJ=E
verte4KiL.distanceJ%===E
verte4KiL.prev[verte4J=E
S
S
void print[(ath0int e[v1
I
int tempJe[vE
if0verte4Ke[vL.prev[verte4JJ=1
printf0VMnNo path from $ource to 9iven )estinationTV1E
else
I
printf0VMn$hortest (ath6Pd V,e[v1E
while0verte4Ke[vL.prev[verte4TJ=1
I
printf0VPd V,verte4Ke[vL.prev[verte41E
e[vJverte4Ke[vL.prev[verte4E
S
printf0VMn-he )istance is6PdV,verte4KtempL.distance1E
S
S
www.technicalsymposium.com W&
For More Notes and questions log on to www.technicalsymposium.com
void main01
I
int s[v,e[v,start[vE
int i,AE
int v,w,inde4E
int min,qw,ne4t[vE
char chE
clrscr01E
printf0VMn7nter the No of :ertices in the 9raph6V1E
scanf0VPdV,Qno[v1E
printf0V7nter the no of 7dges in the 9raph6V1E
scanf0VPdV,Qno[e1E
printf0VMn7nter the +dAacent vertices of each edgeV1E
for0iJ%EiGJno[eEiRR1
I
printf0VMn7dge6Pd--H$tart[: Q 7nd[: Q Ceight6V,i1E
scanf0VPd Pd PdV,Qstart[:KiL,Qend[:KiL,QweightKiL1E
S
Intili;e[:erte4Info01E
printf0VMn7nter :ertices to find the $hortest (ath6V1E
scanf0VPd PdV,Qs[v,Qe[v1E
verte4Ks[vL.distanceJ=E
ne4t[vJs[vE
while0ne4t[v1
I
vJne4t[vE
verte4KvL.knownJ%E
for0iJ%EiGJno[eEiRR1
I
if0start[:KiLJJv1
I
wJend[:KiLE
if0verte4KvL.distanceRweightKiL G verte4KwL.distance1
I
verte4KwL.distanceJverte4KvL.distanceRweightKiLE
verte4KwL.prev[verte4JvE
S
S
S
www.technicalsymposium.com &=
For More Notes and questions log on to www.technicalsymposium.com
minJ%===E
for0AJ%EAGJno[vEARR1
I
if0verte4KAL.knownJJ= QQ minHverte4KAL.distance1
I
minJverte4KAL.distanceE
ne4t[vJAE
S
S
if0minJJ%===1
ne4t[vJ=E
S
print[(ath0e[v1E
while0%1
I
printf0VMn)o you want shortest to one more destination6V1E
fflush0stdin1E
chJgetchar01E
if0chJJ^y^ X chJJ^D^1
I
printf0VMn7nter the )estination :erte46V1E
scanf0VPdV,Qe[v1E
print[(ath0e[v1E
S
else
reakE
S
getch01E
S

K&.&N CO..E*E O) EN*INEERIN*' POTT-P-.-J-/
DEP-RT/ENT O) CO/PUTER -PP.IC-TIONS %/C-(
D-T- STRUCTURES .-E%/C1<@<(
I SE/ESTER C %Lune 2@@< C No6em!er 2@@<(
D-T- STRUCTURES .-E CJC.E PRO*R-/S
%. 2epresent the given sparse matri4 using one-dimensional array and linked list.
8. Create a $tack and do the following operations using arrays and linked lists
0i1(ush 0ii1 (op 0iii1 (eep
www.technicalsymposium.com &%
For More Notes and questions log on to www.technicalsymposium.com
5. Create a `ueue and do the following operations using arrays and linked lists
0i1+dd 0ii1 2emove
>. Implement the operations on singly linked list, douly linked list and circular
linked list.
?. Create a inary search tree and do the following traversals
0i1In-order 0ii1 (re order 0iii1 (ost order
'. Implement the following operations on a inary search tree.
0i1 Insert a node 0ii1 )elete a node
*. $ort the given list of numers using heap and quick sort.
W. (erform the following operations in a given graph
0i1 )epth first search 0ii1 .readth first search
&. Find the shortest path in a given graph using )iAkstra algorithm

K&.&N CO..E*E O) EN*INEERIN*' POTT-P-.-J-/
DEP-RT/ENT O) CO/PUTER -PP.IC-TIONS %/C-(
PRO*R-//IN* .-E %/C1<@=(
I SE/ESTER C %Lune 2@@< C No6em!er 2@@<(
C .-E CJC.E PRO*R-/S
%. )isplay the following6
0i1 FloydZs triangle 0ii1 (ascal -riangle
8. 9enerate the following series of numers6
www.technicalsymposium.com &8
For More Notes and questions log on to www.technicalsymposium.com
+rmstrong numers etween % to %==
(rime numers etween % to ?=
Fionacci series up to N numers
5. Manipulate the strings with following operations.
0i1 Concatenating two strings 0ii1 2eversing the string 0iii1 Finding the su string
0iv1 2eplacing a string 0v1 Finding length of the string
>. Find the summation of the following series6
0i1 $ine 0ii1 Cosine 0iii1 74ponential
?. Create the sales report for M sales person and N products using two-dimensional
array.
'. $imulate following .anking operations using functions.
0i1 )eposit 0ii1 Cithdrawal 0iii1 .alance 7nquiry
*. Implement using recursion
I, Find the solution of -owers of 3anoi prolem using recursion.
II, Fionacci numer generation.
III, Factorial
W. 9enerate $tudent mark sheets using structures.
&. Create a collection of ooks using arrays of structures and do the following6
0i1 $earch a ook with title and author name 0ii1 $orts the ooks on title.
www.technicalsymposium.com &5

You might also like