You are on page 1of 275

C & DATA STRUCTURE LAB PROGRAMS

Objective : 1
To find the sum of individual digits of a given number
Description:
Sum of the individual digits means adding all the digits of a number
Ex: 123 sum of digits is 1+2+3=6
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize the s=
Step !: if n" goto Step #
Step $: if n%= goto Step 6 else goto step #
Step 6: store n&1 value in p
'dd p value to s
'ssign n(1 value to n
)oto Step $
Step #: print the output
Step *:stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:

T+,E -'.SE


T+,E

VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2
ST'+T
S=
+E'/ 0
1- 0"
2,T3,
T
451.E

0% =
ST23
3=0&1
S=S+3
0=0(1
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
main:;
<
int n=s=p>
7lrs7r:;>
printf:?enter the vaue for n:@n?;>
s7anf:?&d?=An;>
s=>
if:n";
printf:?The given number is not valid?;>
else
<
Bhile:n%=; (C 7he7D the given value = or not C(
<
p=n&1>
n=n(1>
s=s+p>
E
printf:?sum of individual digits is &d?=s;>
E
get7h:;>
E
Output:
18Enter the value for n: 333
Sum of individual digits is F
28Enter the value for n: !#33
Sum of individual digits is 1#
38 Enter the value for n: G111
The given number is not valid
Conclusion : The program is error free
VIVA Q!"A#IO$":
1; 4hat is the mean of sum of the individual digitsH
'ns: Sum of the individual digits means adding ea7h digit in a number

2; 4hat is positive integerH
'ns: if the integer value is grater than zero then it is 7alled positive integer
3; /efine prepro7essor H
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 3
C & DATA STRUCTURE LAB PROGRAMS
'ns: Iefore 7ompiling a pro7ess 7alled prepro7essing is done on the sour7e 7ode bJ a
program 7alled the prepro7essor8
Objective:%
To print the -ibona77i series for 1 to n value
Description
' fibona77i series is defined as folloBs
The first term in the seKuen7e is
The se7ond term in the seKuen7e is 1
The sub seKuent terms 1 found bJ adding the pre7eding tBo terms in the seKuen7e
-ormula: let t1=t2=LLLLtn be terms in fibina77i seKuen7e
t1== t2=1
tn=tnG2+tnG1LLBhere n92
algorithm:
Step 1: start
Step 2: initialize the a== b=1
Step 3: read n
Step !: if n== 1 print a go to step #8 else goto step $
Step $: if n== 2 print a= b go to step # else print a=b
Step 6: initialize i=3
i; if i"= n do as folloBs8 1f not goto step #
7=a+b
print 7
a=b
b=7
in7rement 1 value
goto step 6:i;
Step #: stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $
ST'+T
'==b=1
+ead n
1f n
==1
2utput a
1f n
==2
2utput a=b
2utput a=b
1=2 i++
1"n
M = a + b
2utput 7
' = b
I = 7
Stop
True -alse
-alse

True
-alse
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
void main:;
<
int a=b=7=n=i>
7lrs7r:;>
printf:?enter n value?;>
s7anf:?&d?=An;>
a=>
b=1>
if:n==1;
printf:?&d?=a;>
else
if:n==2;
printf:?&d&d?=a=b;>
else
<
printf:?&d&d?=a=b;>
((.223 41.. +,0 -2+ 2 T1NE .ESS 10 SE+1ES 'S T5ESE 4'S 3+10TE/
10 '/O'0ME
for:i=2>i"n>i++;
<
7=a+b>
printf:?&d?=7;>
a=b>
b=7>
E
get7h:;>
E
E
Output:
18 Enter n value : $
1 1 2 3
28 Enter n value : #
1 1 2 3 $ *
38 Enter n value : G6
1
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 6
C & DATA STRUCTURE LAB PROGRAMS
Conclusion : The program is error free
VIVA Q!"A#IO$":
1& 'hat is Fibonacci series (
Ans: ' fibona77i series is defined as folloBs
The first term in the seKuen7e is
The se7ond term in the seKuen7e is 1
The sub seKuent terms 1 found bJ adding the pre7eding tBo terms in the seKuen7e
-ormula: let t1=t2=LLLLtn be terms in fibina77i seKuen7e
t1== t2=1
tn=tnG2+tnG1LLBhere n92
2; 4hat are the various tJpes of un7onditional statementsH
'ns: goto=IreaD and 7ontinue
3;4hat are the various tJpes of 7onditional statementsH
'ns: if = if else =sBit7h statements
!; Expand "ST/1285 9H
'ns: standard input output header file
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #
C & DATA STRUCTURE LAB PROGRAMS
Objective :)
To print a prime numbers up to 1 to n
Description:
3rime number is a number Bhi7h is exa7tlJ divisible bJ one and itself onlJ
Ex: 2= 3=$=#=LLL>
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i=1=7=
Step !:if i"=n goto step $
1f not goto step 1
Step $: initialize P=1
Step 6: if P"=1 do as the folloB8 1f no goto step #
i;if i&P== in7rement 7
ii; in7rement P
iii; goto Step 6
Step #: if 7== 2 print i
Step *: in7rement i
Step F: goto step !
Step 1: stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *
C & DATA STRUCTURE LAB PROGRAMS
Flow chart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F
C & DATA STRUCTURE LAB PROGRAMS
Program:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1
Start
+ead n
1 = 1
1"=n

1++
Q = 1 Q++

Q"=i
1f
fa7t==2
1f 1 & P ==

-a7t ++
true
false
true
false
2utput
fa7t
false
stop
false
C & DATA STRUCTURE LAB PROGRAMS
6in7lude"stdio8h9
6in7lude"7onio8h9
void main:;
<
int n=i=fa7t=P>
7lrs7r:;>
printf:?enter the number:?;>
s7anf:?&d?=An;>
for:i=1>i"=n>i++;
<
fa7t=>
((T51S .223 41.. M5EMR ' 02 T2 IE 3+1NE 028 2+ 02T8
for:P=1>P"=i>P++;
<
if:i&P==;
fa7t++>
E
if:fa7t==2;
printf:?@n &d?=i;>
E
get7h: ;>
E
Output:
Enter the number : $
2 3 $
Enter the number : 1
2 3 $ #
Enter the number : 12
2 3 $ # 11
Conclusion : The program is error free
VIVA Q!"A#IO$":
1& 'hat is prime number (
Ans: 3rime number is a number Bhi7h is exa7tlJ divisible bJ one and itself onlJ
2;4hat is an algorithmH
'ns : ' step bJ step pro7edure is 7alled algorithm
3;4hat is floB 7hartH
'ns: ' pi7torial representation an algorithm is 7alled a floB 7hart
!;4hat is programH
'ns : ' 7olle7tion of statements is 7alled
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 11
C & DATA STRUCTURE LAB PROGRAMS
Objective:*
To find the roots of the Kuadrati7 eKuation
Description:
0ature of roots of Kuadrati7 eKuation 7an be DnoBn from the Kuadrant = b
2
G!a7
1f b
2
G!a7 9 then roots are real and uneKual
1f b
2
G!a7 = then roots are real and eKual
1f b
2
G!a7 " then roots are imaginarJ
Algorithm:
Step 1: start
Step 2: read the a=b=7 value
Step 3: if bCbG!a79 then
+oot 1= :Gb+ poB::bCbG!CaC7;=8$;;(2Ca
+oot 2= :GbGpoB::bCbG!CaC7;=8$;;(2Ca
Step !: if bCbG!a7= then
+oot1 = +oot2 = Gb(:2Ca;
Step $: 2therBise 3rint 1maginarJ roots8 )oto step #8
Step 6: print roots
Step #: stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 12
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 13
Start
+ead a=b=7
/ = poB:bCbG!CaC7;=8$
1f d 9
+1 = ::Gb+/; ( :2Ca;;
+2 = ::GbG/; (:2Ca;;
2utput
+1= +2
Stop
1f d==
+1=Gb ( :2 C a ;
+2= Gb ( :2 C a;
1maginarJ
roots
true
false
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"math8h9
void main:;
<
float a=b=7=r1=r2=d>
7lrs7r:;>
printf:?Enter the values for eKuation:?;>
s7anf:?&f&f&f?=Aa=Ab=A7;>
(C 7he7D the 7ondition C(
if:a==;
printf:?Enter value should not be zero ?;>
else
<
d=bCbG!CaC7>
(C 7he7D the 7ondition C(
if:d9;
<
r1=:Gb+sKrt:d;(:2Ca;;>
r2=:GbGsKrt:d;(:2Ca;;>
printf:?roots are real and uneKual@n?;>
printf:?&f@n&f@n?=r1=r2;>
E
else
if:d==;
<
r1=Gb(:2Ca;>
r2=Gb(:2Ca;>
printf:?roots are real and eKual@n?;>
printf:?root=&f@n?=r1;>
printf:?root=&f@n?=r2;>
E
else
printf:?roots are imaginarJ?;>
E
get7h:;>
E
Output:
18 Enter the values for eKuation: 1= 6= F
+oots are real and eKual
+oot= G38
+oot= G38
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!
C & DATA STRUCTURE LAB PROGRAMS
28 Enter the values for eKuation: 2= #= 6
+oots are real and uneKual
+oot= G68#$
+oot= G#82$
38 Enter the values for eKuation: 1= 2= 3
+oots are imaginarJ
Conclusion: The program is error free
VIVA Q!"A#IO$":
1& 4hat are various tJpes of loop statementsH
'ns : 4hile= doG Bhile= for loop statements
$; 4hat is the differen7e betBeen Bhile and doGBhile statementsH
'ns: 1n Bhile the 7ondition Bill be 7he7Ded first and then enter into a loop8
Iut in doG Bhile the statements Bill be exe7uted first and then finallJ 7he7D the
Mondition8
3; 5oB to find the roots of Kudratri7 eKutations H
'ns: 0ature of roots of Kuadrati7 eKuation 7an be DnoBn from the Kuadrant
= b
2
G!a7
1f b
2
G!a7 9 then roots are real and uneKual
1f b
2
G!a7 = then roots are real and eKual
1f b
2
G!a7 " then roots are imaginarJ
!; .ist out the M features H
'ns: 3ortabilitJ=flexibilitJ= Bide a77eptabilitJ et788=
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$
C & DATA STRUCTURE LAB PROGRAMS
Objective:+
To 7al7ulate the sum8 Sum=1Gx
2
(2%+ x
!
(!%G x
6
(6%+ x
*
(*%G x
1
(1%
Algorithm: main program:
Step 1: start
Step 2: de7lare x=i=n=s==7
Step 3: read x value
Step !: for i= = n=> i"=1> i=i+2= n++ goto step $
Step $: s=s+:poB:G1=n;CpoB:x=i;(fa7t:i;;
Step 6: print s value
Step #: stop
"ub program:
Step 1: Bhile x%= goto Step 2
Step 2: J=J+x> xS
Step 3: return J
Step !: return to main program
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 16
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#
C & DATA STRUCTURE LAB PROGRAMS
Program:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*
Start
+ead x=1=n=7
S =
1==n= i"=1
1 = 1 + 2= n++
S=s+:poB:G1=n;CpoB:x=i; ( fa7t:i;;
3rint s
Stop
Sub 3rogram
4hile x%
=
T=JCx
xGG
+eturn J
-a7t :;
+eturn to main
program
C & DATA STRUCTURE LAB PROGRAMS
6in7lude"stdio8h9
6in7lude"math8h9
long fa7t:int;>
void main:;
<
int x=i=n>
float s==7>
7lrs7r:;>
printf:?@n enter the value of x@t?;>
s7anf:?&d?=Ax;>
(Cperform the looping operationC(
for:i==n=>i"=1>i=i+2=n++;
s=s+:poB:G1=n;CpoB:x=i;(fa7t:i;;>
printf:?@n the result is &f?=s;>
get7h:;>
E
(C 7alling sub programC(
long fa7t:int x;
<
long int J=1>
Bhile:x%=;
<
J=JCx>
xGG>
E
return J>
E
Output:
18Enter the value of x : 1
The result is 8$!32
2 Enter the value of x: 2
The result is G8!161$$
Conclusion: The program is error free
VIVA Q!"A#IO$":
1; 4hat is fun7tion H
'ns: ' fun7tion is a sub program it returns a value8
2; 4hat is pro7edure H
'ns: ' pro7edure is a sub program it does not returns a value

3; 4hat are the basi7 data tJpes in M H
'ns: int= 7har= float= double
!; 5oB to define prepro7essor H
'ns: IJ using the 6 sJmbal Ex: 6in7lude"stdio8h9
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F
C & DATA STRUCTURE LAB PROGRAMS
Objective: ,
3rograms that use re7ursive fun7tion to find the fa7torial of a given integer8
Description:
-a7torial of a number is nothing but the multipli7ation of numbers from a given
number to 1
Algorithm: main program
Step 1: start
Step 2: read n
Step 3: 7all sub program as f=fa7t:n;
Step !: print f value
Step $: stop
"ub program:
Step 1: initialize the f
Step 2: if n= = or n == 1 return 1 to main program if not goto step 3
Step 3: return nCfa7t:nG1; to main program
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 21
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 22
Start
+ead n
Mall subprogram
- = fa7t:n;
output
Stop
"ub program
-a7t :;
1f n= UU n=1
+eturn
nCfa7t:nG1;
+eturn to main
program
true
false
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
int fa7t:int n;
<
int f>
if::n==;UU:n==1;; (( 7he7D the 7ondition for the n value
return:n;>
else
f=nCfa7t:nG1;> ((7al7ulate the fa7torial of n
return:f;>
E
void main:;
<
int n>
7lrs7r:;>
printf:?enter the number :?;>
s7anf:?&d?=An;>
printf:?fa7toria of number&d?=fa7t:n;;>
get7h:;>
E
Output:
18 Enter the number : $
-a7torial of number: 12
28 Enter the number : 3
-a7torial of number: 6
38 Enter the number : F
-a7torial of number: G3336
Conclusion: the program is error free
VIVA Q!"A#IO$":
1; 4hat is the meaning of fa7torial numberH
Ans : -a7torial of a number is nothing but the multipli7ation of numbers from a given
number to 1
2; 4hat is the meaning of re7usive fun7tion H
'ns: ' fun7tion 7all it self is 7alled re7ursive fun7tion
3; define librarJ fun7tions H
'ns: The fun7tions have alreadJ been Britten= 7ompiled and pla7ed in libraries and are
7alled librarJ fun7tions8
!; /efine formal parameters H
'ns: -ormal parameters are the parameters given in the fun7tion de7laration as
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 23
C & DATA STRUCTURE LAB PROGRAMS
fun7tion definition8
Objective : -
3rogram that use non re7ursive fun7tion to find the fa7torial of a given integer8
Description:
-a7torial of a number is nothing but the multipli7ation of numbers from a given
number to 1
Ex: $% =$C!C3C2C1= 12
Algorithm: main program
Step 1: start
Step 2: read n
Step 3: 7all the sub program fa7t:n;
Step !: print the f value
Step $: stop
"ub program:
Step 1: initialize the f=1
Step 2: if n== or n=1 return 1 to main program8 1f not goto step 3
Step 3: perform the looping operation as folloBs
-or i=1 i"=n> i++
Step !: f=fCi
Step $: return f value to the main program
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$
Factorial nonrecursive
start
+ead i
Mall subprogram
-a7t:n;
3rint output
Oalue of fa7t
Stop
"ub program
-a7t : ;
- = 1= i
1f n == UU
n == 1
+eturn to main

program
1 = 1 i++
1"=n

- = f C i
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 26
Factorial nonrecursive
start
+ead i
Mall subprogram
-a7t:n;
3rint output
Oalue of fa7t
Stop
"ub program
-a7t : ;
- = 1= i
1f n == UU
n == 1
+eturn to main

program
1 = 1 i++
1"=n

- = f C i
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
int fa7t:int n; ((starting of the sub program
<
int f=1=i>
if::n==;UU:n==1;; (( 7he7D the 7ondition for n value
return:1;>
else
for:i=1>i"=n>i++; (( perform the looping operation for 7al7ulating the fa7torial
f=fCi>
return:f;>
E
void main:;
<
int n>
7lrs7r:;>
printf:?enter the number :?;>
s7anf:?&d?=An;>
printf:?fa7toria of number&d?=fa7t:n;;>
get7h:;>
E
Output:
18Enter the number: #
-a7torial of number: $!
28 Enter the number: 6
-a7torial of number: #2
38 Enter the number: *
-a7torial of number: G2$216
Conclusion:
The program is error free
VIVA Q!"A#IO$":
1; 4hat is meant bJ 7all bJ value H
'ns: passing values to the fun7tion as arguments
2; 4hat is meant bJ 7all bJ referen7e H
'ns: passing address to the fun7tion as arguments
3;define a7tual parameters H
'ns: The a7tual parameters often DnoBn as arguments are spe7ified in the fun7tion 7all8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2#
C & DATA STRUCTURE LAB PROGRAMS
Objective: .
To find the )M/ of tBo given integers bJ using the re7ursive fun7tion
Description:
)M/ means )reatest Mommon /ivisor8 i8e the highest number Bhi7h divides the
given number
Ex: )M/:12=2!; is 12
-ormula: )M/= produ7t of numbers( .MN of numbers
Algorithm: main program
Step 1: start
Step 2: read a=b
Step 3: 7all the sub program )M/:a=b; for print the value
Step !: stop
"ub program:
Step 1: if n9m return )M/:n=m;
Step 2: if n== return m else goto step 3
Step 3: return )M/ :n=m&n;
Step !: return to main program
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2*
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2F
Start
+ead a=b
Mall sub program
)=g7d:a=b;
3rint g7dvalue
Stop
)7d: ;
1f
n9m
+eturn
)7d :n=m;
1f
n==
+eturn m
Mall the same fun7tion
+eturn g7d : n=m&n;
+eturn to main
program
true false
true
-
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
int g7dre7ursive:int m=int n; (( starting of the sub program
<
if:n9m;
return g7dre7ursive:n=m;>
if:n==;
return m>
else
return g7dre7ursive:n=m&n;> (( return to the main program
E
void main:;
<
int a=b=ig7d>
7lrs7r:;>
printf:?enter the tBo numbers Bhose g7d is to be found:?;>
s7anf:?&d&d?=Aa=Ab;>
printf:?)M/ of a=b is &d?=g7dre7ursive:a=b;;> (( return to the sub program
get7h:;>
E
Output:
18 enter the tBo numbers Bhose g7d is to be found:$=2$
)M/ of a=b is : $
28 enter the tBo numbers Bhose g7d is to be found:36=$!
)M/ of a=b is : 1*
38 enter the tBo numbers Bhose g7d is to be found:11=13
)M/ of a=b is : 1
Conclusion:
The program is error free
VIVA Q!"A#IO$":
1; 4hat is meaning of )M/ H
'ns: )M/ means )reatest Mommon /ivisor8 i8e the highest number Bhi7h divides
the given number
2; /efine s7ope of a variable H
'ns: The s7ope of a variable 7an be define as the region over Bhi7h the variable is
a77essible
3; ShoB an s7ope resolution operator H
'ns: double 7olon:::;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 3
C & DATA STRUCTURE LAB PROGRAMS

!; /efine extent of a variable H
'ns: The period of time during Bhi7h memorJ is asso7iated Bith a variable is 7alled
extent of the variable8
Objective: /
To find the )M/ of tBo given integers bJ using the non re7ursive fun7tion
Description:
)M/ means )reatest Mommon /ivisor8 i8e the highest number Bhi7h divides the
given number
Ex: )M/:12=2!; is 12
-ormula: )M/= produ7t of numbers( .MN of numbers
Algorithm:
Step 1: start
Step 2: read a=b
Step 3: 7all sub program g=)M/:a=b;
Step !: print the g value
Step $: stop
Sub program:
Step 1: initialize the p=1= K= remainder
Step 2: remainder=pG:p(KCK;
Step 3: remainder= return K else goto step !
Step !: )M/:K=remainder; return to main program
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 31
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 32
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 33
start
+ead a= b
Mall subprogram g=g7d:a=b;
output
stop
)7d : ;
+emainder=pG:p(KCK;
1f
remainder==
)7d:K=remainder;
+eturn K
+eturn to main program
false True
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
6in7lude"math8h9
int g7dnonre7ursive:int m=int n;
<
int remainder>
remainder=mG:m(nCn;>
if:remainder==;
return n>
else
g7dnonre7ursive:n=remainder;>
E
void main:;
<
int a=b=ig7d>
7lrs7r:;>
printf:?enter the tBo numbers Bhose g7d is to be found:?;>
s7anf:?&d&d?=Aa=Ab;>
printf:?)M/ of &d?=g7dnonre7ursive:a=b;;>
get7h:;>
E
Output:
18 enter the tBo numbers Bhose g7d is to be found:$=2$
)M/ of a=b is : $
28 enter the tBo numbers Bhose g7d is to be found:36=$!
)M/ of a=b is : 1*
38 enter the tBo numbers Bhose g7d is to be found:11=13
)M/ of a=b is : 1
Conclusion:
The program is error free
VIVA Q!"A#IO$":
1&4hat is meaning of )M/ H
'ns:
)M/ means )reatest Mommon /ivisor8 i8e the highest number Bhi7h divides the
given number
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 3!
C & DATA STRUCTURE LAB PROGRAMS
Objective :10
To solve the toBers of 5anoi problem bJ using the re7ursive fun7tion
Description:
ToBers of 5anoi problem means Be have three toBers
5ere sour7e =intermediate and destination are the three toBers8 4e have to transfer
all the disDs from sour7e to destination toBers8 5ere the restri7tion is not to pla7e
a big disD on smaller one 8 for this Be use intermediate toBer8 -inallJ the
arrangements in the destination toBer must be as same as the disDs in the sour7e
toBer at first8
Algorithm: main program
Step 1: start
Step 2: initialize the sour7e=a= intermediate=7= destination = d
Step 3: read n
Step !: 7all the sub program 5anoi re7ursion :n value=a =b= 7;
Step $: stop
Sub program:
Step 1: if n== 1 7all the sub program 5anoi re7ursion :numG1= a= 7= b;
Step 2: print the output from a to b
Step 3: 7all the sub program 5anoi re7ursion:numG1= b= 7= a;
Step !: return to main program
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 3$
Sour7e intermediate destination
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 36
ST'+T
S2,+ME = '
10TE+NE/1'TE = M
/EST10'T120 = I
+E'/ n
Mall subprogram
5anoi:num=sour7e=intermediate=destination; '=M= I
Stop
3rint '=M
+eturn to main
program
hanoire7ursive: ;
1f num==1
3rint '=M
Mall subprogram
5anoi:numG1=I=M=';;
true false
Mall sbgroram
0umG1=sour7e V'W
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
void 5anoire7ursion:int num=7har ndl1=7har ndl2=7har ndl3;
<
if:num==1;
<
printf:?Nove top disD from needle &7 to needle &7?=ndl1=ndl2;>
return>
E
5anoire7ursion:numG1=ndl1=ndl3=ndl2;>
printf:?Nove top disD from needle &7 to needlle &7?=ndl1=ndl2;>
5anoire7ursion:numG1=ndl3=ndl2=ndl1;>
E
void main:;
<
int no>
7lrs7r:;>
printf:?Enter the no8 of disD to be transferred:?;>
s7anf:?&d?=Ano;>
if:no"1;
printf:?@n ThereXs nothing to move?;>
else
printf:?@n re7ursive?;>
5anoire7ursion:no=X'X=XIX=XMX;>
get7h:;>
E
Outputs:
18 Enter the no8 of disD to be transferred :3
Nove top disD from needle ' to needle I
Nove top disD from needle ' to needle M
Nove top disD from needle I to needle M
Nove top disD from needle ' to needle I
Nove top disD from needle M to needle '
Nove top disD from needle M to needle I
Nove top disD from needle ' to needle I
Conclusion:
The program is error free
VIVA Q!"A#IO$":
1& 'hat is purpose o1 towers o1 2anoi (
Ans: 4e have to transfer all the disDs from sour7e to destination toBers8 5ere the
restri7tion is not to pla7e a big disD on smaller one 8 for this Be use intermediate
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 3#
C & DATA STRUCTURE LAB PROGRAMS
toBer8 -inallJ the arrangements in the destination toBer must be as same as the
disDs in the sour7e toBer at first8
Objective :11
To solve the toBers of 5anoi problem bJ using the non re7ursive fun7tion
Description:
ToBers of 5anoi problem means Be have three toBers
5ere sour7e =intermediate and destination are the three toBers8 4e have to transfer
all the disDs from sour7e to destination toBers8 5ere the restri7tion is not to pla7e
a big disD on smaller one 8 for this Be use intermediate toBer8 -inallJ the
arrangements in the destination toBer must be as same as the disDs in the sour7e
toBer at first8
Algorithm:
Step 1: start
Step 2: de7lare the no
Step 3: read the no value
Step !: if :no"1; 3rint nothing to move
Else 3rint nonre7ursion
Step $:5anoi non re7ursion:no=8'Y=YIY=YMY;
Step 6:stop
Sub program:
Step 1: /e7lare num=sndl=indl=dndl=
stDnZ [=stDsndlZ [=stDdndl Z [=stDaddZ [=
temp=top=add
Step 2: de7lare the top=0,..
Step 3: one:
1f:num==1;then
3rint the out put value
)oto four
Step !: tBo:
Top=top+1
StDnZtop[=num
StDsndlZtop[=sndl
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 3*
Sour7e intermediate destination
C & DATA STRUCTURE LAB PROGRAMS
StDindlZtop[=indl
StDdndlZtop[=dndl
StDaddZtop[=3
0um=numG1
Sndl=sndl
Temp=indl
1ndl=dndl
/ndl=temp
)oto one8 )oto step 3
Step $:
Three:
3rint the output
Top=top+1
StDnZtop[=num
StDsndlZtop[=sndl
StDindlZtop[=indl
StDdndlZtop[=dndl
StDaddZtop[=$
0um=numG1
temp=sndl
sndl=indl
1ndl=temp
/ndl=dndl
)oto one8 )oto step 3
Step 6:
-our:
1f:top==0,..;
+eturn to main program
0um= stDnZtop[
Sndl= stDsndlZtop[
1ndl= stDindlZtop[
/ndl=stDdndlZtop[
'dd=stDaddZtop[
Top=topG1
1f:add==3;
)oto three8 )oto step $
Else
1f:add==$;
)oto four8 )oto step 68
Step #: return to main program
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 3F
C & DATA STRUCTURE LAB PROGRAMS
Flow chart:

VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !
Start
+ead no
1f no" 1
3rint nothing to
move
true false
3rint
nonre7ursion
5anoinonre7ursion
:no=8'Y=YIY=YMY;
Stop
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !1
5anoi nonre7ursion : ;
/e7lare num=sndl=indl=dndl=
stDnZ [=stDsndlZ [=stDdndl Z [=stDaddZ [=
temp=top=add
Top = 0,..
1f num==1 3rint the value
true
1f
top=0,.
.
0um= stDnZtop[
Sndl= stDsndlZtop[
1ndl= stDindlZtop[
/ndl=stDdndlZtop[
'dd=stDaddZtop[
Top=topG1
1f
add==3
false
1f
add==
$
Top=top+1
StDnZtop[=num
StDsndlZtop[=sndl
StDindlZtop[=indl
StDdndlZtop[=dndl
StDaddZtop[=3
0um=numG1
Sndl=sndl
Temp=indl
1ndl=dndl
/ndl=temp
one
four
tBo
3rint value
Top=top+1
StDnZtop[=num
StDsndlZtop[=sndl
StDindlZtop[=indl
StDdndlZtop[=dndl
StDaddZtop[=$
0um=numG1
temp=sndl
sndl=indl
1ndl=temp
/ndl=dndl
Top=top+1
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
void 5anoinonre7ursion:int num=7har sndl=7har indl=7har dndl;
<
7har stDnZ1[=stDsndlZ1[=stDindlZ1[=stDdndlZ1[=stDaddZ1[=temp>
int top=add>
top=0,..>
one:
if:num==1;
<
printf:?@n Nove top disD from needle &7 to needle &7?=sndl=dndl;>
goto four>
E
tBo:
top=top+1>
stDnZtop[=num>
stDsndlZtop[=sndl>
stDindlZtop[=indl>
stDdndlZtop[=dndl>
stDaddZtop[=3>
num=numG1>
sndl=sndl>
temp=indl>
indl=dndl>
dndl=temp>
goto one>
three:
printf:?@n Nove top disD from needle &7 to needle &7?=sndl=dndl;>
top=top+1>
stDnZtop[=num>
stDsndlZtop[=sndl>
stDindlZtop[=indl>
stDdndlZtop[=dndl>
stDaddZtop[=$>
num=numG1>
temp=sndl>
sndl=indl>
indl=temp>
dndl=dndl>
goto one>
four:
if:top==0,..;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !2
C & DATA STRUCTURE LAB PROGRAMS
return>
num=stDnZtop[>
sndl=stDsndlZtop[>
indl=stDindlZtop[>
dndl=stDdndlZtop[>
add=stDaddZtop[>
top=topG1>
if:add==3;
goto three>
else if:add==$;
goto four>
E
void main:;
<
int no>
7lrs7r:;>
printf:?Enter the no8 of diss to be transferred:?;>
s7anf:?&d?=Ano;>
if:no"1;
printf:?@n ThereXs nothing to move?;>
else
printf:?@n nonre7ursive?;>
5anoinonre7ursion:no=X'X=XIX=XMX;>
get7h:;>
E
Output:
18Enter the no8 of diss to be transferred:3
nonre7ursive
Nove top disD from needle ' to needle M
Nove top disD from needle ' to needle I
Nove top disD from needle M to needle I
Nove top disD from needle ' to needle M
Nove top disD from needle I to needle '
Nove top disD from needle I to needle M
Nove top disD from needle ' to needle M
Conclusion: The program is error freed
VIVA Q!"A#IO$":
1& 'hat is purpose o1 towers o1 2anoi (
Ans: 4e have to transfer all the disDs from sour7e to destination toBers8 5ere the
restri7tion is not to pla7e a big disD on smaller one 8 for this Be use intermediate
toBer8 -inallJ the arrangements in the destination toBer must be as same as the
disDs in the sour7e toBer at first8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !3
C & DATA STRUCTURE LAB PROGRAMS
%& 'hat is an arra3 (
'ns: 'n arraJ is a seKuen7e of memorJ lo7ation of same data tJpe8
Objective: 1%
The total distan7e travelled bJ vehi7le in XtX se7onds is given bJ distan7e =
ut+1(2at2 Bhere XuX and XaX are the initial velo7itJ :m(se78; and a77eleration
:m(se72;8 4rite M program to find the distan7e travelled at regular intervals of
time given the values of XuX and XaX8 The program should provide the flexibilitJ to
the user to sele7t his oBn time intervals and repeat the 7al7ulations for different
values of XuX and XaX8
Description:
The total distan7e travelled bJ vehi7le in XtX se7onds is given bJ distan7e =
ut+1(2at2 Bhere XuX and XaX are the initial velo7itJ :m(se78; and a77eleration
:m(se72;8
Algorithm:
Step 1:Start
Step2 : +ead t =dt
Step 3: Set i to 1
Step !:Set D to dt
Step $: +ead u=a
Step 6: set s to uCD+8$CdCDCD
Step #: 4rite s
Step *: 1f:D"=t; and i=1 then
Iegin
Step *81 go to step 6
'nd
Else
Iegin
Step *82 :read
Step *83 :if:P=; then
Iegin
Step *8381:Set 1 to
End
Else
Iegin
Step *8382: Set 1 to 1
Step *8383: go to step !
End
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !!
C & DATA STRUCTURE LAB PROGRAMS
Step F: Stop
Step 1: End
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !$
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
main:;
<
int a=u=t=t1=t2=i>
float s>
7lrs7r:;>
printf:?E0TE+ T5E O'.,ES 2- a=u=t=t1=t2:?;>
s7anf:?&d&d&d&d&d?=Aa=Au=At=At1=At2;>
for:i=t1>i"=t2>i=i+t; (( performing the looping operation for time intervals
<
s=:uCi;+:8$CaCiCi;> (( 7al7ulate the total distan7e
printf:?@n@nthe distan7e travelled in &d se7onds is &f ?=i=s;>
E
get7h:;>
E
Input4Output:
18E0TE+ T5E O'.,ES 2- a=u=t=t1=t2:1
2
3
1
$
the distan7e travelled in 1 se7onds is 28$
the distan7e travelled in ! se7onds is 168
28E0TE+ T5E O'.,ES 2- a=u=t=t1=t2:
1
2
3
!
the distan7e travelled in 3 se7onds is 38
conclusion: The program is error free
VIVA Q!"A#IO$":
1; 5oB manJ tJpes of arraJs are there H
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !6
C & DATA STRUCTURE LAB PROGRAMS
'ns: Three tJpes8 TheJ are one dimensional =tBo dimensional and multi dimensional
arrJs
Objective:1)
TBo integer operands and one operator form user= performs the operation
and then prints the result8
:Monsider the operators +=G=C= (= & and use SBit7h Statement;
Description:
To taDe the tBo integer operands and one operator from user to perform
the some arithmeti7 operations bJ using the folloBing operators liDe
+=G=C= (= &
Ex: 2+3=$
Algorithm:
Step 1: Start
Step 2: +ead the values of a=b and operator
Step 3: if the operator is \+Y then
+=a+b
)o to step *
IreaD
Step !: Else if the operator is \G\ then
+=aGb
)o to step *
Step $: Else if the operator is \C\ then
+=aCb
)o to step *
Step 6: Else if the operator is \(\ then
+=a(b
)o to step *
Step #: Else if the operator is \&\ then
+=a&b
)o to step *
Step *: Brite +
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !#
C & DATA STRUCTURE LAB PROGRAMS
Step F:End
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !*
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page !F
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
main:;
<
7har op>
float a=b=7>
7lrs7r:;>
printf:?enter tBo operands:?;>
s7anf:?&d&d?=Aa=Ab;>
printf:?enter an operator:?;>
s7anf:? &7?=Aop;>
sBit7h:op; (( used to sele7t parti7ular 7ase from the user
<
7ase X+X:printf:?sum of tBo numbers &2d &2d is: &d?=a=b=a+b;>
breaD>
7ase XGX:printf:?subtra7tion of tBo numbers &2d &2d is:
&d?=a=b=aGb;>
breaD>
7ase XCX:printf:?produ7t of tBo numbers &2d &2d is:
&d?=a=b=aCb;>
breaD>
7ase X(X:printf:?Kuotient of tBo numbers &2d &2d is:
&d?=a=b=a(b;>
breaD>
7ase X&X:printf:?reminder of tBo numbers &2d &2d is:
&d?=a=b=7;>
breaD>
default:printf:?please enter 7orre7t operator?;>
breaD>
E
get7h:;>
E
Input4Output:
18enter tBo operands:2 3
enter an operator:+
sum of tBo numbers 2 3 is: $
28enter tBo operands:3 !
enter an operator: G
subtra7tion of tBo numbers 3 ! is: G1
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $
C & DATA STRUCTURE LAB PROGRAMS
38enter tBo operands:3 $
enter an operator:C
produ7t of tBo numbers 3 $ is: 1$
!8enter tBo operands:$ 2
enter an operator:(
Kuotient of tBo numbers $ 2 is: 2
$8 enter tBo operands:$ 2
enter an operator:&
reminder of tBo numbers $ 2 is: 1
conclusion: The program is error free
VIVA Q!"A#IO$":
1; 4hat are the various tJpes of arithemeti7 operators H
'ns: addition :+;= multipli7ation:C;= subtra7tion :G;= division:(; = modulo:&;8
2; 4hat are the tJpes of relational operators H
'ns: less than:";= grater than:9;= less than or eKual to:"=;=eKual to:==;= et788=
3; 3; 4hat are the tJpes of logi7al operators H
'ns: logi7al '0/ :AA;= logi7al 2+:UU;= logi7al 02T:%;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $1
C & DATA STRUCTURE LAB PROGRAMS
Objective : 1*
To find both the largest and smallest number in a list of integers
Description:
This program 7ontains n number of elements= in these elements Be 7an find the
largest and smallest numbers and displaJ these tBo numbers
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i=
Step !: if i"n do as folloBs8 1f not goto step $
+ead aZi[
1n7rement i
)oto step !
Step $: min=aZ[= max=aZ[
Step 6: initialize i=
Step #: if i"n do as folloBs8 1f not goto step *
1f aZi["min
'ssign min=aZi[
1n7rement i goto Step #
Step *: print min=max
Step F: stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $2
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $3
+ead n=
aZi[=min=max=
Nin = aZ[
Nax = aZ[
3rint
min=max
1 = i++
1" n
Nax"min
'Zi[9ma
x
Nin=aZi[
Nax=aZi[
1=
1"n i++

stop
+ead aZi[
Start
true
false
true
false
false
true
Flowchart:
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
void main:;
<
int aZ1[=i=n=min=max>
7lrs7r:;>
printf:?enter the arraJ size:?;>
s7anf:?&d?=An;>
printf:?Enter the elements of arraJ?;>
for:i=>i"n>i++; (( read the elements of an arraJ
s7anf:?&d?=AaZi[;>
min=aZ[>
max=aZ[>
for:i=>i"n>i++;(( read the elements of an arraJ
<
if:aZi["min;(( 7he7D the 7ondition for minimum value
min=aZi[>
if:aZi[9max;((7he7D the 7ondition for maximum value
max=aZi[>
E
printf:?maximum value is:&d@n?=max;>
printf:?minimum value is:&d@n?=min;>
get7h:;>
E
Output:
18enter the arraJ size:!
Enter the elements of arraJ 36 13 2 !$
maximum value is:!$
minimum value is:2
28enter the arraJ size:$
Enter the elements of arraJ 6 2 1 3 *
maximum value is:*
minimum value is:1
38enter the arraJ size:$
Enter the elements of arraJG6 F GF 2 $
maximum value is:F
minimum value is:GF
conclusion: the program is error free
VIVA Q!"A#IO$":
1& 4hat is an arraJ (
'ns: The 7olle7tion of similar elements is 7alled arraJ
2; 5oB manJ tJpes of arraJs are there H
'ns: Three tJpes8 TheJ are one dimensional =tBo dimensional and multi dimensional
arrJs
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $!
C & DATA STRUCTURE LAB PROGRAMS
Objective:1+
To perform the addition of tBo matri7es
Description:
program taDes the tBo matrixes of same size and performs the addition an also
taDes the tBo matrixes of different sizes and 7he7Ds for possibilitJ of
multipli7ation and perform multipli7ation if possible8
algorithm:
Step 1: start
Step 2: read the size of matri7es '=I ] m=n
Step 3: read the elements of matrix '
Step !: read the elements of matrix I
Step $: sele7t the 7hoi7e for Jou Bant8 1f Jou sele7t 7ase 1 then goto matri7
addition8 Else goto Step #8
Step 6: print Sum of matrix ' and I
Step #: if Jou sele7t 7ase 2 then goto matrix multipli7ation
Step *: 7he7D if n=p= if not print matri7es 7an not be multiplied
Step F: 2therBise perform the multipli7ation of matri7es
Step 1: 3rint the resultant matrix
Step 11: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $$
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $6
start
+ead the size
of '=I matrix
m=n
1=
1" r1 i++
/e7lare aZ[Z[=bZ[
Z[=7Z[
Z[=7h=1=P=D=m=n=p=K=r1=
71
+ead the 7hoi7e 7h
1f 7h"=2
A7h9
3rintvalid
7hoi7e
SBit7h
7h
+ead the size
of ' matrix
m=n
+ead 'Zi[ZP[
Q=
P1" 71 Pi++
1=
1" r1 i++
P=
P1"71 P++
+ead IZiZP[
1=
1" r1 i++
P=
P"71 P++
+ead 'Zi[ZP[
+ead size of
matrix I: p8K
1=
1" p i++
I
Mase 1
Mase 2
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $#
I
1=
1" r1 i++
P=
P" 71 P++
3rint 'Zi[ZP[+IZi[ZP[

Stop
I
P=
P" K P++
+ead IZi[ZP[
1f
n==p
Natrix 7annot
be mutiplied
i=
i"m i++
P=
P" K P++
MZi[ZP[=
D
P" n D++
MZi[ZP[=MZi[ZP[+'Zi[ZD[CIZD[ZP[
i=
i"m i++
P++
P=
P"K P++
3rint MZi[ZP[
'
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
void main:;
<
int 7h=i=P=m=n=p=K=D=r1=71=aZ1[Z1[=bZ1[Z1[=7Z1[Z1[>
7lrs7r:;>
printf:?CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC?;>
printf:?@n@t@tNE0,?;>
printf:?@nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC?;>
printf:?@nZ1['//1T120 2- T42 N'T+1MES?;>
printf:?@nZ2[N,.T13.1M'T120 2- T42 N'T+1MES?;>
printf:?@nZ[E^1T?;>
printf:?@nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC?;>
printf:?@n@tEnter Jour 7hoi7e:@n?;>
s7anf:?&d?=A7h;>
if:7h"=2 A 7h9;
<
printf:?Oalid Mhoi7e@n?;>
E
sBit7h:7h;
<
7ase 1:
printf:?1nput roBs and 7olumns of ' A I Natrix:?;>
s7anf:?&d&d?=Ar1=A71;>
printf:?Enter elements of matrix ':@n?;>
for:i=>i"r1>i++;
<
for:P=>P"71>P++;
s7anf:?&d?=AaZi[ZP[;>
E
printf:?Enter elements of matrix I:@n?;>
for:i=>i"r1>i++;
<
for:P=>P"71>P++;
s7anf:?&d?=AbZi[ZP[;>
E
printf:?@n =====Natrix 'ddition=====@n?;>
for:i=>i"r1>i++;
<
-or:P=>P"71>P++;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $*
C & DATA STRUCTURE LAB PROGRAMS
printf:?&$d?=aZi[ZP[+bZi[ZP[;>
printf:?@n?;>
E
breaD>
7ase 2:
printf:?1nput roBs and 7olumns of ' matrix:?;>
s7anf:?&d&d?=Am=An;>
printf:?1nput roBs and 7olumns of I matrix:?;>
s7anf:?&d&d?=Ap=AK;>
if:n==p;
<
printf:?matri7es 7an be multiplied@n?;>
printf:?resultant matrix is &dC&d@n?=m=K;>
printf:?1nput ' matrix@n?;>
read_matrix:a=m=n;>
printf:?1nput I matrix@n?;>
(C-un7tion 7all to read the matrixC(
read_matrix:b=p=K;>
(C-un7tion for Nultipli7ation of tBo matri7esC(
printf:?@n =====Natrix Nultipli7ation=====@n?;>
for:i=>i"m>++i;
for:P=>P"K>++P;
<
7Zi[ZP[=>
for:D=>D"n>++D;
7Zi[ZP[=7Zi[ZP[+aZi[ZD[CbZD[ZP[>
E
printf:?+esultant of tBo matri7es:@n?;>
Brite_matrix:7=m=K;>
E
(Cend ifC(
else
<
printf:?Natri7es 7annot be multiplied8?;>
E
(Cend elseC(
breaD>
7ase :
printf:?@n Mhoi7e Terminated?;>
exit:;>
breaD>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page $F
C & DATA STRUCTURE LAB PROGRAMS
default:
printf:?@n 1nvalid Mhoi7e?;>
E
get7h:;>
E
(C-un7tion read matrixC(
int read_matrix:int aZ1[Z1[=int m=int n;
<
int i=P>
for:i=>i"m>i++;
for:P=>P"n>P++;
s7anf:?&d?=AaZi[ZP[;>
return >
E
(C-un7tion to Brite the matrixC(
int Brite_matrix:int aZ1[Z1[=int m=int n;
<
int i=P>
for:i=>i"m>i++;
<
for:P=>P"n>P++;
printf:?&$d?=aZi[ZP[;>
printf:?@n?;>
E
return >
E
Output:
18
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
NE0,
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Z1['//1T120 2- T42 N'T+1MES
Z2[N,.T13.1M'T120 2- T42 N'T+1MES
Z[E^1T
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Enter Jour 7hoi7e:
1
Oalid Mhoi7e
1nput roBs and 7olumns of ' A I Natrix:2
2
Enter elements of matrix ':
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 6
C & DATA STRUCTURE LAB PROGRAMS
2
2
2
2
Enter elements of matrix I:
2
2
2
2
=====Natrix 'ddition=====
! !
! !
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
NE0,
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Z1['//1T120 2- T42 N'T+1MES
Z2[N,.T13.1M'T120 2- T42 N'T+1MES
Z[E^1T
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Enter Jour 7hoi7e:
2
Oalid Mhoi7e
1nput roBs and 7olumns of ' matrix:2
3
1nput roBs and 7olumns of I matrix:2
2
Natri7es 7annot be multiplied8
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
NE0,
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Z1['//1T120 2- T42 N'T+1MES
Z2[N,.T13.1M'T120 2- T42 N'T+1MES
Z[E^1T
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Enter Jour 7hoi7e:
2
Oalid Mhoi7e
1nput roBs and 7olumns of ' matrix:2
2
1nput roBs and 7olumns of I matrix:2
2
matri7es 7an be multiplied
resultant matrix is 2C2
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 61
C & DATA STRUCTURE LAB PROGRAMS
1nput ' matrix
2
2
2
2
1nput I matrix
2
2
2
2
=====Natrix Nultipli7ation=====
+esultant of tBo matri7es:
* *
* *
Conclusion : The program is error free
VIVA Q!"A#IO$":
1; 4hat is 7ondition for performing an matri7 addition H
'ns: program taDes the tBo matrixes of same size and performs the addition
2; 4hat is 7ondition for performing an matri7 addition H
'ns: The tBo matrixes of different sizes and 7he7Ds for possibilitJ of multipli7ation
and perform multipli7ation if possible
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 62
C & DATA STRUCTURE LAB PROGRAMS
Objective :1,

-un7tions to insert a sub string into given main string from a given position
Description:
in this program Be need to insert a string into another string from a spe7ified
position8
Algorithm:
Step 1: start
Step 2: read main string and sub string
Step 3: find the length of main string:r;
Step !: find length of sub string:n;
Step $: 7opJ main string into sub string
Step 6: read the position to insert the sub string: p;
Step #: 7opJ sub string into main string from position pG1
Step *: 7opJ temporarJ string into main string from position p+nG1
Step F: print the strings
Step 1: stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 63
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 6!
Start
+ead the strings '
A
I
1 =
1 " r
MZi[ = 'Zi[
1 ++
S = n + r
2 = p + n
1 =p 1++
1 " s
^= MZi[
1f t " n

'

I
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 6$
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 66

'
'Zi[ = IZt[
T=t+1
'Zo[=x
2 = o + 1
I
3rint output
Stop
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude "stdio8h9
6in7lude "7onio8h9
6in7lude "string8h9
void main:;
<
7har aZ1[>
7har bZ1[>
7har 7Z1[>
int p==r==i=>
int t=>
int x=g=s=n=o>
7lrs7r:;>
puts:?Enter -irst String:?;>
gets:a;>
puts:?Enter Se7ond String:?;>
gets:b;>
printf:?Enter the position Bhere the item has to be inserted: ?;>
s7anf:?&d?=Ap;>
r = strlen:a;>
n = strlen:b;>
i=>
(( MopJing the input string into another arraJ
Bhile:i "= r;
<
7Zi[=aZi[>
i++>
E
s = n+r>
o = p+n>
(( 'dding the subGstring
for:i=p>i"s>i++;
<
x = 7Zi[>
if:t"n;
<
aZi[ = bZt[>
t=t+1>
E
aZo[=x>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 6#
C & DATA STRUCTURE LAB PROGRAMS
o=o+1>
E
printf:?&s?= a;>
get7h:;>
E
Output:
18enter first string:
7omputer
28enter se7ond string:
ge7
38enter the position Bhere the item has to be inserted:3
7omge7puter
conclusion : the program is error free
VIVA Q!"A#IO$":
1; 4hat is string H
'ns: ' string is an 7olle7tion of 7hara7ters
2; 4hi7h 7ommand is used to 7ombined the tBo strings H
'ns: Str7at:;
3; 4hi7h 7ommand is used to 7opJ the strings H
'ns: IJ using the str7pJ:; fun7tion 7opies one string to another
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 6*
C & DATA STRUCTURE LAB PROGRAMS
Objective:1-
To delete n 7hara7ters from a given position in a given string
Description:
in this program Be need to delete a string from the given string at a spe7ified
position8
Algorithm:
Step 1: start
Step 2: read string
Step 3: find the length of the string
Step !: read the value of number of 7hara7ters to be deleted and positioned
Step $: string 7opJ part of string from position to end= and :position+number of
7hara7ters to end;
Step 6: stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 6F
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #
Start
+ead string
+ead position= no of
7hara7ters
/el7har: string= n= pos;
Stop
Subprogram
/el7har: ;
1f ::a+bG1_"=

strlen:x;;
Str7pJ:AxZbG1[=AxZa+bG1[;
3uts:x;
+eturn to
mainprogram
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude "stdio8h9
6in7lude "7onio8h9
6in7lude "string8h9
void del7har:7har Cx=int a= int b;>
void main:;
<
7har stringZ1[>
int n=pos=p>
7lrs7r:;>
puts:?Enter the string?;>
gets:string;>
printf:?Enter the position from Bhere to delete?;>
s7anf:?&d?=Apos;>
printf:?Enter the number of 7hara7ters to be deleted?;>
s7anf:?&d?=An;>
del7har:string= n=pos;>
get7h:;>
E
(( -un7tion to delete n 7hara7ters
void del7har:7har Cx=int a= int b;
<
if ::a+bG1; "= strlen:x;;
<
str7pJ:AxZbG1[=AxZa+bG1[;>
puts:x;>
E
E
Output:
18enter the string
nagaraPu
Enter the position from Bhere to delete:!
Enter the number of 7har7ters to be deleted3
nagPu
28 enter the string
DaliraPu
Enter the position from Bhere to delete:
Enter the number of 7har7ters to be deleted!
+aPu
Conclusion: the program is error free
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #1
C & DATA STRUCTURE LAB PROGRAMS
VIVA Q!"A#IO$":
1; 4hi7h 7ommand is used to delete the strings H
'ns: delstr:;>
2; 4hat are the various tJpes of string fun7tions H
'ns: Str7at:;= str7pJ:;= delstr:;= substr:; =strlen:;et788=
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #2
C & DATA STRUCTURE LAB PROGRAMS
Objective: 1.
To determine if the given string is a palindrome or not
Description:
if the reverse of a string is eKual to original string then it is 7alled palindrome
Algorithm:
Step 1:start
Step 2: read the string
Step 3: store reverse of the given string in a temporarJ string
Step !: 7ompare the tBo strings
Step $: if both are eKual then print palindrome
Step 6: otherBise print not palindrome
Step #: stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #3
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #!
Start
+ead string
1f:1spalindro
me:string;
1spalindrome:string;
3rint
palindrome
true
3rint not
palindrome
Stop
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #$
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #6
1spalindrome: ;
Enum Ioolean mat7hed= true
1f len==
+eturn
.eft =
+ight=lenG1
4hile:left"ri
ghtAAmat7h
ed
1f:stringZleft[%
=stringZright[;
Nat7hed=false
true
.eft ++
+ight GG
+eturn
mat7hed
+eturn to main
program
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"string8h9
enum Ioolean<false=trueE>
enum Ioolean 1s3alindrome:7har stringZ[;
<
int left=right=len=strlen:string;>
enum Ioolean mat7hed=true>
if:len==;
return >
left=>
right=lenG1>
(C Mompare the first and last letter=se7ond A se7ond last A so on C(
Bhile:left"rightAAmat7hed;
<
if:stringZleft[%=stringZright[;
mat7hed=false>
else
<
left++>
rightGG>
E
E
return mat7hed>
E
int main:;
<
7har stringZ![>
7lrs7r:;>
printf:?CCCC3rogram to test if the given string is a palindromeCCCC@n?;>
printf:?Enter a string:?;>
s7anf:?&s?=string;>
if:1s3alindrome:string;;
printf:?The given string &s is a palindrome@n?=string;>
else
printf:?The given string &s is not a palindrome@n?=string;>
get7h:;>
return >
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page ##
C & DATA STRUCTURE LAB PROGRAMS
Output:
18 Enter the string:malaJalam
The given string malaJalam is a palindrome
28 Enter the string:india
The given string india is not a palindrome
Conclusion: The program is error free
VIVA Q!"A#IO$":
1; 4hat is meant bJ palindrome H
'ns: 1f the reverse of a string(number is eKual to original string( number then it is
7alled palindrome8
2; 4hat is the use of gets:; fun7tion H
'ns: To read the string at a time
3; 4hat is the use of puts:; fun7tion H
'ns: To Brite the string at a time
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #*
C & DATA STRUCTURE LAB PROGRAMS
Objective: 1/
3rogram that displaJs the position or index in the string S Bhere the string T
begins = or G1 if S doesnYt 7ontain T
Algorithm:
Step 1: start
Step 2: read the string and then displaJed
Step 3: read the string to be sear7hed and then displaJed
Step !: sear7hing the string T in string S and then perform the folloBing steps
i8 found=strstr:S=T;
ii8 if found print the se7ond string is found in the first string at the
position8 1f not goto step $
Step $: print the G1
Step 6: stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page #F
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *
Start
1nitialize sZ [=tZ [= found variables
+ead first string
/isplaJ the
string
+ead string to be
sear7hed
/isplaJ the
string
-ound=strstr:s=t;
1f found
3rint G1
3rint the string
-oundGs
Stop
no
Jes
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"string8h9
6in7lude"7onio8h9
void main:;
<
7har sZ3[= tZ2[>
7har Cfound>
7lrs7r:;>
(C Entering the main string C(
puts:?Enter the first string: ?;>
gets:s;>
(C Entering the string Bhose position or index to be displaJed C(
puts:?Enter the string to be sear7hed: ?;>
gets:t;>
(CSear7hing string t in string s C(
found=strstr:s=t;>
if:found;
printf:?Se7ond String is found in the -irst String at &d position8@n?=foundGs;>
else
printf:?G1?;>
get7h:;>
E
Output:
18enter the first string:
Dali
Enter the string to be seare7hed:
li
se7ond string is found in the first string at2position
28enter the first string:
nagaraPu
Enter the string to be seare7hed:
raPu
se7ond string is found in the first string at!position
38enter the first string:
nagarPuna
Enter the string to be seare7hed:
ma
G1
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *1
C & DATA STRUCTURE LAB PROGRAMS
Conclusion: The program is error free
VIVA Q!"A#IO$":
1; 4hat is the differen7e betBeen printf:; and puts:; H
'ns: puts:; is used to displaJ the string at a time and it doesnYt taDe anJ integers
values but printf:; taDes anJ values as defined bJ the user
2; define pointer variable H
'ns: pointer variables are defined as variable that 7ontain the memorJ addresses of
data or exe7utable 7ode8
3; 4hat is use of the str7mp:; fun7tion H
'ns: This fun7tion 7ompares tBo strings 7hara7ter bJ 7hara7ter and returns a value
if both strings are eKual and non zero value if the strings are different8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *2
C & DATA STRUCTURE LAB PROGRAMS
Objective: %0
To 7ount the lines=Bords A 7har7ters in a given text
Description:
1n this program Be have to 7ount the no of lines= no of Bords and no of
7hara7ters in a given program or given text bJ using the string fun7tion
Algorithm:
Step 1: Start
Step 2: +ead the text until an emptJ line
Step 3: Mompare ea7h 7hara7ter Bith neBline 7har \@nY to 7ount no of lines
Step !: Mompare ea7h 7hara7ter Bith tab 7har \@t@Y or spa7e 7har \ \ to 7ount no
of Bords
Step $: Mompare first 7hara7ter Bith 0,.. 7har \@Y to find the end of text
Step 6: 0o of 7hara7ters = length of ea7h line of text
Step #: 3rint no of lines= no of Bords= no of 7hars
Step *: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *3
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *!
Start
1nitialize end==7hars==Bords==lines=
4hile
End==

M =
true
1f :7tr=get7har:;;%
=Y@nY
false
.ineZ7[=Y@Y
.ineZ7++[=7tr
true
1f
lineZ[=Y@
Y
true
4ords ++ 1 =
1 ++ lineZi[%=Y@@
false
1f lineZi[==Y \UU
.ineZi[==Y@tY
.ines++
Mhars+=strlen:line;
4ords ++
3rint lines=
4ords=7hars
stop
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude "stdio8h9
main:;
<
7har lineZ*1[= 7tr>
int i=7=
end = =
7hara7ters = =
Bords = =
lines = >
printf:?RET 10 T5E TE^T8@n?;>
printf:?)1OE 20E S3'ME '-TE+ E'M5 42+/8@n?;>
printf:?45E0 M2N3.ETE/= 3+ESS X+ET,+0X8@n@n?;>
Bhile: end == ;
<
(C +eading a line of text C(
7 = >
Bhile::7tr=get7har:;; %= X@nX;
lineZ7++[ = 7tr>
lineZ7[ = X@X>
(C 7ounting the Bords in a line C(
if:lineZ[ == X@X;
breaD >
else
<
Bords++>
for:i=> lineZi[ %= X@X>i++;
if:lineZi[ == X X UU lineZi[ == X@tX;
Bords++>
E
(C 7ounting lines and 7hara7ters C(
lines = lines +1>
7hara7ters = 7hara7ters + strlen:line;>
E
printf :?@n?;>
printf:?0umber of lines = &d@n?= lines;>
printf:?0umber of Bords = &d@n?= Bords;>
printf:?0umber of 7hara7ters = &d@n?= 7hara7ters;>
E
Output
18RET 10 T5E TE^T8
)1OE 20E S3'ME '-TE+ E'M5 42+/8
45E0 M2N3.ETE/= 3+ESS X+ET,+0X8
'dmiration is a verJ shortGlived passion8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *$
C & DATA STRUCTURE LAB PROGRAMS
'dmiration involves a glorious obliKuitJ of vision8
'lBaJs Be liDe those Bho admire us but Be do not
liDe those Bhom Be admire8
-ools admire= but men of sense approve8
0umber of lines = $
0umber of Bords = 36
0umber of 7hara7ters = 2$
Conclusion: The program is error free
VIVA Q!"A#IO$":
1; 4hat is use of strlen:; H
'ns: to read a string length
2; Bhat is the use of get7:; fun7tion H
'ns: To read the 7hara7ter one bJ one8
3; 4hat is the use of strstr :; H
'ns: The fun7tion strstr:; sear7hes one string for the o77urren7e of another81t a77epts
tBo strings as parameters and sear7hes the first string for an o77urren7e of the
se7ond
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *6
C & DATA STRUCTURE LAB PROGRAMS
Objective %1:
To generate 3as7alYs triangle
Description:
3as7alYs triangle Bhi7h is used for a 7oeffi7ient in the eKuation in
polJnominals8
Alogrithm:
Step 1: Start
Step 2: 1nitialize m=
Step 3: +ead n
Step !: 1f m"n goto step $8if not goto step 12
Step $: initialize i=!Gm
Step 6: 1f i9 is true do as folloBs8 1f not goto step #
i8 print Bhite spa7e
ii8 de7rement i
iii8 goto Step 6
Step #: 1nitialize P=
Step *: 1f P=m do as folloBs8 1f not goto Step 1
i; if:P==UUm==;
ii; 1nitialize b=1 if not b=bC:mGP+1;(P
iii; 3rint Bhite spa7e= b 8
iv; )oto Step F
Step F: in7rement P= goto Step *
Step 1: print neB line 7ontrol
Step 11: in7rement m= goto step !
Step 12: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *#
C & DATA STRUCTURE LAB PROGRAMS
Flow chart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page **
Start
+ead p
1= 1 " p
1++
true
+=!G1 rGG
r9
3rint Bhite
spa7e
true
^= x"=i
^++
false
true
1f x==
UU
1==
false
true
I=1
I=bC:iGx+1;(x
3tint b
-
3rint
neBlin
e
Stop
false
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page *F
Start
+ead p
1= 1 " p
1++
true
+=!G1 rGG
r9
3rint Bhite
spa7e
true
^= x"=i
^++
false
true
1f x==
UU
1==
false
true
I=1
I=bC:iGx+1;(x
3tint b
-
3rint
neBlin
e
Stop
false
C & DATA STRUCTURE LAB PROGRAMS
Program:

6in7lude"stdio8h9
6in7lude"7onio8h9
void main:;
<
int i=p=r=x=binom=1>
7lrs7r:;>
printf:?enter the hoB manJ lines to print?;>
s7anf:?&d?=Ap;>
i=>
Bhile:i"p; (( 7he7D the 7ondition
<
for:r=!Gi>r9>rGG; (( perform the looping operation until
printf:? ?;>
for:x=>x"=i>x++;
<
if::x==;UU:i==;; (( 7he7D the 7ondition
binom=1>
else
binom=binomC:iGx+1;(x>
printf:?&d?=binom;>
printf:? ?;>
E
printf:?@n?;>
i++>
E
get7h:;>
E
Output:
18enter the hoB manJ lines to print$
1
1 1
1 2 1
1 3 3 1
1 ! 6 ! 1
28enter the hoB manJ lines to print3
1
1 1
1 2 1
Conclusion: the program is error free
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F
C & DATA STRUCTURE LAB PROGRAMS
VIVA Q!"A#IO$":
1& 4hat is meant bJ 3as7alYs triangle H
Ans: 3as7alYs triangle Bhi7h is used for a 7oeffi7ient in the eKuation in polJnominals
2;define stru7ture H
'ns: ' stru7ture in 7 is a heterogenous user efined data tJpe8 ' stru7ture maJ 7ontain
different data tJpes81t groups variables into a single entitJ8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F1
C & DATA STRUCTURE LAB PROGRAMS
Objective %%:
To 7onstru7t a pJramid of numbers
Description:
1n this program the Be have to 7onstru7t output in the pJramid shape manner
Algorithm:
Step 1: Start
Step2: initialize the num=1=J= x=3$
Step3: read the num
Step!:perform the loop operation
-or:J=>J"=num>J++;
Step$:)otoxJ:x=J+1;
Step6: perform the loop operation for displaJing digits toBards the left and right
-or:i=GJ>i"=J>i++;
Step#: print abs:i;>
Step*: x=xG2>
StepF: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F2
C & DATA STRUCTURE LAB PROGRAMS
Flow chart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F3
Start
1nitialize
num=1=J=x=3$
+ead num
T = J"=num
J ++
true
)otoxJ:x=J+1;
1 = i++
1"=J
3rint
abs:i;
^=xG2
Stop
C & DATA STRUCTURE LAB PROGRAMS

Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
void main:;
<
int num=i=J=x=3$>
7lrs7r:;>
printf:?@nEnter the number to generate the pJramid:@n?;>
s7anf:?&d?=Anum;>
for:J=>J"=num>J++;
<
(C:xG7oordinate=JG7oordinate;C(
gotoxJ:x=J+1;>
(Cfor displaJing digits toBards the left and right of zeroC(
for:i=GJ>i"=J>i++;
printf:?&3d?=abs:i;;>
x=xG3>
E
get7h:;>
E
Output:

18enter the number:
! 1 1
2 1 1 2
3 2 1 1 2 3
! 3 2 1 1 2 3 !
28enter the number:
3 1 1
2 1 1 2
3 2 1 1 2 3
Conclusion: The program is error free
VIVA Q!"A#IO$":
1; 4hat is the use of dot operator in stru7tures H
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F!
C & DATA STRUCTURE LAB PROGRAMS
'ns: The use of dot:5; operator to a77ess the members of a stru7ture independentlJ8
The dot operator 7onne7ts a member Bith the stru7ture variable8
2; /efine unions H
'ns: ' union is a data tJpe in 7 Bhi7h alloBs the overlaJ of more than one variable in
the same memorJ area8
Objective %):
To read in tBo numbers x and n and then 7ompute the sum of this geometri7
progression 1+x+x
2+
x
3
+LLL8+x
n
Description:
1n this program Be have to read the tBo numbers and the 7al7ulate the sum of this
geometri7 progression in above mention 8
Algorithm:
Step 1: Start
Step 2: read values of x and n= sumG1= i=1
Step 3: 7he7D for n A ^
i; if n"= UU x"=
ii; print values are not valid
iii; read values of x and n
Step !: perform the loop operation
i; for:i=1>i"=n>i++; then folloBs
ii; sum=sum+poB:x=i;
Step $: print sum
Step 6: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F$
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F6
Start
1nitialize
Sum=1=x=n
+ead x=n
1f
n"=UU
x"=
3rint not
valid
1=1 i"=n
1++
Sum=sum+poB:x=i;
3rint sum
Stop
false
true false
Sum = 1
true
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
6in7lude"math8h9
void main:;
<
int s_sum=i=x=n>
7lrs7r:;>
printf:?Enter the values for x and n:?;>
s7anf:?&d &d?=Ax=An;>
if:n"= UU x"=;
<
printf:?Oalue is not valid@n?;>
E
else
<
printf:?Oalue is valid@n?;>
s_sum=1>
for:i=1>i"=n>i++;
<
s_sum=s_sum+poB:x=i;>
E
printf:?Sum of series=&d@n?=s_sum;>
E
get7h:;>
E
Output:
18Enter the values for x and n:2
3
Oalue is valid
Sum of series=1$
28Enter the values for x and n:!
F
Oalue is valid
Sum of series=21*!$
38Enter the values for x and n:
1
Oalue is not valid
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F#
C & DATA STRUCTURE LAB PROGRAMS
Conclusion: the program is error free
VIVA Q!"A#IO$":
1; Bhat are the differen7e betBeen stru7tures and unions H
'ns: 5ere the maPor differen7e is Bith in the stru7ture all elements must be allo7ated
memorJ8 Iut in union highest memorJ allo7ation must be allo7ated the all these
elements8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page F*
C & DATA STRUCTURE LAB PROGRAMS
Objective %*:
To 7onvert the given binarJ number to 2Ys 7omplement
Description:
1n this program the given binarJ number is first 7overt the numbers to1 and
1 to 8 'nd finallJ add the 1 to the 7onverted number8 Then Be Bill get the
2Ys 7omplement number8
Algorithm: main program
Step 1: Start
Step 2: de7lare the subprogram V7omplement:7har Ca;W
Step 3: initialize the variable i
Step !: read the binarJ number
Step $: perform the loop operation8 if it is true then folloBs8 if not goto step #
i; for:i=>aZi[%=Y@Y>i++;
ii; if:aZi[%=YYAAaZi[%=Y1Y; then displaJed the number is not valid8
enter the 7orre7t number8
iii; Exit the loop
Step 6: 7all sub program \7omplemt:a;Y
Step #: stop
"ub program:
Step 1: initialize the variable 1=7==bZ16
Step 2: 1=strlen:a;
Step 3: perform the loop operation8 if it is true then folloBs8 if not goto
i;for:i=lG1>i9=>iGG;
ii;if:aZi[==YY; then bZi[=Y1Y else
iii;bZi[=YY
Step !: for:i=lG1>i9=>iGG; is true
i; if:i==lG1; then
ii; if:bZi[==YY; then bZi[=Y1Y else
iii; bZi[=YY=7=1 if not goto step $
Step $: if:7==1AAbZi[==YY; is true then
i; bZi[=Y1Y= 7= if not goto Step 6
Step 6: if:7==1AAbZi[==Y1Y; then bZi[=YY=7=1
Step #: displaJed bZl[=Y@Y
Step *: print b and return to main program
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page FF
C & DATA STRUCTURE LAB PROGRAMS
Flow chart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1
Start
1nitialize 1= aZ [
+ead a
1 = i++
'Zi[%=Y@Y
true
1f aZi[%
=YYAAaZi[%=Y1Y
0umber is
not valid
true
false
Mall sub program
Momplement:a;
false
Stop
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 11
Momplement:
;
1nitialize
=1=7==bZ [
1=strlen:a;
1=iG1 1GG
i9=
true
1f
aZi[==Y
Y
IZi[=Y1Y
true
IZi[=[[
false
1=lG1 iGG
i9=
true
1f i==lG
1
true
1f
bZi[==YY
IZi[=Y1Y
true
bZi[=YY
M=1
false
1f
7==1AA

IZi[==YY
IZi[=Y1Y
M =
'
I
false
true
M
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude "stdio8h9
6in7lude"7onio8h9
void 7omplement :7har Ca;>
void main:;
<
7har aZ16[>
int i>
7lrs7r:;>
printf:?Enter the binarJ number?;>
gets:a;>
for:i=>aZi[%=X@X> i++;
<
if :aZi[%=XX AA aZi[%=X1X;
<
printf:?The number entered is not a binarJ number8 Enter the
7orre7t number?;>
exit:;>
E
E
7omplement:a;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 12
'
1f 7==1
AA
bZi[==1
IZi[=Y[
M=1
true
I
M
IZi[=Y@Y
3rint \bY
+eturn to
main
program
C & DATA STRUCTURE LAB PROGRAMS
get7h:;>
E
void 7omplement :7har Ca;
<
int l= i= 7=>
7har bZ16[>
l=strlen:a;>
for :i=lG1> i9=> iGG;
<
if :aZi[==XX;
bZi[=X1X>
else
bZi[=XX>
E
for:i=lG1> i9=> iGG;
<
if:i==lG1;
<
if :bZi[==XX;
bZi[=X1X>
else
<
bZi[=XX>
7=1>
E
E
else
<
if:7==1 AA bZi[==XX;
<
bZi[=X1X>
7=>
E
else if :7==1 AA bZi[==X1X;
<
bZi[=XX>
7=1>
E
E
E
bZl[=X@X>
printf:?The 2Xs 7omplement is &s?= b;>
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 13
C & DATA STRUCTURE LAB PROGRAMS
Output:
18Enter the binarJ number111
The 2Xs 7omplement is 111
Enter the binarJ number11111
The 2Xs 7omplement is 1
Enter the binarJ number2222
The number entered is not a binarJ number8 Enter the 7orre7t number
Conclusion: the program is error free
VIVA Q!"A#IO$":
1; Expand 'SM11 H
'ns: 'meri7an standarad 7ode for information inter7hange
2;4hat is binarJ number H
'ns: The number Bhi7h 7ontains onlJ and 1 is 7alled binarJ number8
3; /efine 2Ws 7omplement H
'ns: The given binarJ number is first 7overt the numbers to1 and 1 to 8 'nd finallJ
add the 1 to the 7onverted number8 Then Be Bill get the 2Ys 7omplement number8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!
C & DATA STRUCTURE LAB PROGRAMS
Objective %+:
To 7onvert roman number to itYs de7imal eKuivalent
Description:
1n this program Be have to taDe the roman value8 This value is 7onverted into a
itYs eKuivalent de7imal number8
Ex: ^=1
Algoritham:
Step 1: Start
Step 2: read the roman numeri7al as string
Step 3: find length of roman numeri7al
Step !: for ea7h 7har7ter in the string
i; if:7har=1; then de7imal=1
ii; if:7har=O; then de7imal=$
iii; if:7har=^; then de7imal=1
iv; if:7har=.; then de7imal=$
v; if:7har=M; then de7imal=1
vi; if:7har=/; then de7imal=$
vii; if:7har=N; then de7imal=1
viii; otherBise invalid 7hara7ter
Step $: repeat step ! until the length of the string
Step 6: D=7harZlengthG1[
Step #: for ea7h 7hara7ter of de7imal string
i; if:de7imalZi[9de7ZiG1[; then D=DGde7imalZiG1[
ii; else if:de7imalZi[=de7imalZiG1 or de7imalZi["de7imalZiG1; then
D=D+de7imallZiG1[
Step *: repate step # until the length of de7imal string
Step F: print de7imal value
Step 1: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$
C & DATA STRUCTURE LAB PROGRAMS
Flow chart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 16
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#
Start
+ead
roman
value
.en=strlen:rom;
1= i++
1" len
1f
romZi[==Y1Y
'Zi[=1
True
false
1f
romZi[==YOY
'Zi[=$
true
false
1f
romZi[==Y^Y
'Zi[=1
true
false
I
M
R=aZlenG1[
1=lenG1 iS
1 9
true
'Zi[
9aZiG
1[
true
D=DGaZiG1[
-
'Zi[=
=aZiG
1[UU
aZi["a
ZiG1[
true
D=D+aiG1[
'
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*
I
1f
romZi[==Y.Y
'Zi[=$
true
false
1f
romZi[==YMY
'Zi[=1
true
false
1f
romZi[==Y/Y
'Zi[=$
true
1f
romZi[==YMY
'Zi[=1
false
true
M
3rint
invalid
'
3rint D
Stop
C & DATA STRUCTURE LAB PROGRAMS
Program:

6in7lude"stdio8h9
6in7lude"7onio8h9
6in7lude"string8h9
6in7lude"stdlib8h9
void main:;
<
int Ca=len=i=P=D>
7har Crom>
7lrs7r:;>
printf:?Enter the +oman 0umeral:?;>
s7anf:?&s?=rom;>
len=strlen:rom;>
for:i=>i"len>i++; (( loop Bill 7ontinue until 1 is not graterthan length8
<
if:romZi[==X1X;
aZi[=1>
else if:romZi[==XOX;
aZi[=$>
else if:romZi[==X^X;
aZi[=1>
else if:romZi[==X.X;
aZi[=$>
else if:romZi[==XMX;
aZi[=1>
else if:romZi[==X/X;
aZi[=$>
else if:romZi[==XNX;
aZi[=1>
else
<
printf:?@n1nvalid Oalue?;>
get7h:;>
exit:;>
E
E
D=aZlenG1[>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F
C & DATA STRUCTURE LAB PROGRAMS
for:i=lenG1>i9>iGG; (( loop Bill 7ontinue until 1 lessthan zero
<
if:aZi[9aZiG1[; (( 7he7D the 7ondition
D=DGaZiG1[>
else if:aZi[==aZiG1[ UU aZi["aZiG1[;
D=D+aZiG1[>
E
printf:?@n1ts /e7imal EKuivalent is:?;>
printf:?&d?=D;>
get7h:;>
E
Output:
Enter the +oman 0umeral:/
1ts /e7imal EKuivalent is:$
Enter the +oman 0umeral:^
1ts /e7imal EKuivalent is:1
Enter the +oman 0umeral:23
1nvalid Oalue
Conclusion: The program is error free
VIVA Q!"A#IO$":
1; 4hat is differen7e betBeen stru7ture and unions H
'ns : The amount of memorJ reKuired to store a stru7ture variable is the sum of size all
the members in addition to the padding bJtes that maJ be provided bJ the 7ompiler8 1n
7ase of a union the amount of memorJ reKuired is the same as that reKuired bJ its largest
member8
2; 4hat are various operations performed on union H
'ns: i;'n union variable 7an be assigned to another union variable
ii; ' union variable 7an be passed to a fun7tion as a parameter
iii; The address of the union variable 7an be extra7ted bJ using the address of
operator :A;8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 11
C & DATA STRUCTURE LAB PROGRAMS
Objective %,:
To read the tBo 7omplex numbers and perform the addition and multipli7ation
of these tBo numbers8
Description:
1n this program the 7omplex number means it 7ontains the tBo parts 8 first one is
real part and se7ond one is imaginarJ part:2+3i;8bJ taDing these tBo 7omplex
numbers Be 7an perform the addition and multipli7ation operation8
Algorithm:
Step 1: Start
Step 2: de7lare stru7ture for 7omplex numbers
Step 3: read the 7omplex number
Step !: read 7hoi7e
Step $: if 7hoi7e=1 then addition operation Bill perform and it 7ontains folloBing
steps
i; B8realpart = B18realpart+B28realpart>
ii; B8imgpart = B18imgpart+B28imgpart> goto step !
Step 6: if 7hoi7e=2 then multipli7ation operation Bill perform and it 7ontains
folloBing steps
i; B8realpart=:B18realpartCB28realpart;G:B18imgpartCB28imgpart;>
ii; B8imgpart=:B18realpartCB28imgpart;+:B18imgpartCB28realpart;> goto
step !
Step #: if 7hoi7e= then exit operation Bill perform
Step *:if B8imgpart9 then print realpart+imgpart else
3rint realpart8
Step F: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 111
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 112
Start
/e7lare stru7ture
+ead
option
1f
option=:add
or mul;
true false
+ead real part of first number
image part of first number
+ead real part of se7ond number
image part of se7ond number
1f
option=ad
d
false
B8realpart=B18realpart+B28realpart
B8imgpart=B18imgpart+B28imgpart
1f
option=mu
l
B8realpart=:B18realpartCB28realpart;
B8imgpart=:B18imgpartCB28imgpart;
1f
B8imgpart9
true
3rint
B8realpart+B8img
part i
false
3rint real
part
Stop
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"math8h9
void arithmeti7:int opern;>
stru7t 7omp
<
double realpart>
double imgpart>
E>
void main:;
<
int opern>
7lrs7r:;>
printf:?@n@n @t@t@tCCCCC N'10 NE0, CCCCC?;>
printf:?@n@n Sele7t Jour option: @n 1 : '//@n 2 : N,.T13.T@n : E^1T @n@n@t@t Enter
Jour 2ption Z [@b@b?;>
s7anf:?&d?=Aopern;>
if:opern92;
<
printf:?invalid option?;>
E
else
<
sBit7h:opern;
<
7ase :
exit:;>
7ase 1:
7ase 2:
arithmeti7:opern;>
default:
main:;>
E
E
get7h:;>
E
void arithmeti7:int opern;
<
stru7t 7omp B1= B2= B>
printf:?@n Enter tBo Momplex 0umbers :x+iJ;:@n +eal 3art of -irst 0umber:?;>
s7anf:?&lf?=AB18realpart;>
printf:?@n 1maginarJ 3art of -irst 0umber:?;>
s7anf:?&lf?=AB18imgpart;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 113
C & DATA STRUCTURE LAB PROGRAMS
printf:?@n +eal 3art of Se7ond 0umber:?;>
s7anf:?&lf?=AB28realpart;>
printf:?@n 1maginarJ 3art of Se7ond 0umber:?;>
s7anf:?&lf?=AB28imgpart;>
sBit7h:opern;
<
(Caddition of 7omplex numberC(
7ase 1:
B8realpart = B18realpart+B28realpart>
B8imgpart = B18imgpart+B28imgpart>
breaD>
(Cmultipli7ation of 7omplex numberC(
7ase 2:
B8realpart=:B18realpartCB28realpart;G:B18imgpartCB28imgpart;>
B8imgpart=:B18realpartCB28imgpart;+:B18imgpartCB28realpart;>
breaD>
E
if :B8imgpart9;
printf:?@n 'nsBer = &lf+&lfi?=B8realpart=B8imgpart;>
else
printf:?@n 'nsBer = &lf&lfi?=B8realpart=B8imgpart;>
get7h:;>
main:;>
E
Output:
CCCCC N'10 NE0, CCCCC
Sele7t Jour option:
1 : '//
2 : N,.T13.T
: E^1T
Enter Jour 2ption Z 1[
Enter tBo Momplex 0umbers :x+iJ;:
+eal 3art of -irst 0umber:2
1maginarJ 3art of -irst 0umber:2
+eal 3art of Se7ond 0umber:2
1maginarJ 3art of Se7ond 0umber:2
'nsBer = !8+!8i
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 11!
C & DATA STRUCTURE LAB PROGRAMS

CCCCC N'10 NE0, CCCCC
Sele7t Jour option:
1 : '//
2 : N,.T13.T
: E^1T
Enter Jour 2ption Z 2[
Enter tBo Momplex 0umbers :x+iJ;:
+eal 3art of -irst 0umber:2
1maginarJ 3art of -irst 0umber:2
+eal 3art of Se7ond 0umber:2
1maginarJ 3art of Se7ond 0umber:2
'nsBer = 8+*8i
CCCCC N'10 NE0, CCCCC
Sele7t Jour option:
1 : '//
2 : N,.T13.T
: E^1T
Enter Jour 2ption Z 3[
invalid option
CCCCC N'10 NE0, CCCCC
Sele7t Jour option:
1 : '//
2 : N,.T13.T
: E^1T
Enter Jour 2ption Z [
Conclusion: The program is error free
VIVA Q!"A#IO$":

1; /efine stru7ture H
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 11$
C & DATA STRUCTURE LAB PROGRAMS
'ns: Stru7ture is amethod for pa7Ding data of different tJpes8 Stru7ture help to organize
7omplex data in a more meaninigful BaJ8
2; 4hat is use of "math8h9 header file H
'ns: 1t is used to a77ess the mathemati7al fun7tions in programs8
Objective %-:
3rogram Bhi7h 7opies one file to another
Description:
1n this program Be have to use the file fun7tions to perform the 7opJ
operation from one file to another file8
Algorithm:
Step 1: Start
Step 2: read 7ommand line arguments
Step 3: 7he7D if no of arguments =3 or not8 1f not print invalid no of arguments
Step !: open sour7e file in read mode
Step $: if 0,.. pointer= then print sour7e file 7an not be open
Step 6: open destination file in Brite mode
Step #: if 0,.. pointer= then print destination file 7an not be open
Step * : read a 7hara7ter from sour7e file and Brite to destination file until E2-
Step F: Mlose sour7e file and destination file
Step 1: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 116
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 11#
Start
+ead arguments
from 7ommand line
1f
arg%
=3
3rint invalid no
of arguments
true
false
-s=fopen:argZ1[=WrW
1f
fs=0,.
.
true
3rint sour7e
file 7an not
be opened
false
-t=fopen:argZ2[=WBW
1f
ft==0,.
.
3rint target file
7an not be opened
-7lose:fs;
false
Mh=fget7:fs;
1f
7h==E2
-
-7lose:fs;
-7lose:ft;
true
false
-put7:7h=ft;
Stop
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude "stdio8h9
6in7lude "7onio8h9
6in7lude "pro7ess8h9
void main:int arg7= 7har CargvZ[;
<
-1.E Cfs=Cft>
7har 7h>
7lrs7r:;>
if:arg7%=3;
<
puts:?1nvalid number of arguments8?;>
exit:;>
E
fs = fopen:argvZ1[=?r?;>
if:fs==0,..;
<
puts:?Sour7e file 7annot be opened8?;>
exit:;>
E
ft = fopen:argvZ2[=?B?;>
if :ft==0,..; (( 7he7D the 7ondition if the file pointer is 0,.. or not
<
puts:?Target file 7annot be opened8?;>
f7lose:fs;>
exit:;>
E
Bhile:1;
<
7h=fget7:fs;>
if :7h==E2-; (( 7he7D the 7ondition if the file is end or not
breaD>
else
fput7:7h=ft;>
E
f7lose:fs;>
f7lose:ft;>
get7h:;>
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 11*
C & DATA STRUCTURE LAB PROGRAMS
Output:
sour7e87
this is sour7e text
ouput87
Mommand line arguments
sour7e87 ouput87
sour7e87
this is sour7e text
ouput87
this is sour7e text
Mommand line arguments
sour7e87
1nvalid number of arguments8
Conclusion: the program is error free
VIVA Q!"A#IO$":
1; 4hat is file H
'ns: The 7olle7tion of alphabets is 7alled file
2; 4hat are the various operations performed on the file H
'ns: fopen:;= fread:;= fBrite:;= f7lose:; et788=
3; 4hat is the use of file pointer H
'ns: The file pointer must be used in subseKuent operations on the file
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 11F
C & DATA STRUCTURE LAB PROGRAMS
Objective %.:
To reverse the first n 7hara7ters in afile
Description:
This program perform the reverse operation of n 7hara7ters in the file
Algorithm:
Step 1: Star
Step 2: read the 7ommand line arguments
Step 3: 7he7D if arguments=3 or not
1f not print invalid no of arguments
Step !: open sour7e file in read mode
Step $: if 0,.. pointer= then print file 7an not be open
Step 6: Store no of 7hars to reverse in D
R= CargvZ2[G!*
Step #: read the item from file stream using fread
Step *: Store 7hars from last position to initial position in another string:temp;
Step F: print the temp string
Step 1: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 12
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 121
Start
+ead 7ommand
line args
1f
arg7%
=3
false
-p=fopen:argvZ1[=Wr
W
3rint invalid
no of args
1f
fp==0,.
.
3rint file 7an
not be
opened
true
false
D=CargvZ2[G!*
n = fread:a=1=D=fp;
aZn[=X@X
len=strlen:a;
1 = lenG1 iS
1 9=
true
SZP[=aZi[
3rint sZP[
Q++
false
SZP+1[=Y@Y
Stop
Flow chart:
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude "stdio8h9
6in7lude "7onio8h9
6in7lude "string8h9
6in7lude "pro7ess8h9
void main:int arg7= 7har CargvZ[;
<
7har aZ1$[>
7har sZ2[>
7har n>
int D>
int P=>
int i>
int len>
-1.E Cfp>
if:arg7%=3;
<
puts:?1mproper number of arguments8?;>
exit:;>
E
fp = fopen:argvZ1[=?r?;>
if:fp == 0,..;
<
puts:?-ile 7annot be opened8?;>
exit:;>
E
D=CargvZ2[G!*>
n = fread:a=1=D=fp;>
aZn[=X@X>
len=strlen:a;>
for:i=lenG1>i9=>iGG;
<
sZP[=aZi[>
printf:?&7?=sZP[;>
P=P+1>
E
sZP+1[=X@X>
get7h:;>
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 122
C & DATA STRUCTURE LAB PROGRAMS
Output:
sour7e87
this is sour7e
ouput87
Mommand line arguments
sour7e87 ouput87
sour7e87
this is sour7e
e7ruos si siht
Mommand line arguments
sour7e87
1nvalid number of arguments8
Conclusion: the program is error free
VIVA Q!"A#IO$":
1; .ist out the file handling fun7tions H
'ns: fopen:;= fprintf:;=f7lose:;=fs7anf:;=fget7:;=fput7:;= et788=
2; 4hat is the use of fseeD:; fun7tion H
'ns: The fun7tion fseeD sets the file pointer asso7iated Bith a stream to a neB position
3; 4hat is use of the fflush:; fun7tion H
'ns: 1f the given stream has a buffered output= fflush Brites the output of the stream to
the asso7iate file8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 123
C & DATA STRUCTURE LAB PROGRAMS
Objective %/:
3rogram that uses fun7tions to perform the folloBing operations on single linDed
lists8 i; 7reation ii; insertion iii; deletion iv; traversal
Description:
1n this program Be have to 7reate a single linDed list= insert the elements into that
list =delete the some elements from that list and then perform the sorting operation
and traversal operation on that 7reated linDedlist
Algorithm :
Step 1: Start
Step 2: /e7lare a stru7ture named linDedGlist
Step 3: /e7lare the pointers next= first= fresh= ptr
Step !: 3rint main menu
Step $: +ead 7hoi7e
Step 6: SBit7h:7hoi7e;
Step #: 1f:7hoi7e==1;
#81 'ssign fresh=mallo7:size of :node;;
#82 +ead the element freshG9data
#83 +ead the 7hoi7e Bhere to insert
#8!:SBit7h:7hoi7e;
#8!81: 1f 7hoi7e==1
#88!82: Mall the fun7tion 1Iegin:;
#8!83: 1f 7hoi7e==2
#8!8!: Mall the fun7tion 1end:;
#8!8$: 1f 7hoi7e==3
#8!86: Mall the fun7tion 1middle:;
Step *: 1f:7hoi7e==2;
*81: +ead the position to delete
*82: SBit7h:7hoi7e;
*8281: 1f 7hoi7e==1
*88282: Mall the fun7tion /Iegin:;
*8283: 1f 7hoi7e==2
*828!: Mall the fun7tion /end:;
*828$: 1f 7hoi7e==3
*8286: Mall the fun7tion /middle:;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 12!
C & DATA STRUCTURE LAB PROGRAMS
Step F: 1f 7hoi7e==3
F81 Mall fun7tion vieB
Step 1: 1f 7hoi7e==!
181 Exit:;
Step 11: Start insert fun7tion
Step 12: 1f:first==null;
Step 13: -irstG9data=e
Step 1!: -irstG9next=null
Step 1$: Else de7lare neB node
Step 16:freshG9data=e
Step 1#: 1f 7hoi7e=1
Step 1*: frshG9next=first
Step 1F: first=fresh
Step 2:if 7hoi7e=2
Step 21: ptr=first
Step 22: ptrG9next=fresh
Step 23: freshG9next=full
Step 2!: 1f 7hoi7e =3
Step 2$: Enter the position
Step 26:at pG1 node
Step 2#: freshG9next= ptrG9next
Step 2*: ptrG9next=fresh
Step 2F: for delete fun7tion
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 12$
C & DATA STRUCTURE LAB PROGRAMS
Step 3: 1f first%=null
Step 31: Enter the position to delete
Step 32: 1f 7hoi7e=1
Step 33: d=firstG9data
Step 3!: first=firstG9next
Step 3$: if 7hoi7e=2
Step 36: ptr=first
Step 3#: Traverse to last node
Step 3*: d=ptrG9nextG9data
Step 3F: ptr G9next=ptrG9nextG9next
Step !: 3rint d value
Step !1: for fun7tion vieB
Step !2: for ptr=first and ptr%=null and ptr=ptrG9next
Step !3: 3rint ptrG9data
Step !!: End
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 126
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 12#
C & DATA STRUCTURE LAB PROGRAMS
Program:

6 in7lude"stdio8h9
6 in7lude"mallo78h9
int 7h=i=n=P=p=item> (C O'+1'I.E /EM.'+'T120 C(
(C ST'+T 2- ST+,MT,+E /E-101T120 C(
stru7t linD
<
int data>
stru7t linD Cnext>
ECstart=CneB=Cl=Cl1=Cstart1=Ct>
(C E0/ 2- ST+,MT,+E /E-101T120 C(
(C ST'+T 2- N'10 -,0MT120 C(
main:;
<
7lrs7r:;>
start=0,..>
start1=0,..>
printf:? CCCC NE0, CCCC ?;>
printf:?@n 181nsertion@n 28/eletion@n 38Traverse@n !8Sear7h@n $8Sort@n 68Nerge@n
#8+everse@n?;>
Bhile:1;
<
printf:?enter the 7hoi7e:?;>
s7anf:?&d?=A7h;>
sBit7h:7h;
<
7ase 1: insert:;>
breaD>
7ase 2: delete:;>
breaD>
7ase 3: traverse:;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 12*
C & DATA STRUCTURE LAB PROGRAMS
breaD>
7ase !: sear7h:;>
breaD>
7ase $: sort:;>
breaD>
7ase 6: merge:;>
breaD>
7ase #: reverse:;>
breaD>
7ase *:exit:;>
E
E
get7h:;>
E
(C E0/ 2- N'10 -,0MT120 C(
(C ST'+T 2- 10SE+T -,0MT120 C(
insert:;
<
l=start>
printf:?enter the item to be inserted:?;>
s7anf:?&d?=Aitem;>
neB=mallo7:sizeof:stru7t linD;;>
neBG9data=item>
if:start==0,..;
<
neBG9next=0,..>
start=neB>
E
else
<
printf:?18start@n28middle@n38end@n?;>
printf:?enter the pla7e to pla7e the item:?;>
s7anf:?&d?=A7h;>
if:7h==1;
<
neBG9next=start>
start=neB>
E
if:7h==2;
<
printf:?enter the position to pla7e item:?;>
s7anf:?&d?=Ap;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 12F
C & DATA STRUCTURE LAB PROGRAMS
for:i=1>i"pG1>i++;
l=lG9next>
neBG9next=lG9next>
lG9next=neB>
E
if:7h==3;
<
Bhile:lG9next%=0,..;
l=lG9next>
neBG9next=0,..>
lG9next=neB>
E
E
E
(C E0/ 2- 10SE+T -,0MT120 C(
(C ST'+T 2- /1S3.'T -,0MT120 C(
traverse:;
<
if:start==0,..;
printf:?.1ST 1S EN3TT@n?;>
else
<
for:l=start>lG9next%=0,..>l=lG9next;
if:l==start;
printf:?@nstart:&dG9?=lG9data;>
else
printf:?@n&#dG9?=lG9data;>
if:lG9next==0,..;
printf:?@n last:&dG9@n?=lG9data;>
E
E
(C E0/ 2- /1S3.'T -,0MT120 C(
(C ST'+T 2- /E.ETE -,0MT120 C(
delete:;
<
l=start>
if:start==0,..;
printf:?02 1TENS 10 T5E .1ST@n?;>
else
<
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 13
C & DATA STRUCTURE LAB PROGRAMS
printf:?18start@n28middle@n38end@n?;>
printf:?enter the pla7e to delete the item:?;>
s7anf:?&d?=A7h;>
if:7h==1;
<
item=startG9data>
printf:?deleted item is:&d@n?=item;>
start=startG9next>
E
if:7h==2;
<
printf:?enter the position to delete item:?;>
s7anf:?&d?=Ap;>
if:lG9next==0,..;
<
item=lG9data>
printf:?deleted item is:&d@n?=item;>
l=start=0,..>
E
else
<
for:i=1>i"pG1>i++;
l=lG9next>
item=lG9nextG9data>
printf:?deleted item is:&d@n?=item;>
lG9next=lG9nextG9next>
E
E
if:7h==3;
<
if:lG9next==0,..;
<
item=lG9data>
printf:?deleted item is:&d@n?=item;>
l=start=0,..>
E
else
<
Bhile:lG9nextG9next%=0,..;
l=lG9next>
item=lG9nextG9data>
printf:?deleted item is:&d@n?=item;>
lG9next=0,..>
l=lG9next>
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 131
C & DATA STRUCTURE LAB PROGRAMS
E
E
E
(C E0/ 2- /E.ETE -,0MT120 C(
(C ST'+T 2- SE'+M5 -,0MT120 C(
sear7h:;
<
int f=>
printf:?enter the sear7h item:?;>
s7anf:?&d?=Aitem;>
if:start==0,..;
printf:?.1ST 1S EN3TT?;>
else
<
for:l=start=i=1>l%=0,..>l=lG9next=i++;
if:lG9data==item;
<
f=1>
breaD>
E
if:f==1;
printf:?item &d found at position :&d@n?=item=i;>
else
printf:?item &d not found@n?=item;>
E
E
(C E0/ 2- SE'+M5 -,0MT120 C(
(C ST'+T 2- S2+T -,0MT120 C(
sort:;
<
int t>
if:start==0,..;
printf:?.1ST 1S EN3TT?;>
else
<
for:l1=start>l1G9next%=0,..>l1=l1G9next;
<
for:l=start>lG9next%=0,..>l=lG9next;
if:lG9data 9 lG9nextG9data;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 132
C & DATA STRUCTURE LAB PROGRAMS
<
t=lG9data>
lG9data=lG9nextG9data>
lG9nextG9data=t>
E
E
printf:?T5E S2+TE/ 2+/E+ 1S:?;>
for:l=start>l%=0,..>l=lG9next;
printf:?&3d?=lG9data;>
E
printf:?@n?;>
E
(C E0/ 2- S2+T -,0MT120 C(
(C ST'+T 2- NE+)E -,0MT120 C(
merge:;
<
printf:?enter no of elements to be inserted in se7ond list :?;>
s7anf:?&d?=An;>
for:P=1>P"=n>P++;
<
l1=start1>
printf:?enter the item to be inserted:?;>
s7anf:?&d?=Aitem;>
neB=mallo7:sizeof:stru7t linD;;>
neBG9data=item>
neBG9next=0,..>
if:start1==0,..;
start1=neB>
else
<
printf:?18start@n28middle@n38end@n?;>
printf:?enter the pla7e to pla7e the item:?;>
s7anf:?&d?=A7h;>
if:7h==1;
<
neBG9next=start1>
start1=neB>
E
if:7h==2;
<
printf:?enter the position to pla7e item:?;>
s7anf:?&d?=Ap;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 133
C & DATA STRUCTURE LAB PROGRAMS
for:i=1>i"pG1>i++;
l1=l1G9next>
neBG9next=l1G9next>
l1G9next=neB>
E
if:7h==3;
<
Bhile:l1G9next%=0,..;
l1=l1G9next>
l1G9next=neB>
E
E
E
if:start==0,..;
start=start1>
else
<
l=start>
Bhile:lG9next%=0,..;
l=lG9next>
for:l1=start1>l1G9next%=0,..>l1=l1G9next;
<
lG9next=l1>
l=lG9next>
E
E
printf:? CCC .1ST 1S NE+)E/ CCC @n?;>
E
(C E0/ 2- NE+)E -,0MT120 C(
(C ST'+T 2- +EOE+SE -,0MT120 C(
reverse:;
<
if:start==0,..;
printf:?.1ST 1S EN3TT@n?;>
else
<
l=start>
l1=t=0,..>
Bhile:l%=0,..;
<
l1=t>
t=l>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 13!
C & DATA STRUCTURE LAB PROGRAMS
l=lG9next>
tG9next=l1>
E
start=t>
printf:? CCC .1ST 1S +EOE+SE/ CCC@n?;>
E
E
(C E0/ 2- +EOE+SE -,0MT120 C(
CCCCC 2,T3,T CCCCC
CCCC NE0, CCCC
181nsertion
28/eletion
38Traverse
!8Sear7h
$8Sort
68Nerge
#8+everse
enter the 7hoi7e:1
enter the item to be inserted:1
enter the 7hoi7e:1
enter the item to be inserted:2
18start
28middle
38end
enter the pla7e to pla7e the item:1
enter the 7hoi7e:1
enter the item to be inserted:3
18start
28middle
38end
enter the pla7e to pla7e the item:3
enter the 7hoi7e:1
enter the item to be inserted:!
18start
28middle
38end
enter the pla7e to pla7e the item:2
enter the position to pla7e item:3
enter the 7hoi7e:3
start:2G9
1G9
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 13$
C & DATA STRUCTURE LAB PROGRAMS
!G9
last:3G9
enter the 7hoi7e:!
enter the sear7h item:!
item ! found at position :3
enter the 7hoi7e:6
enter no of elements to be inserted in se7ond list :3
enter the item to be inserted:$
enter the item to be inserted:6
18start
28middle
38end
enter the pla7e to pla7e the item:1
enter the item to be inserted:#
18start
28middle
38end
enter the pla7e to pla7e the item:2
enter the position to pla7e item:2
CCC .1ST 1S NE+)E/ CCC
enter the 7hoi7e:3
start:2G9
1G9
!G9
3G9
6G9
#G9
last:$G9
enter the 7hoi7e:#
CCC .1ST 1S +EOE+SE/ CCC
enter the 7hoi7e:3
start:$G9
#G9
6G9
3G9
!G9
1G9
last:2G9
enter the 7hoi7e:!
enter the sear7h item:1
item 1 found at position :6
enter the 7hoi7e:$
T5E S2+TE/ 2+/E+ 1S: 1 2 3 ! $ 6 #
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 136
C & DATA STRUCTURE LAB PROGRAMS
enter the 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:1
deleted item is:1
enter the 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:3
deleted item is:#
enter the 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:2
enter the position to delete item:!
deleted item is:$
enter the 7hoi7e:3
start:2G9
3G9
!G9
last:6G9
enter the 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:1
deleted item is:2
enter the 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:2
enter the position to delete item:2
deleted item is:!
enter the 7hoi7e:3
start:3G9
last:6G9
enter the 7hoi7e:2
18start
28middle
38end
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 13#
C & DATA STRUCTURE LAB PROGRAMS
enter the pla7e to delete the item:2
enter the position to delete item:2
deleted item is:6
enter the 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:1
deleted item is:3
enter the 7hoi7e:3
.1ST 1S EN3TT
enter the 7hoi7e:2
02 1TENS 10 T5E .1ST
enter the 7hoi7e:*
conclusion: the program is error free
VIVA Q!"A#IO$":
1; .ist out the memorJ allo7ation fun7tions H
'ns: mallo7:;= 7allo7:;=free:;= reallo7:; et788=
2; /efine linDed list H
'ns: .inDed list is list Bhose order is given bJ linDs from one item to the next
3; .ist out the advantages of linDed list H
Ans: i; /Janami7 data stru7ture
ii; no Baste memorJ spa7e
iii; flexibilitJ
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 13*
C & DATA STRUCTURE LAB PROGRAMS
Objective )0:
3rogram that uses fun7tions to perform the folloBing operations on doublJ
linDed list i; 7reation ii; insertion iii; deletion iv; traversal
Description : 1n this program Be have to 7reate a doublJ linDed list= insert the
elements in to a doublJ linDed list= delete the elements from that list and finallJ
perform the traversal operation
A67O8I#29 :
Step 1: Start
Step 2: /e7lare a stru7ture Bith Cnext= Cpre
Step 3: /e7lare Cstart= CneB =Cl as stru7ture pointers
Step !: 3rint main menu
Step $: +ead 7hoi7e
Step 6: SBit7h 7hoi7e
681: 7all insert fun7tion if 7hoi7e==1
682: 7all delete fun7tion if 7hoi7e==2
683: 7all vieB fun7tion if 7hoi7e==3
Step #: Stop
Step *: Start of insert fun7tion
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 13F
C & DATA STRUCTURE LAB PROGRAMS
Step F: +ead e
Step 1: 1f start==null
Step 11: Mreate a neB node
Step 12: StartG9data=e
Step 13: StartG9next=null
Step 1!: StartG9pre=null
Step 1$: read 7hoi7e= Bhere to insert
Step 16: if 7hoi7e==1

Step 1681: Mreate a neB mode

Step 1682: neB G9 data=e

Step 1683: neB G9 next=start
Step 168!: startG9pre=neB
Step 168$: neBG9pre=null
Step 1686: StartG9neB
Step 1#: otherBise if 7hoi7e==2
1#81: read position p
1#82: l=start
1#83: Bhile i":pG1;
1#8!: in7rent i
1#8$: l=lG9next
1#86: neB G9 data =e
1#8#: neB G9 pre=l
1#8*: neBG9next=neB
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!
C & DATA STRUCTURE LAB PROGRAMS
1#8F: lG9 next=neB
1#81: lG9nextG9pre=neB
Step 1*: if 7hoi7e==3
1*81: l=start
1*82: Bhile lG9next%=null
1*83: l=lG9next
1*8!: 7reate a neB mode
1*8$: neBG9data=e
1*86: neBG9next=null
1*8#: lG9next=neB
1*8*: neBG9pre=l
Step1F: end of insert fun7tion
Step2: start of deletion fun7tion
Step21: Brite menu
Step22: read 7hoi7e
Step23: if 7hoi7e==1
2381: temp=startG9data
2382: start=startG9next
2383: startG9pre=null
Step2!: if 7hoi7e==2
2!81: read position
2!82: l=start
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!1
C & DATA STRUCTURE LAB PROGRAMS
2!83: Bhile :i=1 "pG1;
2!8!: l=lG9next
2!8$: in7rement 1 bJ 1
2!86: temp=lGnextG9data
2!8#: lG9next=lG9nextG9next
2!8*: lG9nextG9pre=l
Step2$: if 7hoi7e==3
2$81: read l=start
2$82: Bhile lG9nextG9next%= null
2$83: l=lG9next
2$8!: temp=lG9nextG9data
2$8$: lG9next=null
Step26: end of delete fun7tion
Step2#: start of vieB fun7tion
Step2*: read 7hoi7e
Step2F: if 7hoi7e==1
2F81: l=next
2F82: Bhile :lG9next%= null;
2F83: Brite lG9 data= l=lG9next
2F8!: Brite lG9data
Step3: if 7hoi7e==2
381: l=start
382: Bhile l%=start
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!2
C & DATA STRUCTURE LAB PROGRAMS
383: Brite lG9data
38!: l=lG9pre
38$: Brite lG9data
Step31: end of fun7tion vieB
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!3
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"mallo78h9
(C ST'+T 2- ST+,MT,+E /E-101T120 C(
stru7t linD
<
int data>
stru7t linD Cnext>
stru7t linD Cprev>
ECstart=CneB=Ctemp=Cl=Cl1=Ct=Cstart1>
(C E0/ 2- ST+,MT,+E /E-101T120 C(
int item=7h=i=P=p=n> (C O'+1'I.E /EM.'+'T120 C(
(C ST'+T 2- N'10 -,0MT120 C(
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!!
C & DATA STRUCTURE LAB PROGRAMS
main:;
<
start=0,..>
start1=0,..>
7lrs7r:;>
printf:? CCCC NE0, CCCC?;>
printf:?@n181nsertion@n28/eletion@n38Traverse@n!8sear7h@n$8sort@n68merge@n
#8reverse@n*8exit@n?;>
Bhile:1;
<
printf:?enter Jour 7hoi7e:?;>
s7anf:?&d?=A7h;>
sBit7h:7h;
<
7ase 1:insert:;>
breaD>
7ase 2:delete:;>
breaD>
7ase 3:displaJ:;>
breaD>
7ase !:sear7h:;>
breaD>
7ase $:sort:;>
breaD>
7ase 6:merge:;>
breaD>
7ase #:reverse:;>
breaD>
7ase *:exit:;>
E
E
get7h:;>
E
(C E0/ 2- N'10 -,0MT120 C(
(C ST'+T 2- 10SE+T -,0MT120 C(
insert:;
<
l=start>
printf:?enter an item to be inserted:?;>
s7anf:?&d?=Aitem;>
neB=mallo7:sizeof:stru7t linD;;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!$
C & DATA STRUCTURE LAB PROGRAMS
neBG9data=item>
if:start==0,..;
<
neBG9prev=0,..>
neBG9next=0,..>
start=neB>
E
else
<
printf:?18start@n28middle@n38end@n?;>
printf:?enter the pla7e to insert item:?;>
s7anf:?&d?=A7h;>
if:7h==1;
<
neBG9next=start>
neBG9prev=0,..>
start=neB>
E
if:7h==2;
<
printf:?enter the position to pla7e item:?;>
s7anf:?&d?=Ap;>
for:i=1>i"pG1>i++;
l=lG9next>
neBG9prev=l>
neBG9next=lG9next>
lG9next=neB>
E
if:7h==3;
<
Bhile:lG9next%=0,..;
l=lG9next>
neBG9prev=l>
neBG9next=0,..>
lG9next=neB>
E
E
E
(C E0/ 2- 10SE+T -,0MT120 C(
(C ST'+T 2- /E.ETE -,0MT120 C(
delete:;
<
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!6
C & DATA STRUCTURE LAB PROGRAMS
l=start>
if:start==0,..;
printf:?CCC .1ST 1S EN3TT CCC?;>
else
<
printf:?18start@n28middle@n38end?;>
printf:?enter the pla7e to delete the item:?;>
s7anf:?&d?=A7h;>
if:7h==1;
<
item=startG9data>
printf:?deleted item is :&d?=item;>
start=startG9next>
startG9prev=0,..>
E
if:7h==2;
<
printf:?enter the position to delete an item:?;>
s7anf:?&d?=Ap;>
if:lG9next==0,..;
<
item=lG9data>
printf:?deleted item is:&d?=item;>
l=start=0,..>
E
else
<
for:i=1>i"pG1>i++;
l=lG9next>
item=lG9nextG9data>
printf:?deleted item is:&d?=item;>
lG9next=lG9nextG9next>
lG9nextG9prev=l>
E
E
if:7h==3;
<
if:lG9next==0,..;
<
item=lG9data>
printf:?deleted item is :&d?=item;>
lG9prev=0,..>
l=start=0,..>
E
else
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!#
C & DATA STRUCTURE LAB PROGRAMS
<
Bhile:lG9nextG9next%=0,..;
l=lG9next>
item=lG9nextG9data>
printf:?deleted item is:&d?=item;>
lG9next=0,..>
E
E
E
E
(C E0/ 2- /E.ETE -,0MT120 C(
(C ST'+T 2- /1S3.'T -,0MT120 C(
displaJ:;
<
if:start==0,..;
printf:?CCC .1ST 1S EN3TT CCC@n?;>
else
<
for:l=start>lG9next%=0,..>l=lG9next;
if:l==start;
printf:?@nstart:&d?=lG9data;>
else
printf:?@n &*d?=lG9data;>
if:lG9next==0,..;
printf:?@n last:&d?=lG9data;>
E
E
(C E0/ 2- /1S3.'T -,0MT120 C(
(C ST'+T 2- SE'+M5 -,0MT120 C(
sear7h:;
<
int f=>
if:start==0,..;
printf:? CCC .1ST 1S EN3TT CCC ?;>
else
<
printf:?enter the sear7h item:?;>
s7anf:?&d?=Aitem;>
for:l=start=i=1>l%=0,..>l=lG9next=i++;
if:item==lG9data;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!*
C & DATA STRUCTURE LAB PROGRAMS
<
f=1>
breaD>
E
if:f==1;
printf:?item &d found at position &d?=item=i;>
else
printf:?item &d not found in list?=item;>
E
E
(C E0/ 2- SE'+M5 -,0MT120 C(
(C ST'+T 2- S2+T -,0MT120 C(
sort:;
<
int t>
if:start==0,..;
printf:? CCC .1ST 1S EN3TT CCC ?;>
else
<
for:l1=start>l1G9next%=0,..>l1=l1G9next;
for:l=start>lG9next%=0,..>l=lG9next;
if:lG9data 9 lG9nextG9data;
<
t=lG9nextG9data>
lG9nextG9data=lG9data>
lG9data=t>
E
printf:?T5E S2+TE/ 2+/E+ 1S:?;>
for:l=start>l%=0,..>l=lG9next;
printf:?&3d?=lG9data;>
E
printf:?@n?;>
E
(C E0/ 2- S2+T -,0MT120 C(
(C ST'+T 2- NE+)E -,0MT120 C(
merge:;
<
printf:?enter number items to be inserted in se7ond list:?;>
s7anf:?&d?=An;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1!F
C & DATA STRUCTURE LAB PROGRAMS
for:P=1>P"=n>P++;
<
l1=start1>
printf:?enter an item:?;>
s7anf:?&d?=Aitem;>
neB=mallo7:sizeof:stru7t linD;;>
neBG9data=item>
if:start1==0,..;
<
neBG9prev=0,..>
neBG9next=0,..>
start1=neB>
E
else
<
printf:?18start@n28middle@n38end@n?;>
printf:?enter the pla7e to insert item:?;>
s7anf:?&d?=A7h;>
if:7h==1;
<
neBG9next=start1>
neBG9prev=0,..>
start1=neB>
E
if:7h==2;
<
printf:?enter the position to pla7e item:?;>
s7anf:?&d?=Ap;>
for:i=1>i"pG1>i++;
l1=l1G9next>
neBG9prev=l1>
neBG9next=l1G9next>
l1G9next=neB>
E
if:7h==3;
<
Bhile:l1G9next%=0,..;
l1=l1G9next>
neBG9prev=l1>
neBG9next=0,..>
l1G9next=neB>
E
E
E
if:start==0,..;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$
C & DATA STRUCTURE LAB PROGRAMS
start=start1>
else
<
l=start>
Bhile:lG9next%=0,..;
l=lG9next>
for:l1=start1>l1G9next%=0,..>l1=l1G9next;
<
lG9next=l1>
l=lG9next>
E
E
printf:? CCC .1ST 1S NE+)E/ CCC @n?;>
E
(C E0/ 2- NE+)E -,0MT120 C(
(C ST'+T 2- +EOE+SE -,0MT120 C(
reverse:;
<
if:start==0,..;
printf:? CCC .1ST 1S EN3TT CCC@n ?;>
else
<
l=start>
l1=t=0,..>
Bhile:l%=0,..;
<
l1=t>
t=l>
l=lG9next>
tG9next=l1>
E
start=t>
printf:? CCC .1ST 1S +EOE+SE/ CCC @n?;>
E
E
(C E0/ 2- +EOE+SE -,0MT120 C(
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$1
C & DATA STRUCTURE LAB PROGRAMS
Input4Output:
CCCC NE0, CCCC
181nsertion
28/eletion
38Traverse
!8sear7h
$8sort
68merge
#8reverse
*8exit
enter Jour 7hoi7e:1
enter an item to be inserted:1
enter Jour 7hoi7e:1
enter an item to be inserted:2
18start
28middle
38end
enter the pla7e to insert item:1
enter Jour 7hoi7e:1
enter an item to be inserted:3
18start
28middle
38end
enter the pla7e to insert item:3
enter Jour 7hoi7e:1
enter an item to be inserted:!
18start
28middle
38end
enter the pla7e to insert item:2
enter the position to pla7e item:3
enter Jour 7hoi7e:1
enter an item to be inserted:$
18start
28middle
38end
enter the pla7e to insert item:2
enter the position to pla7e item:2
enter Jour 7hoi7e:3
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$2
C & DATA STRUCTURE LAB PROGRAMS
start: 2
$
1
!
last: 3
enter Jour 7hoi7e:6
enter number items to be inserted in se7ond list:3
enter an item:6
enter an item:#
18start
28middle
38end
enter the pla7e to insert item:3
enter an item:*
18start
28middle
38end
enter the pla7e to insert item:1
CCC .1ST 1S NE+)E/ CCC
enter Jour 7hoi7e:3
start:2
$
1
!
3
*
6
last:#
enter Jour 7hoi7e:!
enter the sear7h item:*
item * found at position 6
enter Jour 7hoi7e:!
enter the sear7h item:1
item 1 found at position 3
enter Jour 7hoi7e:#
CCC .1ST 1S +EOE+SE/ CCC
enter Jour 7hoi7e:3
start:#
6
*
3
!
1
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$3
C & DATA STRUCTURE LAB PROGRAMS
$
last: 2
enter Jour 7hoi7e:$
T5E S2+TE/ 2+/E+ 1S: 1 2 3 ! $ 6 # *
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:1
deleted item is :1
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:3
deleted item is:*
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:2
enter the position to delete an item:3
deleted item is:!
enter Jour 7hoi7e:3
start:2
3
$
6
last: #
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:2
enter the position to delete an item:!
deleted item is:6
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:!
enter Jour 7hoi7e:3
start:2
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$!
C & DATA STRUCTURE LAB PROGRAMS
3
$
last: #
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:2
enter the position to delete an item:3
deleted item is:$
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:2
enter the position to delete an item:3
deleted item is:$
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:2
enter the position to delete an item:1
deleted item is:3
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:1
deleted item is :2
enter Jour 7hoi7e:3
last:#
enter Jour 7hoi7e:2
18start
28middle
38end
enter the pla7e to delete the item:1
deleted item is :#
enter Jour 7hoi7e:3
CCC .1ST 1S EN3TT CCC
enter Jour 7hoi7e:2
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$$
C & DATA STRUCTURE LAB PROGRAMS
CCC .1ST 1S EN3TT CCC
enter Jour 7hoi7e:*
conclusion: the program is error free
VIVA Q!"A#IO$":
1& .ist out the Jpes of linDed lists H
'ns: i; 7ir7ular linDed lists ii; doublJ linDed lists= iii; 7ir7ular doublJ linDed list
2; 4hat are the various operations performed on the linDed lists H
'ns: i; 7reating a list= ii; traversing the list iii; inserting an item et788=
3; 'nother name for doublJ linDed list H
'ns: tBoGBaJ linDed list8
Objective )1:
3rogram that implement sta7D and its operation bJ using the arraJs
Description:
1n this program Be have to implement the sta7D operation bJ using the
arraJs8 5ere theJ sta7D operation are push and pop8 3ush operation is used
to insert the elements into a sta7D and pop operation is used to remove the
elements in to a sta7D
A67O8I#29 FO8 I$"!8#I$7 A$ !6!9!$# I$ A "#AC::
-un7tion 3ush:s=top=x;
Step 1: ZMhe7D for sta7D overfloB[
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$6
C & DATA STRUCTURE LAB PROGRAMS
1f top9=n
Then printf:Vsta7D overfloBW;
+eturn
Step 2: Z1n7rement Top[
Top"Gtop+1
Step 3: Z 1nsert element[
SZtop["Gx
Step !:Zfinished[
+eturn
A67O8I#29 FO8 D!6!#I$7 A$ !6!9!$# F8O9 A "#AC::
-un7tion 323:s=top;
Step 1: ZMhe7D for sta7D underfloB[
1f top=
Then printf:Vsta7D underfloBW;
Exit
Step 2: Z/e7rement Top[
Top"GtopG1
Step 3: Z+eturn former top element of sta7DBBBBB[
+eturn:SZtop+1[;
Step !:Zfinished[
+eturn
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$#
C & DATA STRUCTURE LAB PROGRAMS
Program:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$*
C & DATA STRUCTURE LAB PROGRAMS
6 in7lude "stdio8h9
6 define size !
int 7hoi7e=top==aZsize[=item>
main:;
<
7lrs7r:;>
Bhile:1;
<
printf:? CCC NE0, CCC@n 18 3,S5@n 28 323@n 38
T+'OE+SE@n !8 E^1T@n?;>
printf:?enter Jour 7hoi7e from menu:?;>
s7anf:?&d?=A7hoi7e;>
sBit7h:7hoi7e;
<
7ase 1:push:;>
breaD>
7ase 2:pop:;>
breaD>
7ase 3:traverse:;>
breaD>
7ase !:exit:;>
default:printf:?Brong 7hoi7e@n?;>
E
E
get7h:;>
E
push:;
<
if:size==top;
printf:?CCC sta7D is full CCC@n?;>
else
<
printf:?enter the item to be pushed into the sta7D:?;>
s7anf:?&d?=Aitem;>
top++>
aZtop[=item>
E
E
pop:;
<
if:top==;
printf:?CCC sta7D is emptJ CCC@n?;>
else
<
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1$F
C & DATA STRUCTURE LAB PROGRAMS
item=aZtop[>
topGG>
printf:?the deleted item from sta7D is &d@n?=item;>
E
E
traverse:;
<
int i>
if:top==;
printf:?CCCC sta7D is emptJ CCCC?;>
else
<
printf:?CCC sta7D displaJ CCC@n?;>
for:i=1>i"=top>i++;
if:i==top;
printf:?&d at &d G9top@n?=aZi[=i;>
else
printf:?&d at &d@n?=aZi[=i;>
E
E
Input4Output:
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:1
enter the item to be pushed into the sta7D:11
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:1
enter the item to be pushed into the sta7D:12
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:1
enter the item to be pushed into the sta7D:13
CCC NE0, CCC
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 16
C & DATA STRUCTURE LAB PROGRAMS
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:1
enter the item to be pushed into the sta7D:1!
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:1
CCC sta7D is full CCC
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:3
CCC sta7D displaJ CCC
11 at 1
12 at 2
13 at 3
1! at ! G9top
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:2
the deleted item from sta7D is 1!
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:2
the deleted item from sta7D is 13
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:2
the deleted item from sta7D is 12
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 161
C & DATA STRUCTURE LAB PROGRAMS
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:2
the deleted item from sta7D is 11
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:2
CCC sta7D is emptJ CCC
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:3
CCCC sta7D is emptJ CCCC
CCC NE0, CCC
18 3,S5
28 323
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e from menu:!
conclusion: the program is error free
VIVA Q!"A#IO$":
1& /efine Sta7D H
'ns: ' sta7D is a linear data stru7ture in Bhi7h a data item is inserted and deleted at one
end
2; /efine data stru7ture H
Ans: ' data stru7ture is a 7olle7tion of organized data that are related to ea7h other
)& 4hat are the various operation performed on the sta7D H
'ns: push:;= pop:;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 162
C & DATA STRUCTURE LAB PROGRAMS
Objective )%:
3rogram that implement sta7D operations bJ using the pointers8
Description:
1n this program Be have to implement the sta7D operation bJ using the pointers8
5ere theJ sta7D operation are push and pop8 3ush operation is used to insert the
elements into a sta7D and pop operation is used to remove the elements in to a
sta7D8
Algorithm:
Step 1: Start
Step 2: /e7lare the stru7ture for the sta7D pointers8
Step 3: /efine the push fun7tion
Step !: /efine the pop fun7tion
Step $: /efine the displaJ fun7tion
Step 6: +ead the 7hoi7e
Step #: if 7hoi7e = push
Mreate a 7ell for the T23 7ell in the sta7D8
3la7e the date in the T23 7ell
3la7e the T23 pointer to the neB 7ell
Step *: if 7hoi7e=pop
Mhe7D if emptJ sta7D8 1f so= print sta7D is emptJ8
2therBise= remove the T23 7ell8
Step F: if 7hoi7e=displaJ
/isplaJ all the elements in the Sta7D8
Step 1: Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 163
C & DATA STRUCTURE LAB PROGRAMS

VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 16!
Start
/efine stru7ture for st_point
pointer
/efine push: ;
/efine pop: ;
/efine displaJ : ;
+ead 7hoi7e=
num1==num2=
4hile:1
;
+ead 7hoi7e
SBit7h 7hoi7e
3ush: ; 3op: ; /isplaJ: ; Exit: ;
breaD breaD breaD breaD
stop
Flowchart:
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 16$
Start
/efine stru7ture for st_point
pointer
/efine push: ;
/efine pop: ;
/efine displaJ : ;
+ead 7hoi7e=
num1==num2=
4hile:1
;
+ead 7hoi7e
SBit7h 7hoi7e
3ush: ; 3op: ; /isplaJ: ; Exit: ;
breaD breaD breaD breaD
stop
Flowchart:
C & DATA STRUCTURE LAB PROGRAMS

VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 166
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 16#
3,S5: ;
+ead
element=st_point
er
N=:stru7t st_pointC;mallo7:sizeof:stru7t st_point;;
N G9ele7=P
N G9 l=t
T=m
+eturn to main
program
3op : ;
1f
t=0,..
1 = tG9ele
T =t G9 l
+eturn i
3rint sta7D is
emptJ
+eturn to main
program
-
T
C & DATA STRUCTURE LAB PROGRAMS

Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
stru7t st_point
<
int ele>
stru7t st_point Cl>
E
Ct>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 16*
/isplaJ : ;
/efine st_point C pointer= 0,..
3ointer = t
4hile:pointer%
=
0,..;
3rint the element
3ointer=pointerG91
+eturn to main program
true
C & DATA STRUCTURE LAB PROGRAMS
int i>
void push_ele:int P;>
int pop_ele:;>
void displaJ_ele:;>
void main:;
<
7har 7hoi7e=num1==num2=>
int i>
Bhile:1;
<
7lrs7r:;>
printf:?======================================?;>
printf:?@n@t@t NE0, ?;>
printf:?@n======================================?;>
printf:?@nZ1[ ,sing 3ush -un7tion?;>
printf:?@nZ2[ ,sing 3op -un7tion?;>
printf:?@nZ3[ Elements present in Sta7D?;>
printf:?@nZ![ Exit@n?;>
printf:?@n@tEnter Jour 7hoi7e: ?;>
fflush:stdin;>
s7anf:?&7?=A7hoi7e;>
sBit7h:7hoi7eGXX;
<
7ase 1:
<
printf:?@n@tElement to be pushed:?;>
s7anf:?&d?=Anum1;>
push_ele:num1;>
breaD>
E
7ase 2:
<
num2=pop_ele:1;>
printf:?@n@tElement to be popped: &d@n@t?=num2;>
get7h:;>
breaD>
E
7ase 3:
<
printf:?@n@tElements present in the sta7D are:@n@t?;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 16F
C & DATA STRUCTURE LAB PROGRAMS
displaJ_ele:;>
get7h:;>
breaD>
E
7ase !:
exit:1;>
breaD>
default:
printf:?@nTour 7hoi7e is invalid8@n?;>
breaD>
E
E
E
(C1nserting the elements using push fun7tionC(
void push_ele:int P;
<
stru7t st_point Cm>
m=:stru7t st_pointC;mallo7:sizeof:stru7t st_point;;>
mG9ele=P>
mG9l=t>
t=m>
return>
E
(C+emoving the elements using pop fun7tionC(
int pop_ele:;
<
if:t==0,..;
<
printf:?@n@ST'MR is EmptJ8?;>
get7h:;>
exit:1;>
E
else
<
int i=tG9ele>
t=tG9l>
return :i;>
E
return >
E
(C/isplaJing the elements C(
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#
C & DATA STRUCTURE LAB PROGRAMS
void displaJ_ele:;
<
stru7t st_point Cpointer=0,..>
pointer=t>
Bhile:pointer%=0,..;
<
printf:?&d@t?=pointerG9ele;>
pointer=pointerG9l>
E
E
2utput:
======================================
NE0,
======================================
Z1[ ,sing 3ush -un7tion
Z2[ ,sing 3op -un7tion
Z3[ Elements present in Sta7D
Z![ Exit
Enter Jour 7hoi7e: 1
Element to be pushed:23
======================================
NE0,
======================================
Z1[ ,sing 3ush -un7tion
Z2[ ,sing 3op -un7tion
Z3[ Elements present in Sta7D
Z![ Exit
Enter Jour 7hoi7e: 3
Elements present in the sta7D are:
23
======================================
NE0,
======================================
Z1[ ,sing 3ush -un7tion
Z2[ ,sing 3op -un7tion
Z3[ Elements present in Sta7D
Z![ Exit
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#1
C & DATA STRUCTURE LAB PROGRAMS
Enter Jour 7hoi7e: 2
Element to be popped: 23
======================================
NE0,
======================================
Z1[ ,sing 3ush -un7tion
Z2[ ,sing 3op -un7tion
Z3[ Elements present in Sta7D
Z![ Exit
Enter Jour 7hoi7e: !
Exit the program
VIVA Q!"A#IO$":
1& /efine Sta7D H
'ns: ' sta7D is a linear data stru7ture in Bhi7h a data item is inserted and deleted at one
end
2; /efine data stru7ture H
Ans: ' data stru7ture is a 7olle7tion of organized data that are related to ea7h other
)& 4hat are the various operation performed on the sta7D H
'ns: push:;= pop:;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#2
C & DATA STRUCTURE LAB PROGRAMS
Objective )):
3rogram that implement `ueue operation bJ using the arraJs
Description: 1n this program Be have to implement the `ueue operation bJ using the
arraJs8 5ere theJ `ueue operation are push and pop8 3ush operation is used to insert the
elements into a `ueue and pop operation is used to remove the elements in to a `ueue8
A67O8I#29 FO8 I$"!8#I$7 A$ !6!9!$# I$ #O A Q!!:
-un7tion `10SE+ET:`=-=+=0=T;
Step 1: ZoverfloB[
1f +9=0
Then printf:V overfloBW;
+eturn
Step 2: Z1n7rement rear pointer[
+"G++1
Step 3: Z 1nsert element[
`Z+["GJ
Step !: Z1s front pointer properlJ setH[
1f -=
Then f"G1
+eturn
A67O8I#29 FO8 D!6!#I$7 A$ !6!9!$# F8O9 A "#AC::
-un7tion `/E.ETE:`=-=+;
Step 1: Z,nderfloB[
1f -=
Then printf:V`ueue underfloBW;
+eturn:;
Step 2: Z/elete element[
J"GKZf[
Step 3: Z1s `ueue EmptJH[
1f -=+
Then -=+=
Else
-=-+1
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#3
C & DATA STRUCTURE LAB PROGRAMS
Step !:Z+eturn element[
+eturn:r;
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#!
C & DATA STRUCTURE LAB PROGRAMS
Program:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#$
C & DATA STRUCTURE LAB PROGRAMS
6 in7lude "stdio8h9
6 define size !
int front==rear=G1=item=7hoi7e=aZsize[>
main:;
<
7lrs7r:;>
Bhile:1;
<
printf:?CCC NE0, CCC@n 18 10SE+T120@n 28 /E.ET120@n
38T+'OE+SE@n !8 E^1T@n?;>
printf:?enter Jour 7hoi7e:?;>
s7anf:?&d?=A7hoi7e;>
sBit7h:7hoi7e;
<
7ase 1:insertion:;>
breaD>
7ase 2:deletion:;>
breaD>
7ase 3:traverse:;>
breaD>
7ase !:exit:;>
default:printf:?CCC Brong 7hoi7e CCC@n?;>
E
E
get7h:;>
E
insertion:;
<
if:rear==sizeG1;
printf:?CCC Kueue is full CCC@n?;>
else
<
printf:?enter item into Kueue:?;>
s7anf:?&d?=Aitem;>
rear++>
aZrear[=item>
E
E
deletion:;
<
if:front==rear+1;
printf:?CCC Kueue is emptJ CCC@n?;>
else
<
item=aZfront[>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#6
C & DATA STRUCTURE LAB PROGRAMS
front++>
printf:?the deleted item from Kueue is &d@n?=item;>
E
E
traverse:;
<
int i>
if:front==rear+1;
printf:?CCC Kueue is emptJ CCC@n?;>
else
<
for:i=front>i"=rear>i++;
if:i==front AA rear==i;
printf:?&d at &d G9front=rear@n?=aZi[=i;>
else
if:i==rear;
printf:?&d at &d G9rear@n?=aZi[=i;>
else
if:i==front;
printf:?&d at &d G9front@n?=aZi[=i;>
else
printf:?&d at &d@n?=aZi[=i;>
E
E
Input4Output:
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:1
enter item into Kueue:11
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:1
enter item into Kueue:12
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1##
C & DATA STRUCTURE LAB PROGRAMS
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:1
enter item into Kueue:13
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:1
enter item into Kueue:1!
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:1
CCC Kueue is full CCC
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:3
11 at G9front
12 at 1
13 at 2
1! at 3 G9rear
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:2
the deleted item from Kueue is 11
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:2
the deleted item from Kueue is 12
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#*
C & DATA STRUCTURE LAB PROGRAMS
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:2
the deleted item from Kueue is 13
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:2
the deleted item from Kueue is 1!
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:2
CCC Kueue is emptJ CCC
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:3
CCC Kueue is emptJ CCC
CCC NE0, CCC
18 10SE+T120
28 /E.ET120
38 T+'OE+SE
!8 E^1T
enter Jour 7hoi7e:!
conclusion: the program is error free
VIVA Q!"A#IO$":
1& /efine Kueue H
Ans: ' Kueue is a linear= seKuential list of that are a77essed in the oeder first in first
out:-1-2;8
2; /efine 7ir7ular Kueues H
Ans: ' Kueue 7an also be 7ir7ular in Bhi7h 7ase= it is 7alled as a 7ir7ular Kueue
3; 4hat are the various sta7D oriented notations H
Ans: i; infix ii; prefix iii; postfix
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1#F
C & DATA STRUCTURE LAB PROGRAMS
Objective )*:
3rogram that implement `ueue operation bJ using the pointers
Description :
1n this program Be have to implement the `ueue operation bJ using the pointers8
5ere theJ `ueue operation are push and pop8 3ush operation is used to insert the
elements into a `ueue and pop operation is used to remove the elements in to a
`ueue8
Algorithm:
Step 1: Start
Step 2: define stru7ture for Kueue
Step 3: read 7hoi7e
Step !: if 7hoi7e = insert
i; read the element
ii; 7reate a data stru7ture
iii; if emptJ Kueue then front of Kueue pinter points to neBlJ 7reated data
stru7ture
iv; otherBise end of the Kueue points to neBlJ 7reated data stru7ture
Step $: if 7hoi7e= remove
i; 7he7D if Kueue is emptJ 8 if so= print Kueue is emptJ
ii; otherBise read the element pointed bJ front of the Kueue temp pointer
points to front of Kueue
iii; front of Kueue points to next element
iv; free the element pointed bJ temp pointer
v; return the element
vi; print the element
Step 6: if 7hoi7e = displaJ
i; 7he7D of emptJ Kueue if so= print Kueue emptJ
ii; otherBise print the elements from front of the Kueue until the end of the
Kueue
step #: if 7hoi7e=exits stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*1
start
/efine the stru7ture for Kueue
+ead the 7hoi7e
1f
7hoi7e=inse
rt
+ead the element
to be inserted
1f
7hoi7e=remo
ve
1f %
e_Kue:;
3rint
Kueue is
emptJ
I
3trG9n=Kueue
`ueue G9ele=ele
`ueue G9n=0,..
1f
f_ptr=0,.
.
-+ptr=Kueue
I
3tr=f_ptr ptr G9n%=0,..
3tr = ptr G9 n
1f
7hoi7e=displ
aJ
3tr=f_ptr
'
1f
7hoi7e=ex
it
stop
3rint invalid
7hoi7e
3rint P
I
I
T -
-
T
-
T
T -
T
T
-
T
-
Flowchart:
C & DATA STRUCTURE LAB PROGRAMS
program:
6define true 1
6define false
6in7lude"stdio8h9
6in7lude"7onio8h9
6in7lude"pro7ess8h9
stru7t K_point
<
int ele>
stru7t K_pointC n>
E>
stru7t K_point Cf_ptr = 0,..>
int e_Kue:void;>
void add_ele:int;>
int rem_ele:void;>
void shoB_ele:;>
(Cmain fun7tionC(
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*2
'
1f
emptJ
Kueue
3rint emptJ
Kueue
I
1f ptr%
=0,..
3rint ptr G9
ele
3tr= ptrG9 n
T -
T
C & DATA STRUCTURE LAB PROGRAMS
void main:;
<
int ele=7hoi7e=P>
Bhile:1;
<
7lrs7r:;>
printf:?@n@nCCCC1N3.ENE0T'T120 2- `,E,E ,S10)
3210TE+SCCCC@n?;>

printf:?==============================================?;>
printf:?@n@t@t NE0,@n?;>

printf:?==============================================?;>
printf:?@n@tZ1[ To insert an element?;>
printf:?@n@tZ2[ To remove an element?;>
printf:?@n@tZ3[ To displaJ all the elements?;>
printf:?@n@tZ![ Exit?;>
printf:?@n@n@tEnter Jour 7hoi7e:?;>
s7anf:?&d?= A7hoi7e;>
sBit7h:7hoi7e;
<
7ase 1:
<
printf:?@n@tElement to be inserted:?;>
s7anf:?&d?=Aele;>
add_ele:ele;>
get7h:;>
breaD>
E
7ase 2:
<
if:%e_Kue:;;
<
P=rem_ele:;>
printf:?@n@t&d is removed from the Kueue?=P;>
get7h:;>
E
else
<
printf:?@n@t`ueue is EmptJ8?;>
get7h:;>
E
breaD>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*3
C & DATA STRUCTURE LAB PROGRAMS
E
7ase 3:
shoB_ele:;>
get7h:;>
breaD>
7ase !:
exit:1;>
breaD>
default:
printf:?@n@t1nvalid 7hoi7e8?;>
get7h:;>
breaD>
E
E
E
(C -un7tion to 7he7D if the Kueue is emptJC(
int e_Kue:void;
<
if:f_ptr==0,..;
return true>
return false>
E
(C -un7tion to add an element to the KueueC(
void add_ele:int ele;
<
stru7t K_point CKueue = :stru7t K_pointC;mallo7:sizeof:stru7t K_point;;>
KueueG9ele = ele>
KueueG9n = 0,..>
if:f_ptr==0,..;
f_ptr = Kueue>
else
<
stru7t K_pointC ptr>
ptr = f_ptr>
for:ptr=f_ptr >ptrG9n%=0,..> ptr=ptrG9n;>
ptrG9n = Kueue>
E
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*!
C & DATA STRUCTURE LAB PROGRAMS
(C -un7tion to remove an element from the KueueC(
int rem_ele:;
<
stru7t K_pointC Kueue=0,..>
if:e_Kue:;==false;
<
int P = f_ptrG9ele>
Kueue=f_ptr>
f_ptr = f_ptrG9n>
free :Kueue;>
return P>
E
else
<
printf:?@n@t`ueue is emptJ8?;>
return GFFFF>
E
E
(C -un7tion to displaJ the KueueC(
void shoB_ele:;
<
stru7t K_point Cptr=0,..>
ptr=f_ptr>
if:e_Kue:;;
<
printf:?@n@t`,E,E is EmptJ8?;>
return>
E
else
<
printf:?@n@tElements present in `ueue are:@n@t?;>
Bhile:ptr%=0,..;
<
printf:?&d@t?=ptrG9ele;>
ptr=ptrG9n>
E
E
E
Output:
CCCC1N3.ENE0T'T120 2- `,E,E ,S10) 3210TE+SCCCC
==============================================
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*$
C & DATA STRUCTURE LAB PROGRAMS
NE0,
==============================================
Z1[ To insert an element
Z2[ To remove an element
Z3[ To displaJ all the elements
Z![ Exit
Enter Jour 7hoi7e:1
Element to be inserted:23

CCCC1N3.ENE0T'T120 2- `,E,E ,S10) 3210TE+SCCCC
==============================================
NE0,
==============================================
Z1[ To insert an element
Z2[ To remove an element
Z3[ To displaJ all the elements
Z![ Exit
Enter Jour 7hoi7e:
3
Elements present in `ueue are:
23

CCCC1N3.ENE0T'T120 2- `,E,E ,S10) 3210TE+SCCCC
==============================================
NE0,
==============================================
Z1[ To insert an element
Z2[ To remove an element
Z3[ To displaJ all the elements
Z![ Exit
Enter Jour 7hoi7e:2
23 is removed from the Kueue
CCCC1N3.ENE0T'T120 2- `,E,E ,S10) 3210TE+SCCCC
==============================================
NE0,
==============================================
Z1[ To insert an element
Z2[ To remove an element
Z3[ To displaJ all the elements
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*6
C & DATA STRUCTURE LAB PROGRAMS
Z![ Exit
Enter Jour 7hoi7e:!
Exit
Conclusion : the program is error free
VIVA Q!"A#IO$":
1& /efine Kueue H
Ans: ' Kueue is a linear= seKuential list of that are a77essed in the oeder first in first
out:-1-2;8
2; /efine 7ir7ular Kueues H
Ans: ' Kueue 7an also be 7ir7ular in Bhi7h 7ase= it is 7alled as a 7ir7ular Kueue
3; 4hat are the various sta7D oriented notations H
Ans: i; infix ii; prefix iii; postfix
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*#
C & DATA STRUCTURE LAB PROGRAMS
Objective )+: 3rogram that uses the sta7D operation to perform the folloBing
i; 7onverting infix expression into postfix expression
ii; evaluating the postfix expression
Description:
1n this program Be have to 7overt the given infix expression to postfix expression
and the finallJ evaluating that postfix expression8 5ere Be made use of sta7D
operations8 The propertJ of sta7Ds is last in first out8 i8e8= the item that is inserted
last Bill be the firt item remove8
A67O8I#29:
Step 18 start
Step 28 first initialize the sta7D to be emptJ
Step 38 for ea7h 7hara7ter in the input string
1f input string is an operand= append to the output
if the input string is a left paranthesis = push it onto the sta7D
else
if sta7D is emptJ or the operator has higher prioritJ than the operator on
the topof sta7D or
the top of the sta7D is opening parenthesis
then
push the operator onto the sta7D
else
pop the operator from the sta7D and append to the output
Step !8 if the input string is a 7losing parenthesis = pop operators from the sta7D
and append the operators
to the output until an opening parenthesis is en7ountered8 3op the
opening parenthesis from the sta7D
and dis7ard it8
Step $8 if the end of the input string is en7ountered = then iterate the loop until the
sta7D is not emptJ8 3op
the sta7D and append the remaining input string to the output8
Step 68 stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1**
C & DATA STRUCTURE LAB PROGRAMS
2 + :$G*;(3 = 2$*G3(+
1nput 2 input $ input *
1nput pop *=$ push result
input 3

1nput 1 input +
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1*F
2
$
2
*
$
2
$
2
2
G3
2
3
G3
2
$
2
G1
2
1
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"7tJpe8h9
6in7lude"string8h9
stati7 7har strZ2[>
int top=G1>
main:;
<
7har inZ2[=postZ2[=7h>
int i=P=l>
7lrs7r:;>
printf:?enter the string?;>
gets:in;>
l=strlen:in;>
for:i==P=>i"l>i++;
if:isalpha:inZi[;;
postZP++[=inZi[>
else
<
if:inZi[==X:X;
push:inZi[;>
else if:inZi[==X;X;
Bhile::7h=pop:;;%=X:X;
postZP++[=7h>
else
<
Bhile:prioritJ:inZi[;"=prioritJ:strZtop[;;
postZP++[=pop:;>
push:inZi[;>
E
E
Bhile:top%=G1;
postZP++[=pop:;>
postZP[=X@X>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F
C & DATA STRUCTURE LAB PROGRAMS
printf:?@n eKuivalent infix to postfix is:&s?=post;>
get7h:;>
E
prioritJ :7har 7;
<
sBit7h:7;
<
7aseX+X:
7aseXGX: return 1>
7aseXCX:
7aseX(X:
return 2>
7aseXaX:
return 3>
E
return >
E
push:7har 7;
<
strZ++top[=7>
E
pop:;
<
return:strZtopGG[;>
E
Input4Output:
enter the string:a+b;G:7Gd;Ce(f
eKuivalent infix to postfix is:ab+7dGeCf(G
enter the stringa+b(7Cd
eKuivalent infix to postfix is:ab7(dC+
Conclusion: the program is error free8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F1
C & DATA STRUCTURE LAB PROGRAMS
ii&A67O8I#29:
"tep 1: "tart
Step 2: 'ssign top=G1
Step 3: +ead the input expression
Step !: for i=>sZi[%=Y@Y in steps of 1
Step $: 1f isdigit:7h;
Step $81:3ush:7h;
Step 6: otherBise
Step 681:op1=pop:;
Step 682: op2=pop:;
Step #: 7=op2+op1
Step *: 3ush:7;
Step F: 7=op2Gop1
Step 1: 3ush:7;
Step 11: 7=poB:op2=op1;
Step 12: 3ush:7;
Step 13: 7=op2(op1
Step 1!:3ush:7;
Step 1$: 3rint the result
Step 16:3ush:int x;
Step 1#:1n7rement top bJ 1
Step 1*: s18item:s18top3;=x
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F2
C & DATA STRUCTURE LAB PROGRAMS
Step 1F:pop:;
Step 2: +ead x
Step 21: x1=s18itemZs18top[
Step 22:s18topS
Step 23:return x
Step 2!: Stop
Flowchart:
a+b = ab+
3ush a 3op a
3ush + 3ush b
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F3
a
+
b
+
C & DATA STRUCTURE LAB PROGRAMS
3op ab+
Program:
6in7lude"stdio8h9
6in7lude"7tJpe8h9
int stDZ1[=top==op1=op2=i>
main:;
<
7har postexpZ1[>
7lrs7r:;>
printf:?enter the postfix expression:?;>
gets:postexp;>
for:i=>postexpZi[%=X@X>i++;
<
if:isdigit:postexpZi[;;
push:postexpZi[G!*;>
else
<
op1=pop:;>
op2=pop:;>
sBit7h:postexpZi[;
<
7ase X+X:push:op1+op2;>
breaD>
7ase XGX:push:op1Gop2;>
breaD>
7ase XCX:push:op1Cop2;>
breaD>
7ase X(X:push:op1(op2;>
breaD>
7ase X&X:push:op1&op2;>
breaD>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F!
C & DATA STRUCTURE LAB PROGRAMS
7ase X8X:exit:;>
E
E
E
printf:?the result of postfixexpression is: &d?=pop:;;>
get7h:;>
E
pop:;
<
return:stDZtopGG[;>
E
push:int x;
<
top++>
stDZtop[=x>
E
Input4Output:
enter the postfix expression:23!+G
the result of postfix expression is: $
Conclusion: the program is error free8
VIVA Q!"A#IO$":
1& /efine Sta7D H
'ns: ' sta7D is a linear data stru7ture in Bhi7h a data item is inserted and deleted at one
end
2; /efine data stru7ture H
Ans: ' data stru7ture is a 7olle7tion of organized data that are related to ea7h other
)& 4hat are the various operation performed on the sta7D H
'ns: push:;= pop:;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F$
C & DATA STRUCTURE LAB PROGRAMS
Objective ),: 3rogram to
i; 7reate a binarJ tree of integers
ii; Traversing the above binarJ tree in preorder= inorder and post order

Description: The data stru7ture tree is of non linear tJpe8 IinarJ tree is bit spe7ial8 The
propartJ of a binarJ tree is the value at the root must be grater than the left node and less
than the right node8 The binarJ tree 7onsist of almost tBo 7hildrens8 Tree is a re7ursive
data stru7ture and re7ursive programming te7hniKues are popularlJ used in trees8 ' tree
7an 7an be traversed in three maPor BaJs
i; 1norder traversal: here left 7hild is visited first folloBed bJ root and finallJ bJ
right 7hild8
ii; 3reorder traversal: 5ere root is visitedfirst follBed bJ left 7hild and finallJ bJ
right 7hild8
iii; 3ostorder traversal: 5ere left 7hild is visited first folloBed bJ right 7hild
finallJ bJ the root8
A67O8I#29:
Step 1: Start
Step 2: /efine a stru7ture btree
Step 3: TJpe define stru7t btree as node
Step !: 4hile:tree;= begin
Step $: 3rint NE0,
Step 6: 4rite Jour 7hoi7e
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F6
C & DATA STRUCTURE LAB PROGRAMS
Step #: 1f 7hoi7e=1
Step *: Enter Jour no of nodes
Step F:+ead nodes n
Step 1: for i=1 to n in steps of 1 do
Step 11: 3rint enter item
Step 12: +ead item
Step 13: +oot =7all 7reate :root= item;8end for
Step 1!: if 7hoi7e=2
Step1$: +ead element to be deleated
Step 16: Mall delete:root= item; end for
Step 1#: 1f 7hoi7e=3
Step 1*: Mall preorder:root;
Step 1F: Mall postorder:root;
Step 2: Mall inorder:root;
Step 21: IreaD= end of sBit7h
Step 22: Stop

-or insert fun7tion
Step 1: Start
Step 2: 1f t= null
Step 3: 'llo7ate memorJ to temp
Step !: TempG9data =item
Step $: TempG9 l7=null
Step 6: TempG9r7=null
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F#
C & DATA STRUCTURE LAB PROGRAMS
Step #: return t to main and end
Step *: 1f item" :lG9data;
Step F: TG9l7=7all insert:eG9l7= t;
Step 1: TG9r7=7all isert:eG9l7=t;
Step 11:+eturn t
Step 12: Stop
-or DELETION fun7tion
Step 1: Start
Step 2: x=d
Step 3: Bhile x%=null
Step !: 1f xG9data =t
Strep $:IreaD
Step 6: 3arent =x
Step #: if t"xG9data
Step *: t=tl7
Step F: t=lr7
Step 1: 1f xl7%=null AAxr7%=null
Step11: parent =x
Step12: 1f parent==null
Step 13: parentl7==null
Step 1!: parentr7==null
Step 1$: 1f pG9l7=xG9r7
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1F*
C & DATA STRUCTURE LAB PROGRAMS
Step 16: 1f pG9r7=xG9r7
Step 1#: 4hile insertG9l7=null
Step 1*: 1nsert=insertG9la
Step 1F:xG9data=insertG9data
Step 2:x=insert
Step 21: +eturn d
Step 22: Stop
-or 102+/E+ fun7tion
Step 1: Start
Step 2: 1f t%=null
Step 3: Mall inorder:tG9l7;
Step !: 4rite tG9data
Step $: Mall inorder:tG9r7;
Step 6: Stop
-or 32ST2+/E+ fun7tion
Step 1: Start
Step 2: 1f t%=null
Step 3: Mall postorder:tG9l7;
Step !: Mall postorder:tG9r7;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 1FF
C & DATA STRUCTURE LAB PROGRAMS
Step $: 4rite data
Step 6: Stop
-or 3+E2+/E+ fun7tion
Step 1: Start
Step 2: 1f t%=null
Step 3: 4rite data
Step !: Mall postorder:tG9l7;
Step $: Mall postorder:tG9r7;
Step 6: Stop
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 21
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 22
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"allo78h9
stru7t bstnode
<
int data>
stru7t bstnode Cl7=Cr7>
ECroot=CaZ2[=CbZ2[>
int top=G1=top1=G1=n=i>
main:;
<
int 7h=ele>
stru7t bstnode Ct=Cinsert:;=Cpop:;>
7lrs7r:;>
t=root=0,..>
Bhile:1;
<
printf:?@n CCCC N E 0 , CCCC @n?;>
printf:?1810SE+T@n28+EM,+SS1OE T+'OE+SE@n38020G+EM,+S1OE
T+'OE+SE@n!8E^1T@n?;>
printf:?Enter Jour 7hoi7e: ?;>
s7anf:?&d?=A7h;>
sBit7h:7h;
<
7ase 1: printf:?Enter hoB manJ elements u Bant to insert:?;>
s7anf:?&d?=An;>
printf:?Enter tree elements: ?;>
for:i=1>i"=n>i++;
<
s7anf:?&d?=Aele;>
t=insert:t=ele;>
E
breaD>
7ase 2: (C +EM,+SS1OE T+'OE+SE C(
if:t==0,..;
printf:?CCCC T+EE 1S EN3TT CCCC?;>
else
<
printf:?102+/E+ :?;>
inorder:t;>
printf:?@n3+E2+/E+ :?;>
preorder:t;>
printf:?@n32ST2+/E+ :?;>
postorder:t;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 23
C & DATA STRUCTURE LAB PROGRAMS
E
breaD>
7ase 3: (C 020G+EM,+SS1OE T+'OE+SE C(
if:t==0,..;
printf:?T+EE 1S EN3TT?;>
else
<
printf:?102+/E+ :?;>
nrinorder:t;>
printf:?@n3+E2+/E+ :?;>
nrpreorder:t;>
printf:?@n32ST2+/E+ :?;>
nrpostorder:t;>
E
breaD>
7ase !:
exit:;>
E
E
E
stru7t bstnode Cinsert:stru7t bstnode Cx=int J;
<
if:x==0,..;
<
x=mallo7:sizeof:stru7t bstnode;;>
xG9data=J>
xG9l7=0,..>
xG9r7=0,..>
E
else
<
if:J"xG9data;
xG9l7=insert:xG9l7=J;>
else
xG9r7=insert:xG9r7=J;>
return:x;>
E
E
inorder:stru7t bstnode Cx;
<
if:x%=0,..;
<
inorder:xG9l7;>
printf:?&3d?=xG9data;>
inorder:xG9r7;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!
C & DATA STRUCTURE LAB PROGRAMS
E
E
preorder:stru7t bstnode Cx;
<
if:x%=0,..;
<
printf:?&3d?=xG9data;>
preorder:xG9l7;>
preorder:xG9r7;>
E
E
postorder:stru7t bstnode Cx;
<
if:x%=0,..;
<
postorder:xG9l7;>
postorder:xG9r7;>
printf:?&3d?=xG9data;>
E
E
nrinorder:stru7t bstnode Cx;
<
stru7t bstnode Cl>
l=x>
do
<
Bhile:l%=0,..;
<
push:l;>
l=lG9l7>
E
Bhile:top9G1;
<
l=pop:;>
printf:?&d?=lG9data;>
if:lG9r7%=0,..;
<
l=lG9r7>
breaD>
E
else
l=0,..>
E
EBhile:l%=0,..;>
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$
C & DATA STRUCTURE LAB PROGRAMS
nrpreorder:stru7t bstnode Cx;
<
stru7t bstnode Cl>
l=x>
do
<
printf:?&d?=lG9data;>
if:lG9r7%=0,..;
push:lG9r7;>
l=lG9l7>
if:l==0,..AAtop9G1;
l=pop:;>
EBhile:l%=0,..;>
E
nrpostorder:stru7t bstnode Cx;
<
stru7t bstnode Cl>
l=x>
do
<
Bhile:l%=0,..;
<
push:l;>
if:lG9r7%=0,..;
<
push:lG9r7;>
bZ++top1[=lG9r7>
E
l=lG9l7>
E
do
<
l=pop:;>
if:l%=bZtop1[;
printf:?&3d?=lG9data;>
else
<
top1G=1>
breaD>
E
E Bhile:top9G1;>
EBhile:l%=0,..AAtop9G1;>
E
push:stru7t bstnode CJ;
<
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 26
C & DATA STRUCTURE LAB PROGRAMS
top+=1>
aZtop[=J>
E
stru7t bstnode Cpop:;
<
return aZtopGG[>
E
Input4Output:
Enter Jour 7hoi7e
181nsert 28/elete 38Traversal
Enter the element F2
Enter Jour 7hoi7e
18 1nsert 28/elete 38 Traversal
Enter the element 26
Enter Jour 7hoi7e
181nsert 28/elete 38Traversal
Enter the element 12
Enter Jour 7hoi7e
181nsert 28/elete 38Traversal
Enter the element 123
Enter Jour 7hoi7e
181nsert 28/elete 38Traversal
Enter the element 13$
Enter Jour 7hoi7e
181nsert 28/elete 38Traversal
Enter the element 12*
Enter Jour 7hoi7e
181nsert 28/elete 38Traversal
3
1norderSeKuen7e: 12 26 F2 123 12* 13$
3reorder seKuen7e:F2 26 12 123 13$ 12*
3ostorder seKuen7e: 12 26 12* 13$ 12 F2
Conclusion: the program is error free8
VIVA Q!"A#IO$":
1; /efine IinarJ tree H
'ns: IinarJ tree is a bit spe7ial= be7ause Bhan theJ are in the sorted form= theJ fa7ilitate
Kui7D sear7h= insertion= and deletion8
2; 5oB manJ BaJs a tree 7an be traversed H
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2#
C & DATA STRUCTURE LAB PROGRAMS
'ns: 1n three BaJs8 TheJ are i; 1nGorder ii; preGorder iii; postGorder
3; define graph H
'ns: ' graph is a set of nodes:verti7es; and a set of ar7s:edges;8 ' graph is 7onne7ted if
there is a path betBeen anJ tBo nodes of the graph8
Objective )-:
To perform the linear sear7h operation
Description: The linear sear7h is most simple ser7hing method8 1t does not expe7t the list
to be sorted8 The DeJ Bhi7h is to be sear7hed is 7ompared Bith ea7h element of the list
one bJ one8 1f a mat7h exists= the sear7h is terminated8 1f the end of list is rea7hed it
means that the sear7h has failed and DeJ has no mat7hing in the list8
A67O8I#29:
6I$!A8 "!A8C2
18 Start
28 +ead the value of n
38 for i=1 to n in7rement in steps of 1
+ead the value of ith element into arraJ
!8 +ead the element:x; to be sear7hed
$8 sear7h"GGlinear:a=n=x;
68 if sear7h eKual to goto step # otherBise goto step *
#8 print unsu77essful sear7h
*8 print su77essful sear7h
F8 stop
.10E'+ -,0MT120
18 start
28 for i=1 to n in7rement in steps of 1
38 if m eKual to DZi[ goto step ! otherBise goto step 2
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2*
C & DATA STRUCTURE LAB PROGRAMS
!8 return i
$8 return
68 stop
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2F
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 21
start
+ead n
i=1
i"=n i++
+ead aZi[
+ead b
M=linear sear7h:a=b;
1f 7%
=
3rint 7
Print element is
not 1oun;
stop
T -
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 211
start
-or:i=1>i"n>i++;
1f
m==DZi[
T
-
stop
-
T
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
main:;
<
int i=P=n=aZ1[=DeJ>
7lrs7r:;>
printf:?enter range for arraJ:?;>
s7anf:?&d?=An;>
printf:?enter elements into arraJ:?;>
for:i=>i"=n>i++;
s7anf:?&d?=AaZi[;>
printf:?enter the sear7h element:?;>
s7anf:?&d?=ADeJ;>
for:i=>i"=n>i++;
<
if:DeJ==aZi[;
<
printf:?element &d found at &d?=DeJ=i;>
breaD>
E
else
if:i==n;
printf:?element &d not found in arraJ?=DeJ;>
E
get7h:;>
E
Input4Output:
enter range for arraJ:!
enter elements into arraJ:$6
!3
12
**
F
enter the sear7h element:F
element F found at !
enter range for arraJ:$
enter elements into arraJ:23
12
$6
3!
3
*
enter the sear7h element:3
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 212
C & DATA STRUCTURE LAB PROGRAMS
element 3 found at !
conclusion: the program is error free
VIVA Q!"A#IO$":
1; /efine linear sear7h H
'ns : The linear sear7h is most simple ser7hing method8 1t does not expe7t the list to be
sorted8 The DeJ Bhi7h is to be sear7hed is 7ompared Bith ea7h element of the list one bJ
one8 1f a mat7h exists= the sear7h is terminated8 1f the end of list is rea7hed it means that
the sear7h has failed and DeJ has no mat7hing in the list8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 213
C & DATA STRUCTURE LAB PROGRAMS
Objective ).:
To perform the binarJ sear7h operation
Description: IinarJ sear7h is a vast improvement over the seKuential sear7h8 -or binarJ
sear7h to BorD= the item in the list must be in assorted order8 The approa7h emploJed in
the binarJ sear7h is divid and 7onKuer8 1f the list to be sorted for a spe7ifi7 item is not
sorted= binarJ sear7h fails8
A67O8I#29:
I10'+T SE'+M5
18 Start
28 +ead the value of n
38 for i=1 to n in7rement in steps of 1
+ead the value of ith element into arraJ
!8 +ead the element:x; to be sear7hed
$8 sear7h"GGbinarJ:a=n=x;
68 if sear7h eKual to goto step # otherBise goto step *
#8 print unsu77essful sear7h
*8 print su77essful sear7h
F8 stop
I10'+T SE'+M5 -,0MT120
18 start
28 initialise loB to 1 =high to n= test to
38 if loB"= high repeat through steps ! to F otherBise goto step 1
!8 assign :loB+high;(2 to mid
$8 if m"DZmid[ goto step 6 otherBise goto step #
68 assign midG1 to high goto step 3
#8 if m9DZmid[ goto step * otherBise goto step F
*8 assign mid+1 to loB
F8 return mid
18 return
118stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 21!
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 21$
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 216
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 21#
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
main:;
<
int i=n=DeJ=aZ1[=loB=high=mid>
7lrs7r:;>
printf:?enter range for arraJ:?;>
s7anf:?&d?=An;>
printf:?enter elements into arraJ:?;>
for:i=>i"n>i++;
s7anf:?&d?=AaZi[;>
printf:?the sear7h element:?;>
s7anf:?&d?=ADeJ;>
loB=>
high=nG1>
for:i=>i"n>i++;
<
mid=:loB+high;(2>
if:aZmid[==DeJ;
<
printf:?element &d found at &d?=DeJ=mid;>
breaD>
E
if:DeJ"aZmid[;
high=mid>
else
loB=mid+1>
if:i==nG1;
printf:?element &d not found in arraJ?=DeJ;>
E
get7h:;>
E
Input4Output:
enter range for arraJ:!
enter elements into arraJ:12 23 3! !$
the sear7h element:!$
element !$ found at 3
enter range for arraJ:$
enter elements into arraJ:1 3! $6 #* **
the sear7h element:!$
element !$ not found in arraJ
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 21*
C & DATA STRUCTURE LAB PROGRAMS
conclusion: the program is error free
VIVA Q!"A#IO$"
1& /efine IinarJ sear7h H
Ans: IinarJ sear7h is a vast improvement over the seKuential sear7h8 -or binarJ sear7h to
BorD= the item in the list must be in assorted order8 The approa7h emploJed in the binarJ
sear7h is divid and 7onKuer8 1f the list to be sorted for a spe7ifi7 item is not sorted= binarJ
sear7h fails8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 21F
C & DATA STRUCTURE LAB PROGRAMS
Objective )/:
3rogram that implements the bubble sort method
Description: Iubble sort is the simplest and oldest sorting te7hniKue8 This method taDes
tBo elements at a time8 1t 7ompare these tBo elements8 1f first elements is less than
se7ond one= theJ are left undistrurbed8 1f the first element is greater then se7ond one then
theJ are sBapped8 The pro7edure 7ontinues Bith the next tBo elements goes and ends
Bhen all the elements are sorted8
Iut bubble sort is an ineffi7ient algorithm8 The order of
bubble sort algorithm is 2:n
2
;8
Algorithm:
i;Iubble Sort:
18 start
28 read the value of n
38 for i= 1 to n in7rement in steps of 1
+ead the value of ith element into arraJ
!8 7all fun7tion to sort :bubble_sort:a=n;;
$8 for i= 1 to n in7rement in steps of 1
print the value of ith element in the arraJ
68 stop
I,II.E S2+T -,0MT120
18 start
28 initialise last to n
38 for i= 1 to n in7rement in steps of 1
begin
!8 initialise ex to
$8 for i= 1 to lastG1 in7rement in steps of 1
begin
68 if DZi[9DZi+1[ goto step # otherBise goto step $
begin
#8 assign DZi[ to temp
assign DZi+1[ to DZi[
assign temp to DZi+1[
in7rement ex bJ 1
endGif
end inner for loop
118 if ex%=
assign lastG1 to last
end for loop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 22
C & DATA STRUCTURE LAB PROGRAMS
128 stop
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 221
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 222
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
main:;
<
int i=P=t=aZ$[=n>
7lrs7r:;>
printf:?enter the range of arraJ:?;>
s7anf:?&d?=An;>
printf:?enter elements into arraJ:?;>
for:i=>i"n>i++;
s7anf:?&d?=AaZi[;>
for:i=>i"nG1>i++;
for:P=i+1>P"n>P++;
if:aZi[9aZP[;
<
t=aZi[>
aZi[=aZP[>
aZP[=t>
E
printf:?the sorted order is:?;>
for:i=>i"n>i++;
printf:?@t&d?=aZi[;>
get7h:;>
E
Input4Output:
enter the range of arraJ:3
enter elements into arraJ:3
2
1
the sorted order is: 1 2 3
enter the range of arraJ:$
enter elements into arraJ:$6
23
3!
12
*
the sorted order is: * 12 23 3! $6
conclusion: The program is error free
VIVA Q!"A#IO$"
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 223
C & DATA STRUCTURE LAB PROGRAMS
1; /efine bubble sort H
'ns: : Iubble sort is the simplest and oldest sorting te7hniKue8 This method taDes tBo
elements at a time8 1t 7ompare these tBo elements8 1f first elements is less than se7ond
one= theJ are left undistrurbed8 1f the first element is greater then se7ond one then theJ are
sBapped8 The pro7edure 7ontinues Bith the next tBo elements goes and ends Bhen all the
elements are sorted8
2; displaJ the effi7ien7J of bubble sort H
Ans : 2:n
2
;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 22!
C & DATA STRUCTURE LAB PROGRAMS
Objective *0:
3rogram that implements the `ui7D sort method
Description: This method is invented bJ hoare= 7onsidered to be fast method to sort the
elements8 The method is also 7alled partition ex7hange sorting8 The method is based on
divide and 7onKuer te7hniKue8 i8e8= the entire list is divided into various partitions and
sorting is applied again and again on the partition8
1n this method the list is divided into tBo baesd on an element 7alled pivot
element8 ,suallJ the first element is 7onsiderd to be the pivot element8 0oB move the
pivot element to its 7orre7t position in the list8 The elements to the left and pivot element
are less that this Bhile the elements to the right of pivot are greater than the pivot8 The
pro7ess is reapplied to ea7h of these partitions till Be got the sorted list of elements8
Algorithm:
Quic< "ort:
18 start
28 if loBerbound " upperbound repeat through steps 3 to 13 otherBise
goto step 1!
begin
38 assign loBerbound to i=upperbound to P= i to pivot
!8 if i"P repeat through steps $ to 1 otherBise goto step _
Iegin
$8 if aZi["=DZpivot[ and i" upperbound repeat through step 6 otherBise
goto step #
begin
68 assign i+1 to i
end if
#8 if DZP[ 9 DZpivot[ repeat through step * otherBise goto step F
begin
*8 assign PG1 to P
end if
F8 if i" P goto step 1 other Bise goto step !
Iegin
18 7all fun7tion to sBap DZi[ and DZP[
end if
end if
118 7all fun7tion to sBap DZpivot[ and DZP[
128 7all fun7tion Ksort:x=loBerbound=PG1;
138 7all fun7tion Ksort:x=P+1=upperbound;
end if
1!8 stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 22$
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:

VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 226
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
main:;
<
int xZ1[=i=n>
7lrs7r:;>
printf:?enter no of elements:?;>
s7anf:?&d?=An;>
printf:?enter &d elements:?=n;>
for:i=1>i"=n>i++;
s7anf:?&d?=AxZi[;>
Kui7Dsort:x=1=n;>
printf:?sorted elements are:?;>
for:i=1>i"=n>i++;
printf:?&3d?=xZi[;>
get7h:;>
E
Kui7Dsort:int xZ1[=int first=int last;
<
int pivot=i=P=t>
if:first"last;
<
pivot=first>
i=first>
P=last>
Bhile:i"P;
<
Bhile:xZi["=xZpivot[ AA i"last;
i++>
Bhile:xZP[9xZpivot[;
PGG>
if:i"P;
<
t=xZi[>
xZi[=xZP[>
xZP[=t>
E
E
t=xZpivot[>
xZpivot[=xZP[>
xZP[=t>
Kui7Dsort:x=first=PG1;>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 22#
C & DATA STRUCTURE LAB PROGRAMS
Kui7Dsort:x=P+1=last;>
E
E
CCCCC 2,T3,T CCCCC
enter no of elements:6
enter 6 elements:23
12
!$
3!
21
*#
sorted elements are: 12 21 23 3! !$ *#
conclusion: The program is error free
VIVA Q!"A#IO$"
1& /efine Kui7D sort H
Ans:
This method is invented bJ hoare= 7onsidered to be fast method to sort the elements8 The
method is also 7alled partition ex7hange sorting8 The method is based on divide and
7onKuer te7hniKue8 i8e8= the entire list is divided into various partitions and sorting is
applied again and again on the partition8
1n this method the list is divided into tBo baesd on an element 7alled pivot
element8 ,suallJ the first element is 7onsiderd to be the pivot element8 0oB move the
pivot element to its 7orre7t position in the list8 The elements to the left and pivot element
are less that this Bhile the elements to the right of pivot are greater than the pivot8 The
pro7ess is reapplied to ea7h of these partitions till Be got the sorted list of elements8
%& Effi7ien7J of Kui7D sort H
Ans: 2:n log n;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 22*
C & DATA STRUCTURE LAB PROGRAMS
Objective *1:Program that implements the insertion sort method
Description: 1nsertion sort is similar to plaJing 7ards8 To sort the 7ards in Jourhand Jou
extrat a 7ard shift the remaining 7ards and then insert the extra7ted 7ard in its 7orre7t
pla7e8 The effi7ien7J of insertion sort is 2:n
2
;8
Algorithm:
ii; 1nsertion Sort:
18 start
28 for i= 1 to n in7rement in steps of 1
begin
assign DZi[ to temp
38 forP=iG1 doBn to P9= and temp"DZP[
begin
assign DZP[ to DZP+1[
end inner for loop
!8 assign temp to DZP+1[
end for loop
$8 stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 22F
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 23
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
main:;
<
int i=P=t=aZ1[=n=p=>
7lrs7r:;>
printf:?enter the range of arraJ:?;>
s7anf:?&d?=An;>
printf:?enter elements into arraJ:?;>
for:i=>i"n>i++;
s7anf:?&d?=AaZi[;>
for:i=1>i"n>i++;
<
t=aZi[>
for:p=i>p9 AA aZpG1[9t>pGG;
aZp[=aZpG1[>
aZp[=t>
E
printf:?the sorted order is:?;>
for:i=>i"n>i++;
printf:?@t&d?=aZi[;>
get7h:;>
E
CCCCC 2,T3,T CCCCC
enter the range of arraJ:$
enter elements into arraJ:$
!
3
2
1
the sorted order is: 1 2 3 ! $
enter the range of arraJ:6
enter elements into arraJ:23
12
*F
!$
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 231
C & DATA STRUCTURE LAB PROGRAMS
6#
3!
the sorted order is: 12 23 3! !$ 6# *F
conclusion: The program is error free
VIVA Q!"A#IO$"
1& /efine insertion sort H
Ans: 1nsertion sort is similar to plaJing 7ards8 To sort the 7ards in Jourhand Jou extrat a
7ard shift the remaining 7ards and then insert the extra7ted 7ard in its 7orre7t pla7e8
%& Effi7ien7J of the insertion sort H
Ans: The effi7ien7J of insertion sort is 2:n
2
;8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 232
C & DATA STRUCTURE LAB PROGRAMS
Objective *%:
Program to implement the merge sort method
Description:
The merge sort splits the list to be sorted into tBo eKual halves= and pla7es them
in separate arraJs8 Ea7h arraJ is re7ursivelJ sorted= and then merged ba7D together
to form the final sorted list8 .iDe most re7ursive sorts= the merge sort has an
algorithmi7 7omplexitJ of O:n log n;8
Algorithm: main program
Step1: Start
Step2: de7lare the merge sort fun7tion
Step3: /e7lare the arraJ and their size and initailaze the P=
Step!: read the arraJ elements and then sort these elements8
Step$: read the arraJ elements before the merge sort and then displaJ the
elements8
Step6: 7all the merge sort fun7tion
Step#: displaJ the arraJ elements after merge sort bJ using the folloBing stament8
for: P=>P"Nax_arJ>P++;
Step*: Stop
"ubprogram
Step1:initialize the arraJ ex7utingZN'^_'+T[ and
P==mid==mrg1==mrg2==size=startGend+1
Step2: 7he7D the 7ondition if:end==start; then return
Step3: 7al7ulate the mid value
Nid=:end+start;(2
Step!: 7all themerge_sort:x=end=mid;
Step$:merge_sort:x=mid+1=start;
Step6: performing the looping operation
-or:P=>P"S1bE>P++; then its true
Exe7utingZP[=xZend+1[
Nrg1=>
Step#: 7al7ulate the mrg2=midGend+1
Step*: performing the looping operation
-or:P=>P"S1bE>P++; then its true then goto stepF
StepF: 7he7D the 7ondition
i; if:mrg2"=startGend; is true goto ii;8 1f not goto Step128
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 233
C & DATA STRUCTURE LAB PROGRAMS
ii; 1f:mrg1"=midGend; is true goto iii;8 1f not goto step11
iii; 1f:exe7utingZmrg1[9exe7utingZmrg2[; is true then folloBs8 1f not
goto step18
^ZP+end[= exe7utingZmrg2++[
Step1: xZP+end[=exe7utingZmrg1++[8 1f not goto Step11
Step11: xZP+end[= exe7utingZmrg2++[
Step12: xZP+end[=exe7utingZmrg1++[
Step13: return to main program
Flow chart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 23!
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 23$
Start
/e7lare the arraJ and arraJ size
+ead the arraJ elements
/efine merge_sort fun7tion
Q= P++
P"N'^_'+T
/isplaJ the
elements before
merge sort
Nergesort:arJ==N'^_'+TG1;
Q= P++
P"N'^_'+T
/isplaJ the
elements after
merge sort
True
-alse
True
-alse
Stop
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 236
Nerge_sort:;
Size=startGend+1
Nid= Nrg1= Nrg2=
1f end==start
Nid=:end+start;(2
Mall merge_sort:x=end=mid;
Mall merge_sort:x=mid+1=start;
P=
P++
P"size
Exe7utingZP[=xZend+P[
Nrg1=
True
P++
P=
P"size
-alse
1f mrg2"=startG
end
-alse
^ZP+end[=exe7utingZmrg1++[
True
1f mrg1"=midG
end
-alse
^ZP+end[=exe7utingZmrg2++[
1f exe7utingZmrg1[9
exe7utingZmrg2[
-alse
^ZP+end[=exe7utingZmrg1++[
True
^ZP+end[=exe7utingZmrg2++[ True +eturn to
main program
T
-
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude "stdio8h9
6in7lude "stdlib8h9
6define N'^_'+T 1
void merge_sort:int xZ[= int end= int start;>
int main:void; <
int arJZN'^_'+T[>
int P = >
printf:?@n@nEnter the elements to be sorted: @n?;>
for:P=>P"N'^_'+T>P++;
s7anf:?&d?=AarJZP[;>
(C arraJ before mergesort C(
printf:?Iefore :?;>
for:P = > P " N'^_'+T> P++;
printf:? &d?= arJZP[;>
printf:?@n?;>
merge_sort:arJ= = N'^_'+T G 1;>
(C arraJ after mergesort C(
printf:?'fter Nerge Sort :?;>
for:P = > P " N'^_'+T> P++;
printf:? &d?= arJZP[;>
printf:?@n?;>
get7h:;>
E
(C Nethod to implement Nerge SortC(
void merge_sort:int xZ[= int end= int start; <
int P = >
7onst int size = start G end + 1>
int mid = >
int mrg1 = >
int mrg2 = >
int exe7utingZN'^_'+T[>
if:end == start;
return>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 23#
C & DATA STRUCTURE LAB PROGRAMS
mid = :end + start; ( 2>
merge_sort:x= end= mid;>
merge_sort:x= mid + 1= start;>
for:P = > P " size> P++;
exe7utingZP[ = xZend + P[>
mrg1 = >
mrg2 = mid G end + 1>
for:P = > P " size> P++; <
if:mrg2 "= start G end;
if:mrg1 "= mid G end;
if:exe7utingZmrg1[ 9 exe7utingZmrg2[;
xZP + end[ = exe7utingZmrg2++[>
else
xZP + end[ = exe7utingZmrg1++[>
else
xZP + end[ = exe7utingZmrg2++[>
else
xZP + end[ = exe7utingZmrg1++[>
E
E
Output:
Enter the elements to be sorted:
* 2 3 ! 1 $ # 6 F
Iefore : * 2 3 ! 1 $ # 6 F
'fter Nerge Sort : 1 2 3 ! $ 6 # * F
Enter the elements to be sorted:
# 6 $ ! * ! 3 2 1 3
Iefore : # 6 $ ! * ! 3 2 1 3
'fter Nerge Sort : 1 2 3 3 ! ! $ 6 # *
Conclusion: the program is error free
VIVA Q!"A#IO$"
1; /efine merge sort H
Ans: The merge sort splits the list to be sorted into tBo eKual halves= and pla7es
them in separate arraJs8 Ea7h arraJ is re7ursivelJ sorted= and then merged ba7D
together to form the final sorted list8
2; Effi7ien7J of merge sort H
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 23*
C & DATA STRUCTURE LAB PROGRAMS
'ns: O:n log n;8
Objective *):
To implements the .agrange interpolation and 0eBton )regorJ forBard interpolation
6agrange Interpolation :
Algorithm:
Step 18 +ead x=n
Step28 for i=1 to :n+1; is steps of 1 do +ead xi=fi end for <the above
statements reads x=s and the 7orresponding values of f is E
Step 38 Sum=
Step !8 for i=1 to :n+1; in steps of 1 do
Step $8 3rofvn7=1
Step 68 for Q=1 to :n+1; in steps of 1 do
Step #8 1f :Pci; then prodfun7=prodfun7 ^:xGxP; ( :xiGxP; endfor
Step *8 Sum=Sum+fi x 3rodfun7 <sum is the value of f at xE end for
Step F8 4rite x= sum
Step 18 ST23
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 23F
C & DATA STRUCTURE LAB PROGRAMS
Flow chart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!
C & DATA STRUCTURE LAB PROGRAMS
Program:
=inclu;e>st;io5h?
6in7lude"math8h9
Nain:;
<
-loat J= xZ2[=fZ2[=sum=pf>
1nt 1=P=n>
3rintf:Venter the value of nW;>
S7anf:V&dW=An;>
3rintf:Venter the value to be foundW;>
S7anf:V&fW=AJ;>
3rintf:Venter the values of xiYs A fiYsW;>
-or:i=>i"n>i++;
<
3f=1>
-or:P=>P"n>P++;
<
1f:P%=i;
3f C= :JGxZP[;(:xZi[ ] xZP[;>
E
Sum += fZi[ Cpf>
E
3rintf:V@nx = &f W=J;>
3rintf:V@n sum =&f W=sum;>
E
1nput(2utput:
Enter the value of n !
Enter the value to be found 28$
Enter the values for xiYs A fiYs
1 1
2 *
3 2#
! 6!
^ = 28$
Sum = 1$862$
Conclusion: The program is error free
VIVA Q!"A#IO$"
1; /efine storage 7lass H
'ns: Storage 7lass spe7ifiers inform the 7omplier hoB to store the variable> the
storage 7las spe7ifiers in the 7 language are : auto= register= stati7=extern= tJpedef
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!1
C & DATA STRUCTURE LAB PROGRAMS
$ewton gregor3 1orwar; interpolation5
Algorithm:
Step1: ST'+T
Step2: +ead n
Step3: for i= to :nG1; do read xi=Ji
Step!: read x
Step$: hdxiGx
Step6: pd:xGxo;(n
Step#: for P= to nG2 do
e1JPdJP+1GeiG1
Step*: DdnG2
StepF: for i=2 to :nG1;do
StepF81: DdDG1
StepF82:for P= to D do
eiJPdeiG1 JP+1GeiG1JP
Step1: SumJdJ
Step11: 3valued1
Step12: -a7t valued1
Step13: for l=1 to :nG1; do
Step1381: 3valuedpvalue x :pG:lG1;;
Step1382: fa7tvaluedfa7tvaluex1
Step1383: termd:pvalue x elJ; ( fa7tvalue
Step138!: SumJdSumJ+term
Step1!: 3rint x=S,NT
Step1$: ST23
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!2
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!3
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!!
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"math8h9
Nain:;
<
1nt i= P= n= D= l>
-loat sumJ= h= term= p= z= pvalue>
-loat xZ2$[= JZ2$[= dZ2$[Z2$[= fa7tvalue>
3rintf:Venter the value of nW;>
S7anf:V&dW=An;>
3rintf:Venter &d values for x= J @nW=n;>
-or:i=>i"n>i++;
S7anf:V&f &fW= AxZi[= AJZi[;>
3rintf:V@n enter zW;>
S7anf:V&fW=Az;>
h = xZ1[ ] xZ[>
p = :z G xZ[ ;( h>
for:P=> P"nG2> P++;
dZi[ZP[ =JZP+1[ ] JZP[>
D=nG2>
for:i=2> i"n> i++;
<
D++>
for:P=> P"=D> P++;
dZi[ZP[ =dZiG1[ZP+1[ ] dZiG1[ZP[>
E
-or:l=1> l"n> l++;
<
3value C= :pG:l G 1;;>
-a7tvalue C= 1>
Term = pvalueC dZl[Z[ ( fa7tvalue>
SumJ += term>
E
3rintf:V@n J value at z = &f is &fW= z= sumJ;>
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!$
C & DATA STRUCTURE LAB PROGRAMS
Input4Output:
Enter n #
Enter # data values for x= J
1F21 3$
1F31 !2
1F!1 $*
1F$1 *!
1F61 12
1F#1 16$
1F*1 22
Enter z 1F2$
T value at z = 1F2$8 is 368#$6#1
Conclusion: The program is error free
VIVA Q!"A#IO$"
1& 4hat is the use of goto statement H
Ans: The goto statement is used to alter the normal seKuen7e of the program exe7ution
bJ un7onditionallJ transferring 7ontrol to some other part of the program8
%& 4hat is the use of 7ontinue statement H
Ans: The 7ontinue statement is used to bJpass the remainder of the 7urrent pass through
a loop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!6
C & DATA STRUCTURE LAB PROGRAMS
Objective **:
1mplement in \MY the linear regression and polJnomial regression algorithms
6inear regression
Algorithm:
Step 18 +ead n
Step 28 Sumx=
Step 38 SumxsK=
Step !8 SumJ=
Step $8 SumxJ=
Step 68 fori=1 to n do
Step #8 +ead x=J
Step *8 Sumx=sumx+x
Step F8SumxsK=SumxsK+x2
Step 18SumJ=SumJ+J
Step 118SumxJ=sumxJ+x x J end for
Step 128 denom=n x sumxsK ] sumx x sumx
Step 138 a=:sumJ x sumxsK ] sumx x sumxJ; ( denom
Step 1!8 a1=:n x sumxJGsumx x sumJ;( denonm
Step 1$8 4rite a1=a
Step 168 ST23
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!#
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!*
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"math8h9
Nain:;
<
1nt n=1>
-loat sumx= sumxsK= sumJ= sumxJ= x= J= a= a1= denom>
3rintf:Venter the n valueW;>
S7anf:V&dW=An;>
Sumx=>
SumsK=>
SumJ=>
SumxJ=>
-or:i=>i"n>i++;
<
S7anf:V&f &fW=Ax= AJ;>
Sumx +=x>
SumsK += poB:x= 2;>
SumJ +=J>
SumxJ +=x C J>
E
/enom = n C sumxsK ] poB:sumx= 2;>
' = :sumJ C sumxsK ]sumx CsumxJ;(denom>
'1 = :n C sumxJ ]sumx CsumJ;(denom>
3rintf:VJ= &fx + &fW=a1=a;>
E
Input4Output:
Enter the n value #
1 2
2 $
! #
$ 1
6 12
* 1$
F 1F
T = 18F*#6Fx + 8F61$!
Conclusion: The program is error free
VIVA Q!"A#IO$"
1& 4hat is the use of goto statement H
Ans: The goto statement is used to alter the normal seKuen7e of the program exe7ution
bJ un7onditionallJ transferring 7ontrol to some other part of the program8
%& 4hat is the use of 7ontinue statement H
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2!F
C & DATA STRUCTURE LAB PROGRAMS
Ans: The 7ontinue statement is used to bJpass the remainder of the 7urrent pass through
a loop
Pol3nomial regression
Algorithm:
Sep 1: Strart
Step 2: +ead n
Step 3: 1nitialize sumx = = sumxsK = = sumJ = = sumxJ = = sumx3 = =
sumx! = = sumxsK =
Step !: 1ntialize i=
Step $: +epeat steps $ to # until i"n
Step 6: +ead x=J
Step #: Sumx = sumx + x
SumxsK =sumxsK + poB:x=2;
Sumx3 = sumx3 + poB:x=3;
Sumx! = sumx! + poB:x=!;
SumJ = sumJ + J
SumxJ = SumxJ + xCJ
SumxsKJ = SumxsKJ + poB:x=2; CJ
Step *: 1n7rement 1 bJ 1
Step F: 'ssign
aZ[Z[ = n
aZ[Z1[ = n
aZ[Z2[ = n
aZ[Z3[ = n
aZ1[Z[ = n
aZ1[Z1[ = n
aZ1[Z2[ = n
aZ1[Z3[ = n
aZ2[Z[ = n
aZ2[Z1[ = n
aZ2[Z2[ = n
aZ2[Z3[ = n

Step 1: 1ntialize i=
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$
C & DATA STRUCTURE LAB PROGRAMS
Step 11: +epeat steps 11 to 1$ until i"3
Step 12: 1ntialize P=
Step 13: +epeat step 13 to 1! until P"=3
Step 1!: 4rite aZi[ZP[
Step 1$: 1n7rement P bJ 1
Step 16: 1n7rement 1 bJ 1
Step 1#: 1nitialize D =
Step 1*: +epeat steps 1* to 2# until D"=2
Step 1F: 1ntialize i=
Step 2: +epeat step 2 to 26 until i"=2
Step 21: 1f 1 not eKual to D
Step 22: 'sign u=aZi[ZD[(aZD[ZD[
Step 23: 1ntialize P=D
Step 2!: +epeat steps 2! and 2$ until P"=3
Step 2$: 'ssign aZi[ZP[ = aZi[ZP[ ] u CaZD[ZP[
Step 26: 1n7rement P bJ 1
Step 2#: 1n7rement i bJ 1
Step 2*: 1n7rement D bJ 1
Step 2F: 1nitialize 1 =
Step 3: +epeat steps 31 to 33 until i"3
Step 31: 'ssign bZi[ = aZi[Z3[(aZi[Zi[
Step 32: 4rite 1= bZi[
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$1
C & DATA STRUCTURE LAB PROGRAMS
Step 33: 1n7rement 1 bJ 1
Step 3!: 4rite bZ2[=bZi[=bZ[
Step 3$: Stop
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$2
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$3
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
6in7lude"math8h9
main:;
<
1nt n= 1= P= D>
-loat sumx= sumxsK= sumJ= sumxJ= x= J>
-loat sumx3= sumx!= sumxsKJ= aZ2[Z2[= u=8= bZ2[>
3rintf:V@n Enter the n valueW;>
S7anf:V&dW= An;>
Sumx = >
SumxsK = >
SumJ = >
SumxJ = >
Sumx3 = >
Sumx! = >
SumxsKJ = >
-or:i=> i"n> i++;
<
S7anf:V&f &fW= Ax= AJ;>
Sumx +=x>
SumxsK += poB:x=2;>
Sumx3 += poB:x=3;>
Sumx! += poB:x=!;>
SumJ +=J>
SumxJ += x C J>
SumxsKJ += poB:x=2; CJ>
E
'Z[Z[ = n>
'Z[Z1[ = sumx>
'Z[Z2[ = sumxsK>
'Z[Z3[ = sumJ>
'Z1[Z[ = sumx>
'Z1[Z1[ = sumxsK>
'Z1[Z2[ = sumx3>
'Z1[Z3[ = sumxJ>
'Z2[Z[ = sumxsK>
'Z2[Z1[ = sumx3>
'Z2[Z2[ = sumx!>
'Z2[Z3[ = sumxsKJ>
for:i=> i"3> i++;
<
for:P=> P"=3> P++;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$!
C & DATA STRUCTURE LAB PROGRAMS
3rintf:V&182fW=aZi[ZP[;>
3rintf:V@nW;>
E
-or:D=> D"=2> D++;
<
-or:i=>i"=2>i++;
<
1f:i%=D;
,=aZi[ZD[(aZD[ZD[>
-or:P = D> P"=3> P++;
'Zi[ZP[=aZi[ZP[ ] u C aZD[ZP[>
E
E
-or:i=>i"3>i++;
<
IZi[ = aZi[Z3[(aZi[Zi[>
3rintf:V@nxZ&d[ = &fW= 1= bZi[;>
E
3rintf:V@nW;>
3rintf:VJ= &18!fx +18! fx +&18!fW=bZ2[=bZi[=bZ[;>
E
Input4Output:
Enter the n value 1
G! 21
G3 12
G2 !
G1 1
2
1 #
2 1$
3 3
! !$
$ 6#
18 $8 *$8 2!8
$8 *$8 12$8 $138
*$8 12$8 13338 31F38
^Z[ = 28333
^Z1[ = 28FF6F#
^Z2[ = 18F*!*!*
T = 18F*!*xsK + 28FF#Fx + 2833
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$$
C & DATA STRUCTURE LAB PROGRAMS
Mon7lusion: The program is error free
VIVA Q!"A#IO$"
1& /efine insertion sort H
Ans: 1nsertion sort is similar to plaJing 7ards8 To sort the 7ards in Jourhand Jou extrat a
7ard shift the remaining 7ards and then insert the extra7ted 7ard in its 7orre7t pla7e8
%& Effi7ien7J of the insertion sort H
Ans: The effi7ien7J of insertion sort is 2:n
2
;8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$6
C & DATA STRUCTURE LAB PROGRAMS
Objective *+:
3rogram to 1mplement Traezodial and simpson methods8
#rae@o;ial metho;:
Algorithm:
Step 18 +ead x1= x2= e < x1 and x2 are the tBo end points of the
internal the alloBed error in integral is eE
Step 28 h=x2Gx1
Step 38 S1 = :f:x1; + f:x2;;(2>
Step !8 1 = hGsi
Step $8 i=1 +epeat
Step 68 x=x1 + h(2
Step #8 for Q= 1 to 1 do
Step *8 S1= S1 + f:x;
Step F8 x=x+h
Endfor
Step 18 i=21
Step 118 h=h(2 < 0ote that the internal has been halved above and
the number of points Bhere the fun7tion has to be 7omputed
is doubledE
Step 128i=i1
Step 138 i1 = h8si
Step 1!8 until ( 11Gi ( "=78(i1(
Step 1$8 4rite 11=h=i
Step 168 Stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$#
C & DATA STRUCTURE LAB PROGRAMS
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$*
C & DATA STRUCTURE LAB PROGRAMS
Program:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2$F
C & DATA STRUCTURE LAB PROGRAMS
6in7lude"stdio8h9
6in7lude"math8h9
main:;
<
float h=a=b=n=xZ2[=JZ2[=sum==integral>
int i>
7lrs7r:;>
printf:?enter the value ofa=b=n:?;>
s7anf:?&f &f &f?=Aa=Ab=An;>
printf:?enter the values of x:?;>
for:i=>i"=:nG1;>i++;
<
s7anf:?&f?=AxZi[;>
E
printf:?@n enter the values of J:?;>
for:i=>i"=:nG1;>i++;
<
s7anf:?&f?=AJZi[;>
E
h=:bGa;(n>
xZ[=a>
for:i=1>i"=nG1>i++;
<
xZi[=xZiG1[+h>
sum=sum+2CJZi[>
E
sum=sum+JZb[>
integral=sumC:h(2;>
printf:?approximate integral value is: &f?=integral;>
get7h:;>
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 26
C & DATA STRUCTURE LAB PROGRAMS
Input4Output:
Enter the values of a=b=n
1
2
3
Enter the values of x:
1
2
3
Enter the values of J:
1
2
3
'pproximate integral value is 2816666#
Conclusion: The program is error free
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 261
C & DATA STRUCTURE LAB PROGRAMS
"impsons 9etho;:
Algorithm:
Step 18 +ead x1=x2=e
Step 28 h=:x2Gx1;(2
Step 38 i=2
Step !8 si=f:x1; + f:x2;
Step $8 s2=
Step 68 s!=f:x1+h;
Step #8 1=
Step *8 1n =:s+!s!;8 :h(3;
+epeat
Step F8 s2=s2+s! <s2 stores alreadJ 7omputed fun7tional value and s!
the value 7omputed in the neB nitration E
Step 18 s!=
Step 118 x=x1+h(2
Step 128 for P=1 to 1 do
Step 138 s!=s!+f:x;
Step 1!8 x=x+h
Step 1$8 h=h(2
Step 168 i=2i
Step 1#8 io=in
Step 1*8 in= :s1+2s2+!s!; 8 :h(3;
Step 1F8 until U1nG1oUfe8 (in
Step 28 4rite 1n=h=i
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 262
C & DATA STRUCTURE LAB PROGRAMS
Step 218 ST23
Flowchart:
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 263
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 26!
C & DATA STRUCTURE LAB PROGRAMS

Program:
6in7lude"stdio8h9
6in7lude"7onio8h9
6in7lude"math8h9
main:;
<
float h=a=b=n=xZ2[=JZ2[=sum==itgl>
int i>
7lrs7r:;>
printf:?enter the values of a=b=n?;>
s7anf:?&f&f&f?=Aa=Ab=An;>
printf:?enter the values of x?;>
for:i=>i"=n>i++;
<
s7anf:?&f?=AxZi[;>
E
printf:?@n enter the values of J?;>
for:i=>i"=n>i++;
<
s7anf:?&f?=AJZi[;>
E
h=:bGa;(n>
a=xZ[>
b=xZn[>
for:i=>i"=:nG2;>i++;
<
xZi[=xZi[+h>
if:i&2==;
<
sum=sum+!CJZi[>
E
else
<
sum=sum+2CJZi[>
E
E
itgl=sumC:h(3;>
printf:?integral value&f?=itgl;>
get7h:;>
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 26$
C & DATA STRUCTURE LAB PROGRAMS
Input4Output:
Enter the values of a=b=n
1
2
3
Enter the value of x
!
$
6
#
Enter the values of J
*
F
1
2
1ntegral value is $8$$$$$6
Conclusion: The program is error free
VIVA Q!"A#IO$"
1& /efine IinarJ sear7h H
Ans: IinarJ sear7h is a vast improvement over the seKuential sear7h8 -or binarJ sear7h to
BorD= the item in the list must be in assorted order8 The approa7h emploJed in the binarJ
sear7h is divid and 7onKuer8 1f the list to be sorted for a spe7ifi7 item is not sorted= binarJ
sear7h fails8
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 266
C & DATA STRUCTURE LAB PROGRAMS
ADDI#IO$A6 6AA !BP!8I9!$#"
Objective *,:
4rite a 7 program for heap sort
Description:
In this method= a tree stru7ture 7aed heap is used8 ' heap is tJpe of a
binarJ tree8 'n ordered baan7ed binarJ tree is 7aed a minGheap Bhere the vaue at the roo
of anJ sub tree is ess than or eKua to the vaue of either of its 7hidern8 5eap sort is
basi7allJ an improvement over the binarJ tree sort8

Algorithm:
2eap sort
S4'3 -,0MT120
18 start
28 assign Ca to temp
38 assign Cb to Ca
!8 assign temp to Cb
$8 stop
5E'3 S2+T
18 start
28 assign n to i and aZn[ to item
38 if i 9 1 and aZi(2[" item repeat through step ! other Bise goto
step $
begin
!8 assign aZi(2[ to aZi[ and i(2 to i
end if
$8 assign item to aZi[
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 26#
C & DATA STRUCTURE LAB PROGRAMS
68 stop
Flowchart:

VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 26*
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7lude"stdio8h9
int aZ2[>
main:;
<
int n=i>
7lrs7r:;>
printf:?Enter number of elements: ?;>
s7anf:?&d?=An;>
printf:?Enter &d elements: ?=n;>
for:i=1>i"=n>i++;
s7anf:?&d?=AaZi[;>
heapsort:n;>
printf:?Sorted elements are: @n?;>
for:i=1>i"=n>i++;
printf:?&3d?=aZi[;>
get7h:;>
E
heapsort:int n;
<
int t>
Bhile:n91;
<
maxheap:n;>
t=aZ1[>
aZ1[=aZn[>
aZn[=t>
n=nG1>
E
E
maxheap:int n;
<
int i=t=P>
for:i=2>i"=n>i++;
<
t=aZi[>
P=i>
Bhile:aZP(2["tAAP91;
<
aZP[=aZP(2[>
P=P(2>
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 26F
C & DATA STRUCTURE LAB PROGRAMS
E
aZP[=t>
E
E
Input4Output:
Enter number of elements: !
Enter ! elements: 23
!
12
*
Sorted elements are:
! * 12 23
Enter number of elements: 6
Enter 6 elements: 6#
23
6
!$
FF
#*
Sorted elements are:
6 23 !$ 6# #* FF
Mon7lusion:

The program is error free
VIVA Q!"A#IO$"
1& Drawbac< o1 the binar3 tree (
'ns: 'dditional spa7e is reKuired for building the tree
2; The 7omplexitJ of the heap sort algorithm H
Ans: OCn og n&
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2#
C & DATA STRUCTURE LAB PROGRAMS
Objective *-:
4rite a 7 program for sele7tioon sort
Description:
This is the simplest method of sorting8 1n this method= to sort the data in
as7ending order= the
th
element is 7ompared Bith all other eements8 1f the
th
element is
found to be greater than the 7ompared element then theJ are inter7hanged8
Algorithm:
1; Start
2; 1nitiliaze the variables 1=P=temp and arrZ[
3; +ead the loop and 7he7D the 7ondition8 1f the 7ondition is true
print the arraJ elements and in7rement the 1 value8 Else goto
step !
!; +ead the loop and 7he7D the 7ondition8 1f the 7ondition true
then goto next loop8
$; +ead the loop and 7he7D the 7ondition8 1f the 7ondition true
then goto if 7ondition
6; 1f the 7ondition if:arrZi[9arrZP[; is true then do the folloBing
steps
i; temp=arrZi[
ii; arrZi[=arrZP[
iii; arrZP[=temp
#; in7rement the P value
*; perform the loop operation for the displaJing the sorted
elements8
F; print the sorted elements
1; stop
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2#1
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2#2
C & DATA STRUCTURE LAB PROGRAMS
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2#3
satrt
1nitialize the
i=P=temp=arrZ[
1= i++
1"=!
3rint the arrZi[
elements
1= i++
1"=3
Q=P+1 P++
Q"=!
1f
arrZi[9arrZP[
Temp=arrZi[
'rrZi[=arrZP[
'rrZP[=temp
3rint arrZi[
stop
1= 1++
1"=!
F6O' C2A8#
C & DATA STRUCTURE LAB PROGRAMS
Program:
6in7ude"stdio8h9
6in7ude"7onio8h9
Ooid main:;
<
1nt arrZ$[=<2$=1#=31=13=2E>
1nt 1=P=temp>
Mlrs7r:;>
3rintf:Vsele7tion sort@nW;>
3rintf:V@n arraJ before sorting:@nW;>
-or:i=>i"=3>i++;
3rintf:V&d@t=arrZi[W;>
-or:i=>i"=3>i++;
<
-or:P=P+1>P"=!>P++;
<
1f:arrZi[9arrZP[;
<
Temp=arrZi[>
'rrZi[=arrZP[>
'rrZP[=temp>
E
E
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2#!
C & DATA STRUCTURE LAB PROGRAMS
E
3rintf:V@n@n arraJ after sortong:@nW;>
-or:i=>i"=!>i++;
3rintf:V&d@tW=arrZi[;>
)et7h:;>
E
Sampe input A output:
1; Se7tion sort
'rraJ before sorting:
2$ 1# 31 13 2
'rraJ after sorting:
2 13 1# 2$ 31
2; se7tion sort
'rraJ before sort
2$ 31 3 12 1
'rraJ after sort
1 12 2$ 3 31
Mon7usion: this program is error free
VIVA Q!"A#IO$"
1; The 7omplexitJ of the se7tion sort algorithm H
'ns: 2:n
2
;
2; 1; /raBba7D of the binarJ tree H
'ns: 'dditional spa7e is reKuired for building the tree
3; The 7omplexitJ of the heap sort algorithm H
'ns: 2:n og n;
VEMU INSTITUTE OF TECHNOLOGY :: DESIGNED BY G.V.S.ANANTH
NATH Page 2#$

You might also like