c-programming-and-data-structures-lab-manual.pdf
TRANSCRIPT
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
1/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
2/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
3/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
4/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
5/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
6/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
7/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
8/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
9/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
10/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
11/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
12/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
13/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
14/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
15/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
16/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
17/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
18/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
19/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
20/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
21/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
22/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
23/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
24/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
25/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
26/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
27/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
28/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
29/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
30/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
31/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
32/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
33/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
34/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
35/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
36/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
37/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
38/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
39/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
40/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
41/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
42/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
43/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
44/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
45/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
46/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
47/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
48/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
49/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
50/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
51/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
52/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
53/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
54/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
55/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
56/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
57/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
58/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
59/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
60/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
61/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
62/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
63/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
64/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
65/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
66/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
67/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
68/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
69/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
70/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
71/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
72/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
73/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
74/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
75/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
76/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
77/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
78/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
79/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
80/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
81/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
82/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
83/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
84/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
85/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
86/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
87/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
88/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
89/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
90/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
91/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
92/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
93/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
94/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
95/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
96/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
97/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
98/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
99/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
100/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
101/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
102/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
103/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
104/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
105/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
106/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
107/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
108/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
109/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
110/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
111/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
112/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
113/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
114/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
115/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
116/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
117/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
118/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
119/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
120/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
121/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
122/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
123/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
124/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
125/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
126/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
127/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
128/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
129/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
130/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
131/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
132/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
133/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
134/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
135/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
136/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
137/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
138/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
139/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
140/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
141/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
142/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
143/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
144/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
145/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
146/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
147/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
148/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
149/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
150/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
151/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
152/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
153/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
154/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
155/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
156/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
157/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
158/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
159/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
160/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
161/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
162/252
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
163/252
item=aZfront[>front++>
printf ?the deleted item from Kueue is &d@n? item;>
traverse ;if front==rear+1;
printf ?CCC Kueue is emptJ CCC@n?;>elsei"=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;>elseif i==front;
printf ?&d at &d G9front@n? aZi[ i;>else
printf ?&d at &d@n? aZi[ i;>
Input4Output:
CCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:1enter item into Kueue:11CCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:1enter item into Kueue:12CCC NE0, CCC 18 0SE T 0 28 /E.ET 0
163
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
164/252
38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:1enter item into Kueue:13CCC NE0, CCC
18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:1enter item into Kueue:1!CCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ T
enter Jour 7hoi7e:1CCC Kueue is full CCCCCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:311 at G9front12 at 113 at 21! at 3 G9rear CCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:2the deleted item from Kueue is 11CCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:2the deleted item from Kueue is 12CCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE
16!
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
165/252
!8 E^ Tenter Jour 7hoi7e:2the deleted item from Kueue is 13
CCC NE0, CCC
18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:2the deleted item from Kueue is 1!CCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ T
enter Jour 7hoi7e:2CCC Kueue is emptJ CCCCCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:3CCC Kueue is emptJ CCCCCC NE0, CCC 18 0SE T 0 28 /E.ET 0 38 T 'OE SE !8 E^ Tenter Jour 7hoi7e:!
conclusion: the program is error free
VIVA Q !"A#IO$":1) /efine Kueue HAns: ' Kueue is a linear seKuential list of that are a77essed in the oeder first in firstout - - ;82; /efine 7ir7ular Kueues HAns: ' Kueue 7an also be 7ir7ular in Bhi7h 7ase it is 7alled as a 7ir7ular Kueue3; 4hat are the various sta7D oriented notations HAns: i; infix ii; prefix iii; postfix
16$
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
166/252
Objective (*:rogram that implement `ueue operation bJ using the pointers
Description :n this program Be have to implement the `ueue operation bJ using the pointers8
5ere theJ `ueue operation are push and pop8 ush 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 elementii; 7reate a data s tru7tureiii; if emptJ Kueue then front of Kueue pinter points to neBlJ 7reated data
stru7tureiv; 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 emptJii; otherBise read the element pointed bJ front of the Kueue temp pointer
points to front of Kueueiii; front of Kueue points to next elementiv; free the element pointed bJ temp pointer v; return the elementvi; print the element
Step 6: if 7hoi7e = displaJi; 7he7D of emptJ Kueue if so print Kueue emptJii; otherBise print the elements from front of the Kueue until the end of the
Kueuestep #: if 7hoi7e=exits stop
166
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
167/252
start
/efine the stru7ture for Kueue
ead the 7hoi7e
f7hoi7e=insert
ead the elementto be inserted
f7hoi7e=remo
ve
f %e_Kue ;
rintKueue isemptJ
I
trG9n=Kueue
`ueue G9ele=ele`ueue G9n=0,..
ff_ptr=0,..
-+ptr=Kueue
I
tr=f_ptr ptr G9n%=0,..
tr = ptr G9 n
f7hoi7e=displaJ
tr=f_ptr
'
f
7hoi7e=exit
stop rint invalid7hoi7e
rint P
I
I
T -
-T
-
TT -
T
T
-
T-
Flowchart:
16#
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
168/252
program:define true 1define false
in7lude"stdio8h9
in7lude"7onio8h9in7lude"pro7ess8h9
stru7t K_point< int ele>
stru7t K_pointC n>>
stru7t K_point Cf_ptr = 0,..>
int e_Kue void;>void add_ele int;>int rem_ele void;>void shoB_ele ;>
(Cmain fun7tionC(void main ; 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: s7anf ?&d? Aele;>add_ele ele;>get7h ;>
breaD>
7ase 2:
printf ?@n@t&d is removed from the Kueue? P;> get7h ;>
else< printf ?@n@t`ueue is EmptJ8?;> get7h ;>
breaD>
7ase 3:
16F
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
170/252
shoB_ele ;>get7h ;>
breaD>
7ase !:
exit 1;> breaD>
default: printf ?@n@t nvalid 7hoi7e8?;>get7h ;>
breaD>
(C -un7tion to 7he7D if the Kueue is emptJC(int e_Kue void;< if f_ptr==0,..;
return true> return false>
(C -un7tion to add an element to the KueueC(void add_ele int ele; KueueG9ele = ele> KueueG9n = 0,..> if f_ptr==0,..; f_ptr = Kueue>
else ptr = f_ptr> for ptr=f_ptr >ptrG9n%=0,..> ptr=ptrG9n;> ptrG9n = Kueue>
(C -un7tion to remove an element from the KueueC(int rem_ele ;< stru7t K_pointC Kueue=0,..>
1#
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
171/252
if e_Kue ;==false; Kueue=f_ptr> f_ptr = f_ptrG9n>
free Kueue;> return P> else return GFFFF>
(C -un7tion to displaJ the KueueC(
void shoB_ele ;< stru7t K_point Cptr=0,..>
ptr=f_ptr> if e_Kue ;; return> else
< printf ?@n@tElements present in `ueue are:@n@t?;>
Bhile ptr%=0,..; < printf ?&d@t? ptrG9ele;> ptr=ptrG9n>
Output:
CCCC N .ENE0T'T 0 - `,E,E ,S 0) 0TE SCCCC============================================== NE0,============================================== Z1[ To insert an element Z2[ To remove an element Z3[ To displaJ all the elements
1#1
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
172/252
Z![ Exit
Enter Jour 7hoi7e:1
Element to be inserted:23
CCCC N .ENE0T'T 0 - `,E,E ,S 0) 0TE 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
CCCC N .ENE0T'T 0 - `,E,E ,S 0) 0TE 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 CCCC N .ENE0T'T 0 - `,E,E ,S 0) 0TE SCCCC============================================== NE0,============================================== Z1[ To insert an element Z2[ To remove an element Z3[ To displaJ all the elements Z![ Exit
Enter Jour 7hoi7e:! Exit
Conclusion : the program is error free
1#2
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
173/252
VIVA Q !"A#IO$":1) /efine Kueue HAns: ' Kueue is a linear seKuential list of that are a77essed in the oeder first in firstout - - ;82; /efine 7ir7ular Kueues H
Ans: ' Kueue 7an also be 7ir7ular in Bhi7h 7ase it is 7alled as a 7ir7ular Kueue3; 4hat are the various sta7D oriented notations HAns: i; infix ii; prefix iii; postfix
1#3
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
174/252
Objective (+: rogram that uses the sta7D operation to perform the folloBingi; 7onverting infix expression into postfix expressionii; evaluating the postfix expression
Description: n 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 sta7Doperations8 The propertJ of sta7Ds is last in first out8 i8e8 the item that is insertedlast Bill be the firt item remove8
A67O8I#29:
Step 18 startStep 28 first initialize the sta7D to be emptJStep 38 for ea7h 7hara7ter in the input string f 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 onthe topof sta7D or the top of the sta7D is opening parenthesisthen 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 sta7Dand append the operators
to the output until an opening parenthesis is en7ountered8 op theopening parenthesis from the sta7D
and dis7ard it8Step $8 if the end of the input string is en7ountered then iterate the loop until the
sta7D is not emptJ8 opthe sta7D and append the remaining input string to the output8
Step 68 stop
1#!
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
175/252
2 + $G*;(3 = 2$*G3(+nput 2 input $ input *
nput pop * $ push result input 3
nput 1 input +
2
$2
*
$
2
$2
2
G32
3
G3 2
$2
G1 2
1
1#$
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
176/252
Program:
in7lude"stdio8h9in7lude"7tJpe8h9
in7lude"string8h9stati7 7har strZ2 [>int top=G1>main ;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[>elseelse if inZi[==X;X;Bhile 7h=pop ;;%=X X;
postZP++[=7h>elsei++;t=insert t ele;>
breaD>7ase 2: (C EM, SS OE T 'OE SE C(
if t==0,..; printf ?CCCC T EE S EN T CCCC?;>elseinorder t;>
printf ?@n E /E :?;> preorder t;> printf ?@n ST /E :?;> postorder t;>
breaD>
1*F
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
190/252
7ase 3: (C 0 0G EM, SS OE T 'OE SE C(if t==0,..;
printf ?T EE S EN T ?;>elsenrinorder t;> printf ?@n E /E :?;>nrpreorder t;>
printf ?@n ST /E :?;>nrpostorder t;>
breaD>7ase !:
exit ;>
stru7t bstnode Cinsert stru7t bstnode Cx int J;xG9l7=0,..>xG9r7=0,..>
elsei"n>i++;
f m==DZi[
T
-
stop
-
T
1F6
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
197/252
Program:in7lude"stdio8h9
main ;
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++; breaD>
elseif i==n;
printf ?element &d not found in arraJ? DeJ;>
get7h ;>
Input4Output:enter range for arraJ:!enter elements into arraJ:$6!312**Fenter the sear7h element:Felement F found at !
enter range for arraJ:$enter elements into arraJ:2312$63!3*enter the sear7h element:3element 3 found at !conclusion: the program is error free
1F#
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
198/252
VIVA Q !"A#IO$":
1; /efine linear sear7h H'ns : The linear sear7h is most simple ser7hing method8 t 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 f a mat7h exists the sear7h is terminated8 f the end of list is rea7hed it means that
the sear7h has failed and DeJ has no mat7hing in the list8
1F*
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
199/252
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 f the list to be sorted for a spe7ifi7 item is not
sorted binarJ sear7h fails8
A67O8I#29:
I 0' SE' M5
18 Start28 ead the value of n38 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 sear7hF8 stop
I 0' SE' M5 -,0MT 0
18 start28 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 loBF8 return mid1 8 return 118stop
1FF
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
200/252
Flowchart:
2
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
201/252
2 1
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
202/252
Program:in7lude"stdio8h9
main ;
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++;
if aZmid[==DeJ; breaD>
if DeJ"aZmid[;high=mid>elseloB=mid+1>if i==nG1;
printf ?element &d not found in arraJ? DeJ;>
get7h ;>
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
conclusion: the program is error free
2 2
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
203/252
VIVA Q !"A#IO$"
1) /efine IinarJ sear7h HAns: 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 f the list to be sorted for a spe7ifi7 item is not sorted binarJ
sear7h fails8
2 3
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
204/252
Objective (/: rogram that implements the bubble sort methodDescription : Iubble sort is the simplest and oldest sorting te7hniKue8 This method taDestBo elements at a time8 t 7ompare these tBo elements8 f first elements is less than
se7ond one theJ are left undistrurbed8 f the first element is greater then se7ond one thentheJ are sBapped8 The pro7edure 7ontinues Bith the next tBo elements goes and endsBhen all the elements are sorted8 Iut bubble sort is an ineffi7ient algorithm8 The order of
bubble sort algorithm is n 2;8Algorithm: i;Iubble Sort:
18 start28 read the value of n38 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 arraJ68 stop
I,II.E S T -,0MT 0
18 start28 initialise last to n38 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
begin68 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 1endGif
end inner for loop118 if ex%=
assign lastG1 to last end for loop128 stop
2 !
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
205/252
Flowchart:
2 $
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
206/252
Program:
in7lude"stdio8h9main ;
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> printf ?the sorted order is:?;> for i= >i"n>i++; printf ?@t&d? aZi[;> get7h ;>
Input4Output:enter the range of arraJ:3enter elements into arraJ:321the sorted order is: 1 2 3
enter the range of arraJ:$enter elements into arraJ:$6233!12*the sorted order is: * 12 23 3! $6
conclusion: The program is error free
VIVA Q !"A#IO$"
2 6
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
207/252
1; /efine bubble sort H'ns: : Iubble sort is the simplest and oldest sorting te7hniKue8 This method taDes tBoelements at a time8 t 7ompare these tBo elements8 f first elements is less than se7ondone theJ are left undistrurbed8 f the first element is greater then se7ond one then theJ aresBapped8 The pro7edure 7ontinues Bith the next tBo elements goes and ends Bhen all the
elements are sorted82; displaJ the effi7ien7J of bubble sort HAns : n2;
Objective *0:
2 #
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
208/252
rogram that implements the `ui7D sort methodDescription : This method is invented bJ hoare 7onsidered to be fast method to sort theelements8 The method is also 7alled partition ex7hange sorting8 The method is based ondivide and 7onKuer te7hniKue8 i8e8 the entire list is divided into various partitions andsorting is applied again and again on the partition8
n this method the list is divided into tBo baesd on an element 7alled pivotelement8 ,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 elementare 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 start28 if loBerbound " upperbound repeat through steps 3 to 13 otherBise
goto step 1! begin38 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 # begin68 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 !
Iegin1 8 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
Flowchart:
2 *
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
209/252
Program:
2 F
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
210/252
in7lude"stdio8h9main ;
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 ;>
Kui7Dsort int xZ1 [ int first int last;if first"last;i=first>
P=last>Bhile i"P; Bhile xZP[9xZpivot[;
PGG> if i"P; xZi[=xZP[> xZP[=t>
t=xZpivot[> xZpivot[=xZP[> xZP[=t> Kui7Dsort x first PG1;> Kui7Dsort x P+1 last;>
21
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
211/252
CCCCC ,T ,T CCCCC
enter no of elements:6
enter 6 elements:2312!$3!21*#sorted elements are: 12 21 23 3! !$ *#conclusion: The program is error free
VIVA Q !"A#IO$"
1) /efine Kui7D sort HAns:This method is invented bJ hoare 7onsidered to be fast method to sort the elements8 Themethod is also 7alled partition ex7hange sorting8 The method is based on divide and7onKuer te7hniKue8 i8e8 the entire list is divided into various partitions and sorting isapplied again and again on the partition8 n this method the list is divided into tBo baesd on an element 7alled pivotelement8 ,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 elementare 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 HAns: n log n;
Objective *1:P rogram that implements the insertion sort method
211
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
212/252
Description: nsertion sort is similar to plaJing 7ards8 To sort the 7ards in Jourhand Jouextrat a 7ard shift the remaining 7ards and then insert the extra7ted 7ard in its 7orre7t
pla7e8 The effi7ien7J of insertion sort is n 2;8
Algorithm:
ii; nsertion Sort:18 start28 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
Flowchart:
212
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
213/252
Program:
213
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
214/252
in7lude"stdio8h9main ;
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++;for p=i>p9 AA aZpG1[9t>pGG;aZp[=aZpG1[>
aZp[=t> printf ?the sorted order is:?;>for i= >i"n>i++;
printf ?@t&d? aZi[;>get7h ;>
CCCCC ,T ,T CCCCC
enter the range of arraJ:$enter elements into arraJ:$!321the sorted order is: 1 2 3 ! $
enter the range of arraJ:6enter elements into arraJ:2312*F!$6#3!the sorted order is: 12 23 3! !$ 6# *F
conclusion: The program is error free
VIVA Q !"A#IO$"
21!
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
215/252
1) /efine insertion sort HAns: nsertion sort is similar to plaJing 7ards8 To sort the 7ards in Jourhand Jou extrat a7ard 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 n2
;8
Objective *%:
21$
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
216/252
P rogram 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 togetherto form the final sorted list8 .iDe most re7ursive sorts the merge sort has analgorithmi7 7omplexitJ of O n log n ;8
Algorithm: main programStep1: StartStep2: de7lare the merge sort fun7tionStep3: /e7lare the arraJ and their size and initailaze the P=Step!: read the arraJ elements and then sort these elements8Step$: read the arraJ elements before the merge sort and then displaJ the
elements8Step6: 7all the merge sort fun7tionStep#: displaJ the arraJ elements after merge sort bJ using the folloBing stament8 for P= >P"Nax_arJ>P++;Step*: Stop"ubprogramStep1:initialize the arraJ ex7utingZN'^_' [ and
P= mid= mrg1= mrg2= size=startGend+1Step2: 7he7D the 7ondition if end==start; then returnStep3: 7al7ulate the mid value
Nid= end+start;(2Step!: 7all themerge_sort x end mid;Step$:merge_sort x mid+1 start;Step6: performing the looping operation
-or P= >P"S E>P++; then its true Exe7utingZP[=xZend+1[Nrg1= >Step#: 7al7ulate the mrg2=midGend+1Step*: performing the looping operation
-or P= >P"S E>P++; then its true then goto stepFStepF: 7he7D the 7ondition
i; if mrg2"=startGend; is true goto ii;8 f not goto Step128ii; f mrg1"=midGend; is true goto iii;8 f not goto step11iii; f exe7utingZmrg1[9exe7utingZmrg2[; is true then folloBs8
f not goto step1 8^ZP+end[= exe7utingZmrg2++[
Step1 : xZP+end[=exe7utingZmrg1++[8 f not goto Step11Step11: xZP+end[= exe7utingZmrg2++[Step12: xZP+end[=exe7utingZmrg1++[Step13: return to main program
Flow chart:
216
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
217/252
Start
/e7lare the arraJ and arraJ size
ead the arraJ elements
/efine merge_sort fun7tion
Q= P++
P"N'^_'
/isplaJ theelements beforemerge sort
Nergesort arJ N'^_' G1;
Q= P++
P"N'^_'
/isplaJ theelements after
merge sort
True
-alse
True
-alse
Stop
21#
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
218/252
Nerge_sort ;
Size=startGend+1Nid= Nrg1= Nrg2=
f 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
f mrg2"=startGend
-alse
^ZP+end[=exe7utingZmrg1++[
Truef mrg1"=midG
end
-alse
^ZP+end[=exe7utingZmrg2++[
f exe7utingZmrg1[9exe7utingZmrg2[
-alse
^ZP+end[=exe7utingZmrg1++[
^ZP+end[=exe7utingZmrg2++[ Trueeturn tomain program
T
-
21*
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
219/252
Program:in7lude "stdio8h9in7lude "stdlib8h9
define N'^_' 1
void merge_sort int xZ[ int end int start;>
int main void; < int arJZN'^_' [> int P = >
printf ?@n@nEnter the elements to be sorted: @n?;> for P= >P"N'^_' >P++; s7anf ?&d? AarJZP[;>
(C arraJ before mergesort C( printf ?Iefore :?;> for P = > P " N'^_' > P++; printf ? &d? arJZP[;>
printf ?@n?;>
merge_sort arJ N'^_' G 1;>
(C arraJ after mergesort C( printf ?'fter Nerge Sort :?;> for P = > P " N'^_' > P++; printf ? &d? arJZP[;>
printf ?@n?;> get7h ;>
(C Nethod to implement Nerge SortC(void merge_sort int xZ[ int end int start; 7onst int size = start G end + 1> int mid = > int mrg1 = > int mrg2 = > int exe7utingZN'^_' [>
if end == start; return>
21F
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
220/252
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++[>
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 3Iefore : # 6 $ ! * ! 3 2 1 3'fter Nerge Sort : 1 2 3 3 ! ! $ 6 # *Conclusion: the program is error freeVIVA Q !"A#IO$"
1; /efine merge sort H
Ans: The merge sort splits the list to be sorted into tBo eKual halves and pla7esthem in separate arraJs8 Ea7h arraJ is re7ursivelJ sorted and then merged ba7Dtogether to form the final sorted list8
2; Effi7ien7J of merge sort H'ns: O n log n ;8
22
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
221/252
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
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
222/252
Flow chart:
222
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
223/252
Program:
=inclu;e>st;io5h?in7lude"math8h9
Nain ;
< -loat J xZ2 [ fZ2 [ sum pf> nt P n> rintf Venter the value of nW;> S7anf V&dW An;> rintf Venter the value to be foundW;> S7anf V&fW AJ;> rintf Venter the values of xiYs A fiYsW;> -or i= >i"n>i++; < f=1>
-or P= >P"n>P++;
Sum += fZi[ Cpf>
rintf V@nx = &f W J;>rintf V@n sum =&f W sum;>
nput( utput:
Enter the value of n !Enter the value to be found 28$Enter the values for xiYs A fiYs
1 12 *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> thestorage 7las spe7ifiers in the 7 language are : auto register stati7 extern tJpedef
223
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
224/252
$ewton gregor3 orwar; interpolation5
Algorithm:
Step1: ST' T
Step2: ead n
Step3: for i= to nG1; do read xi Ji
Step!: read x
Step$: h xiGx
Step6: p xGxo;(nStep#: for P= to nG2 do
1JP JP+1G iG1
Step*: D nG2
StepF: for i=2 to nG1;do StepF81: D DG1 StepF82:for P= to D do iJP iG1 JP+1G iG1JP
Step1 : SumJ J
Step11: value 1
Step12: -a7t value 1
Step13: for l=1 to nG1; doStep1381: value pvalue x pG lG1;;
Step1382: fa7tvalue fa7tvaluex1Step1383: term pvalue x lJ; ( fa7tvalueStep138!: SumJ SumJ+term
Step1!: rint x S,N
Step1$: ST
Flowchart:
22!
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
225/252
Program:
22$
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
226/252
in7lude"stdio8h9in7lude"math8h9
Nain ; -loat sumJ h term p z pvalue> -loat xZ2$[ JZ2$[ dZ2$[Z2$[ fa7tvalue> rintf Venter the value of nW;> S7anf V&dW An;> rintf Venter &d values for x J @nW n;> -or i= >i"n>i++; S7anf V&f &fW AxZi[ AJZi[;> rintf 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++; for P= > P"=D> P++;
dZi[ZP[ =dZiG1[ZP+1[ ] dZiG1[ZP[> -or l=1> l"n> l++; -a7tvalue C= 1>Term = pvalueC dZl[Z [ ( fa7tvalue>SumJ += term>
rintf V@n J value at z = &f is &fW z sumJ;>
Input4Output:
226
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
227/252
Enter n #Enter # data values for x J
1F21 3$
1F31 !21F!1 $*1F$1 *!1F61 12
1F#1 16$1F*1 22
Enter z 1F2$ 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 HAns: 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 HAns: The 7ontinue statement is used to bJpass the remainder of the 7urrent pass througha loop
Objective **:
22#
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
228/252
mplement 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 doStep #8 ead x J
Step *8 Sumx=sumx+x
Step F8SumxsK=SumxsK+x2
Step 1 8SumJ=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 ST
22*
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
229/252
22F
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
230/252
Program:
in7lude"stdio8h9in7lude"math8h9
Nain ;
< nt n >-loat sumx sumxsK sumJ sumxJ x J a a1 denom>
rintf Venter the n valueW;> S7anf V&dW An;>
Sumx= >SumsK= >SumJ= >SumxJ= >-or i= >i"n>i++;Sumx +=x>SumsK += poB x 2;>SumJ +=J>SumxJ +=x C J>
/enom = n C sumxsK ] poB sumx 2;>' = sumJ C sumxsK ]sumx CsumxJ;(denom>'1 = n C sumxJ ]sumx CsumJ;(denom>
rintf VJ= &fx + &fW a1 a ;>
Input4Output:Enter the n value #1 22 $! #$ 16 12* 1$F 1F
= 18F* #6Fx + 8 F61$!Conclusion: The program is error free
VIVA Q !"A#IO$"
1) 4hat is the use of goto statement HAns: 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 HAns: The 7ontinue statement is used to bJpass the remainder of the 7urrent pass througha loop
23
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
231/252
Pol3nomial regression
Algorithm:
Sep 1: Strart
Step 2: ead n
Step 3: nitialize sumx = sumxsK = sumJ = sumxJ = sumx3 = sumx! = sumxsK =
Step !: ntialize i=
Step $: epeat steps $ to # until i"n
Step 6: ead x JStep #: 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 *: n7rement bJ 1
Step F: 'ssignaZ [Z [ = n
aZ [Z1[ = naZ [Z2[ = naZ [Z3[ = naZ1[Z [ = naZ1[Z1[ = naZ1[Z2[ = naZ1[Z3[ = naZ2[Z [ = naZ2[Z1[ = naZ2[Z2[ = naZ2[Z3[ = n
Step 1 : ntialize i=
Step 11: epeat steps 11 to 1$ until i"3
231
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
232/252
Step 12: ntialize P=
Step 13: epeat step 13 to 1! until P"=3
Step 1!: 4rite aZi[ZP[
Step 1$: n7rement P bJ 1
Step 16: n7rement bJ 1
Step 1#: nitialize D =
Step 1*: epeat steps 1* to 2# until D"=2
Step 1F: ntialize i=
Step 2 : epeat step 2 to 26 until i"=2Step 21: f not eKual to D
Step 22: 'sign u=aZi[ZD[(aZD[ZD[
Step 23: ntialize P=D
Step 2!: epeat steps 2! and 2$ until P"=3
Step 2$: 'ssign aZi[ZP[ = aZi[ZP[ ] u CaZD[ZP[
Step 26: n7rement P bJ 1
Step 2#: n7rement i bJ 1
Step 2*: n7rement D bJ 1
Step 2F: nitialize =
Step 3 : epeat steps 31 to 33 until i"3
Step 31: 'ssign bZi[ = aZi[Z3[(aZi[Zi[
Step 32: 4rite bZi[
Step 33: n7rement bJ 1
Step 3!: 4rite bZ2[ bZi[ bZ [Step 3$: Stop
232
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
233/252
Flowchart:
233
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
234/252
Program:
in7lude"stdio8h9in7lude"math8h9
main ;
-loat sumx sumxsK sumJ sumxJ x J>-loat sumx3 sumx! sumxsKJ aZ2 [Z2 [ u= 8 bZ2 [>
rintf V@n Enter the n valueW;>S7anf V&dW An;>Sumx = >SumxsK = >SumJ = >SumxJ = >
Sumx3 = >Sumx! = >SumxsKJ = >-or i= > i"n> i++;Sumx +=x>SumxsK += poB x 2;>Sumx3 += poB x 3;>Sumx! += poB x !;>SumJ +=J>SumxJ += x C J>SumxsKJ += poB x 2; CJ>
'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++; P"=3> P++;rintf V&1 82fW aZi[ZP[;>
23!
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
235/252
rintf V@nW;> -or D= > D"=2> D++; i"=2>i++;
< f i%=D; ,=aZi[ZD[(aZD[ZD[>-or P = D> P"=3> P++;
'Zi[ZP[=aZi[ZP[ ] u C aZD[ZP[>
-or i= >i"3>i++;
rintf V@nxZ&d[ = &fW bZi[;> rintf V@nW;> rintf VJ= &1 8!fx +1 8! fx +&1 8!fW bZ2[ bZi[ bZ [;>
Input4Output:
Enter the n value 1
G! 21G3 12G2 !G1 1 2 1 # 2 1$ 3 3 ! !$ $ 6#
1 8 $8 *$8 2 !8 $8 *$8 12$8 $138*$8 12$8 13338 31F38
^Z [ = 28 3 3 3^Z1[ = 28FF6F#^Z2[ = 18F*!*!*
= 18F*!*xsK + 28FF#Fx + 28 3 3Mon7lusion: The program is error free
23$
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
236/252
VIVA Q !"A#IO$"
1) /efine insertion sort HAns: nsertion sort is similar to plaJing 7ards8 To sort the 7ards in Jourhand Jou extrat a7ard shift the remaining 7ards and then insert the extra7ted 7ard in its 7orre7t pla7e8
%)Effi7ien7J of the insertion sort HAns: The effi7ien7J of insertion sort is n 2;8
236
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
237/252
Objective *+: rogram to mplement Traezodial and simpson methods8
#rae@o;ial metho;:
Algorithm:
Step 18 ead x1 x2 e < x1 and x2 are the tBo end points of theinternal the alloBed error in integral is e
Step 28 h=x2Gx1
Step 38 S = f x1; + f x2;;(2>
Step !8 = hGsi
Step $8 i=1 epeatStep 68 x=x1 + h(2
Step #8 for Q= 1 to do
Step *8 S = S + f x;
Step F8 x=x+hEndfor
Step 1 8 i=21
Step 118 h=h(2 < 0ote that the internal has been halved above andthe number of points Bhere the fun7tion has to be 7omputedis doubled
Step 128i =i1
Step 138 i1 = h8si
Step 1!8 until ( 1Gi ( "=78(i1(
Step 1$8 4rite 1 h i
Step 168 Stop
23#
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
238/252
Flowchart:
23*
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
239/252
Program:
in7lude"stdio8h9in7lude"math8h9
main ;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++;
printf ?@n enter the values of J:?;>for i= >i"= nG1;>i++;
h= bGa;(n>xZ [=a>for i=1>i"=nG1>i++;sum=sum+2CJZi[>
sum=sum+JZb[>integral=sumC h(2;>
printf ?approximate integral value is: &f? integral;>get7h ;>
23F
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
240/252
Input4Output:
Enter the values of a b n12
3Enter the values of x:123Enter the values of J:123'pproximate integral value is 2816666#
Conclusion: The program is error free
2!
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
241/252
"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 =Step *8 n = s+!s!;8 h(3; epeat
Step F8 s2=s2+s!
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
242/252
Flowchart:
2!2
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
243/252
Program:
in7lude"stdio8h9in7lude"7onio8h9
in7lude"math8h9main ;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++; printf ?@n enter the values of J?;>for i= >i"=n>i++;
h= bGa;(n>a=xZ [>
b=xZn[>for i= >i"= nG2;>i++;if i&2== ;
else
itgl=sumC h(3;> printf ?integral value&f? itgl;>get7h ;>
2!3
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
244/252
Input4Output:
Enter the values of a b n1
23Enter the value of x!$6#Enter the values of J*F1
2ntegral value is $8$$$$$6
Conclusion: The program is error free
VIVA Q !"A#IO$"
1) /efine IinarJ sear7h HAns: 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 f the list to be sorted for a spe7ifi7 item is not sorted binarJ
sear7h fails8
2!!
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
245/252
ADDI#IO$A6 6A !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 rooof 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' -,0MT 0
18 start
28 assign Ca to temp
38 assign Cb to Ca
!8 assign temp to Cb
$8 stop
5E' S 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 gotostep $
begin
!8 assign aZi(2[ to aZi[ and i(2 to iend if
$8 assign item to aZi[
68 stop
2!$
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
246/252
Flowchart:
2!6
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
247/252
Program:in7lude"stdio8h9
int aZ2 [>main ;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 ;>heapsort int n;Bhile n91;t=aZ1[>aZ1[=aZn[>aZn[=t>n=nG1>
maxheap int n;for i=2>i"=n>i++; P=i>Bhile aZP(2["tAAP91;
P=P(2>
aZP[=t>
2!#
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
248/252
Input4Output:
Enter number of elements: !Enter ! elements: 23
!12*Sorted elements are: ! * 12 23
Enter number of elements: 6Enter 6 elements: 6#236
!$FF#*Sorted elements are: 6 23 !$ 6# #* FF
Mon7lusion:
The program is error free
VIVA Q !"A#IO$"
1) Drawbac< o the binar3 tree ''ns: 'dditional spa7e is reKuired for building the tree
2; The 7omplexitJ of the heap sort algorithm HAns: O n og n)
2!*
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
249/252
Objective *-:4rite a 7 program for sele7tioon sort
Description: This is the simplest method of sorting8 n this method to sort the data in
as7ending order the th
element is 7ompared Bith all other eements8 f the th
element isfound to be greater than the 7ompared element then theJ are inter7hanged8
Algorithm:1; Start2; nitiliaze the variables P temp and arrZ[3; ead the loop and 7he7D the 7ondition8 f the 7ondition is true
print the arraJ elements and in7rement the value8 Else gotostep !
!; ead the loop and 7he7D the 7ondition8 f the 7ondition truethen goto next loop8
$; ead the loop and 7he7D the 7ondition8 f the 7ondition truethen goto if 7ondition6; f the 7ondition if arrZi[9arrZP[; is true then do the folloBing
stepsi; temp=arrZi[ii; arrZi[=arrZP[iii; arrZP[=temp#; in7rement the P value*; perform the loop operation for the displaJing the sorted
elements8F; print the sorted elements1 ; stop
2!F
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
250/252
satrt
nitialize the
i P temp arrZ[
= i++"=!
rint the arrZi[elements
= i++"=3
Q=P+1 P++Q"=!
farrZi[9arrZP[
Temp=arrZi[ 'rrZi[=arrZP[ 'rrZP[=temp
rint arrZi[stop
= ++"=!
F6O& C2A8#
2$
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
251/252
Program:
in7ude"stdio8h9
in7ude"7onio8h9
Ooid main ;
Mlrs7r ;>
rintf Vsele7tion sort@nW;>rintf V@n arraJ before sorting:@nW;>
-or i= >i"=3>i++;
rintf V&d@t arrZi[W;>
-or i= >i"=3>i++;
P"=!>P++;
2$1
-
7/25/2019 C-Programming-and-Data-Structures-Lab-Manual.pdf
252/252
rintf V@n@n arraJ after sortong:@nW;>
-or i= >i"=!>i++;
rintf V&d@tW arrZi[;>
2$2