Anda di halaman 1dari 27

AIM: Implement functions of Dictionary using Hashing (division method,

Multiplicationmethod, Universal hashing)


PROGRAM:
#include<stdio.h
#include<conio.h
#include<process.h
int hsearch(int !ey,int d,int "ht,int "empty)
#
int i$!ey%(d)&
int '$i&
int c$(&
do
#
if(empty)'*++("(ht,')$$!ey))
return '&
c,,&
'$(i,c)%(d)&
-.hile('/$i)&
return (&
-
int search(int !ey,int d,int "ht,int "empty)
#
int 0$hsearch(!ey,d,ht,empty)&
if(empty)0*)
return 12&
else
return 0&
-
void insert(int !ey,int d,int "ht,int "empty)
#
int 0$hsearch(!ey,d,ht,empty)&
if(empty)0*)
#
empty)0*$(&
"(ht,0)$!ey&
printf(34lement is inserted5n3)&
-
-
void delete2(int !ey,int d,int "ht,int" empty)
#
int 0$hsearch(!ey,d,ht,empty)&
"(ht,0)$(&
empty)0*$2&
printf(34lement is delted5n3)&
-
void display(int d,int "ht,int "empty)
#
int i&
printf(3Hash ta0le elements are5n3)&
for(i$(&i<d&i,,)
#
if(empty)i*)
printf(3 (3)&
else
printf(3%6d3,"(ht,i))&
-
printf(35n3)&
-
void main()
#
int choice$2&
int !ey&
int d,i&
int "empty,"ht&
clrscr()&
printf(34nter the hash ta0le si7e83)&
scanf(3%d3,9d)&
ht$(int")malloc(d"si7eof(int))&
empty$(int")malloc(d"si7eof(int))&
for(i$(&i<d&i,,)
empty)i*$2&
.hile(2)
#
printf(35n3)&
printf(35n :I;4<= >=?@I;A3)&
printf(35n2.insert hash ta0le3)&
printf(35nB.delete hash ta0le3)&
printf(35nC.search hash ta0le3)&
printf(35nD.display hash ta0le3)&
printf(35n6.4Eit3)&
printf(34nter your choice83)&
scanf(3%d3,9choice)&
s.itch(choice)
#
case 28
printf(34nter the element83)&
scanf(3%d3,9!ey)&
insert(!ey,d,ht,empty)&
0rea!&
case B8
printf(34nter to remove from hash ta0le83)&
scanf(3%d3,9!ey)&
delete2(!ey,d,ht,empty)&
0rea!&
case C8
printf(34nter the search element83)&
scanf(3%d3,9!ey)&
if(search(!ey,d,ht,empty)$$()
printf(3;ot found5n3)&
else
printf(34lement found at indeE
%d3,hsearch(!ey,d,ht,empty))&
0rea!&
case D8
display(d,ht,empty)&
0rea!&
case 68
eEit(()&
-
getch()&
-
-
OUTPUT:
LINEAR PROBING
2.insert hash ta0le
B.delete hash ta0le
C.search hash ta0le
D.display hash ta0le
6.4Eit
4nter your choice82
4nter the element822
4lement is inserted
LINEAR PROBING
2.insert hash ta0le
B.delete hash ta0le
C.search hash ta0le
D.display hash ta0le
6.4Eit
4nter your choice82
4nter the element8BB
4lement is inserted
LINEAR PROBING
2.insert hash ta0le
B.delete hash ta0le
C.search hash ta0le
D.display hash ta0le
6.4Eit
4nter your choice8D
Hash ta0le elements are
22 BB ( ( ( ( ( ( (
( (
LINEAR PROBING
2.insert hash ta0le
B.delete hash ta0le
C.search hash ta0le
D.display hash ta0le
6.4Eit
4nter your choice8B
4nter to remove from hash ta0le822
4lement is delted
LINEAR PROBING
2.insert hash ta0le
B.delete hash ta0le
C.search hash ta0le
D.display hash ta0le
6.4Eit
4nter your choice8D
Hash ta0le elements are
( BB ( ( ( ( ( (
( ( (
LINEAR PROBING
2.insert hash ta0le
B.delete hash ta0le
C.search hash ta0le
D.display hash ta0le
6.4Eit
4nter your choice8C
4nter the search element822
;ot found
AIM : Fo perform various operations i.e, insertions and deletions on <G: trees
PROGRAM:
#include<stdio.h
#include<malloc.h
#deHne IH<;A4D (
#deHne @<:<;I4D 2
int height&
struct 0node
#
int data,0factor&
struct 0node "left,"right&
-&
typedef struct 0node node&
node" getnode()
#
int si7e&
node" ne.node&
si7e$si7eof(node)&
ne.node$(node")malloc(si7e)&
return(ne.node)&
-
void copynode(node "r,int data)
#
r1data$data&
r1left$;U::&
r1right$;U::&
r10factor$(&
-
void releasenode(node "p)
#
free(p)&
-
node" searchnode(node "root,int data)
#
if(root/$;U::)
if(data<root1data)
root$searchnode(root1left,data)&
else if(dataroot1data)
root$searchnode(root1right,data)&
return(root)&
-
void lefttoleft(node "">ptr,node ""<ptr)
#
node "p$">ptr,"<$"<ptr&
printf(35n:eft to left <G: =otation5n3)&
p1left$<1right&
<1right$p&
if(<10factor$$()
#
p10factor$2&
<10factor$12&
height$@<:<;I4D&
-
else
#
p10factor$(&
<10factor$(&
-
p$<&
">ptr$p&
"<ptr$<&
-
void lefttoright(node "">ptr,node ""<ptr, node ""@ptr)
#
node "p$">ptr, "<$"<ptr, "@$"@ptr&
printf(35n :eft to =ight <G: =otation 5n3)&
@$<1right&
<1right$@1left&
@1left$<&
p1left$@1right&
@1right$p&
if(@10factor$$2)
p10factor$12&
else
p10factor$(&
if(@10factor$$12)
<10factor$2&
else
<10factor$(&
@10factor$(&
p$@&
">ptr$p&
"<ptr$<&
"@ptr$@&
-
void righttoright(node "">ptr, node ""<ptr)
#
node "p$">ptr, "<$"<ptr&
printf(35n =ight to =ight <G: =otation 5n3)&
p1right$<1left&
<1left$p&
if(<10factor$$()
#
p10factor$12&
<10factor$2&
height$@<:<;I4D&
-
else
#
p10factor$(&
<10factor$(&
-
p$<&
">ptr$p&
"<ptr$<&
-
void righttoleft(node "">ptr, node ""<ptr, node ""@ptr)
#
node "p$">ptr, "<$"<ptr, "@$"@ptr&
printf(35n =ight to :eft <G: =otation 5n3)&
@$<1left&
<1left$@1right&
@1right$<&
p1right$@1left&
@1left$p&
if(@10factor$$12)
p10factor$2&
else
p10factor$(&
if(@10factor$$2)
<10factor$12&
else
<10factor$(&
@10factor$(&
p$@&
">ptr$p&
"<ptr$<&
"@ptr$@&
-
node" insertnode(int data,node" p)
#
node "<,"@&
if(p$$;U::)
#
p$getnode()&
copynode(p,data)&
height$IH<;A4D&
return(p)&
-
if(data<p1data)
#
p1left$insertnode(data,p1left)&
if(height$$IH<;A4D)
#
s.itch(p10factor)
#
case 128
p10factor$(& JJright heavy tree
height$@<:<;I4D&
0rea!&
case (8
p10factor$2& JJ0alanced tree
0rea!&
case 28
<$p1left&
if(<10factor$$2)
lefttoleft(9p,9<)&
else
lefttoright(9p,9<,9@)&
height$@<:<;I4D&
0rea!&
-
-
-
if(datap1data)
#
p1right$insertnode(data,p1right)&
if(height$$IH<;A4D)
#
s.itch(p10factor)
#
case 28
p10factor$(& JJleft heavy trees
height$@<:<;I4D&
0rea!&
case (8
p10factor$12& JJ0alanaced trees
0rea!&
case 128
<$p1right& JJright heavy trees
if(<10factor$$12)
righttoright(9p,9<)&
else
righttoleft(9p,9<,9@)&
height$IH<;A4D&
0rea!&
-
-
-
return(p)&
-
void del(node "";,node ""I)
#
node "F,"<,"@&
node ""p&
F$(";)&
if((";)1right/$;U::)
#
del(9((";)1right),I)&
if(height$$IH<;A4D)
#
p$;&
s.itch(("p)10factor)
#
case 128
("p)10factor$(&
0rea!&
case (8
("p)10factor$2&
height$@<:<;I4D&
0rea!&
case 28
<$("p)1left&
if(<10factor$()
lefttoleft(p,9<)&
else
lefttoright(p,9<,9@)&
0rea!&
-
-
-
else
#
("I)1data$(";)1data&
(";)$(";)1left&
releasenode(F)&
height$IH<;A4D&
-
-
void deletenode(int data,node ""p)
#
node "<,"@,"I&
if("p$$;U::)
#
printf(35n<G: Free is empty3)&
return&
-
if(data<("p)1data)
#
deletenode(data,9(("p)1left))&
if(height$$IH<;A4D)
#
s.itch(("p)10factor)
#
case 28
("p)10factor$(&
0rea!&
case (8
("p)10factor$2&
height$@<:<;I4D&
0rea!&
case 128
<$("p)1right&
if(<10factor<$()
righttoright(p,9<)&
else
righttoleft(p,9<,9@)&
0rea!&
-
-
-
else if(data("p)1data)
#
deletenode(data,9(("p)1right))&
if(height$$IH<;A4D)
#
s.itch(("p)10factor)
#
case 128
("p)10factor$(&
0rea!&
case (8
("p)10factor$2&
height$@<:<;I4D&
0rea!&
case 28
<$("p)1left&
if(<10factor$()
lefttoleft(p,9<)&
else
lefttoright(p,9<,9@)&
0rea!&
-
-
-
else
#
I$"p&
if(I1right$$;U::)
#
"p$I1left&
height$IH<;A4D&
releasenode(I)&
-
else if(I1left$$;U::)
#
"p$I1right&
height$IH<;A4D&
releasenode(I)&
-
else
#
del(9(I1left),9I)&
if(height$$IH<;A4D)
#
s.itch(("p)10factor)
#
case 28
("p)10factor$(&
0rea!&
case (8
("p)10factor$12&
height$@<:<;I4D&
0rea!&
case 128
<$("p)1right&
if(<10factor<$()
righttoright(p,9<)&
else
righttoleft(p,9<,9@)&
0rea!&
-
-
-
-
-

void inorder(node "root)
#
if(root$$;U::)
return&
inorder(root1left)&
printf(3%Dd3,root1data)&
inorder(root1right)&
-

void main()
#
int data,ch,choice$KyK&
node "root$;U::&
printf(35n@asic operations in an <G: Free...3)&
printf(35n2.Insert a node in the <G: Free3)&
printf(35nB.Delete a node in the <G: Free3)&
printf(35nC.Gie. the <G: Free3)&
printf(35nD.4Eit3)&
.hile((choice$$KyK)++(choice$$KLK))
#
printf(35n3)&
Mush(stdin)&
scanf(3%d3,9ch)&
s.itch(ch)
#
case 28
printf(35n4nter the value to 0e inserted83)&
scanf(3%d3,9data)&
if(searchnode(root,data)$$;U::)
root$insertnode(data,root)&
else
printf(35nData already eEists3)&
0rea!&
case B8
printf(34nter the value to 0e deleted83)&
scanf(3%d3,9data)&
if(searchnode(root,data)/$;U::)
deletenode(data,9root)&
else
printf(35n4lement to 0e deleted is not found3)&
0rea!&
case C8
if(root$$;U::)
#
printf(35n<G: Free is 4moty5n3)&
continue&
-
printf(35nInorder Fraversal of the <G: Free83)&
inorder(root)&
0rea!&
default8
printf(35n4nd of run of your program...3)&
releasenode(root)&
return&
-
-
-
OUTPUT:
@asic operations in an <G: Free...
2.Insert a node in the <G: Free
B.Delete a node in the <G: Free
C.Gie. the <G: Free
D.4Eit
2
4nter the value to 0e inserted8BC
2
4nter the value to 0e inserted826
C
Inorder Fraversal of the <G: Free8 26 BC
4nter the value to 0e inserted82C
:eft to left <G: =otation
C
Inorder Fraversal of the <G: Free8 2C 26
BC
4nter the value to 0e inserted8BN
2
4nter the value to 0e inserted8B(
2
4nter the value to 0e inserted82O
=ight to :eft <G: =otation
4nter the value to 0e inserted8CB
=ight to =ight <G: =otation
C
Inorder Fraversal of the <G: Free8 2C 26
2O B( BC BN CB
4nter the value to 0e inserted8P
2
4nter the value to 0e inserted82B
:eft to =ight <G: =otation
C
Inorder Fraversal of the <G: Free8 P 2B 2C
26 2O B( BC BN CB
AIM: Fo perform various operations i.e., insertions and deletions on B1C trees.
PROGRAM:
J">rogram of insertion and deletion in @ tree"J
#include<stdio.h
#include <conio.h
#deHne M 6
struct node#
int n& J" n < M ;o. of !eys in node .ill al.ays less than order of @
tree "J
int !eys)M12*& J"array of !eys"J
struct node "p)M*& J" (n,2 pointers .ill 0e in use) "J
-"root$;U::&
enum QeyRtatus # Duplicate,RearchSailure,Ruccess,InsertIt,:essQeys -&
void insert(int !ey)&
void display(struct node "root,int)&
void Del;ode(int E)&
void search(int E)&
enum QeyRtatus ins(struct node "r, int E, int" y, struct node "" u)&
int search>os(int E,int "!eyTarr, int n)&
enum QeyRtatus del(struct node "r, int E)&
int main()
#
int !ey&
int choice&
printf(3Ireation of @ tree for node %d5n3,M)&
.hile(2)
#
printf(32.Insert5n3)&
printf(3B.Delete5n3)&
printf(3C.Rearch5n3)&
printf(3D.Display5n3)&
printf(36.Uuit5n3)&
printf(34nter your choice 8 3)&
scanf(3%d3,9choice)&
s.itch(choice)
#
case 28
printf(34nter the !ey 8 3)&
scanf(3%d3,9!ey)&
insert(!ey)&
0rea!&
case B8
printf(34nter the !ey 8 3)&
scanf(3%d3,9!ey)&
Del;ode(!ey)&
0rea!&
case C8
printf(34nter the !ey 8 3)&
scanf(3%d3,9!ey)&
search(!ey)&
0rea!&
case D8
printf(3@tree is 85n3)&
display(root,()&
0rea!&
case 68
eEit(2)&
default8
printf(3Vrong choice5n3)&
0rea!&
-J"4nd of s.itch"J
-J"4nd of .hile"J
JJreturn (&
-J"4nd of main()"J
void insert(int !ey)
#
struct node "ne.node&
int upQey&
enum QeyRtatus value&
value $ ins(root, !ey, 9upQey, 9ne.node)&
if (value $$ Duplicate)
printf(3Qey already availa0le5n3)&
if (value $$ InsertIt)
#
struct node " uproot $ root&
root$(struct node ")malloc(si7eof(struct node))&
root1n $ 2&
root1!eys)(* $ upQey&
root1p)(* $ uproot&
root1p)2* $ ne.node&
-J"4nd of if "J
-J"4nd of insert()"J
enum QeyRtatus ins(struct node "ptr, int !ey, int "upQey,struct node ""ne.node)
#
struct node "ne.>tr, "last>tr&
int pos, i, n,split>os&
int ne.Qey, lastQey&
enum QeyRtatus value&
if (ptr $$ ;U::)
#
"ne.node $ ;U::&
"upQey $ !ey&
return InsertIt&
-
n $ ptr1n&
pos $ search>os(!ey, ptr1!eys, n)&
if (pos < n 99 !ey $$ ptr1!eys)pos*)
return Duplicate&
value $ ins(ptr1p)pos*, !ey, 9ne.Qey, 9ne.>tr)&
if (value /$ InsertIt)
return value&
J"If !eys in node is less than M12 .here M is order of @ tree"J
if (n < M 1 2)
#
pos $ search>os(ne.Qey, ptr1!eys, n)&
J"Rhifting the !ey and pointer right for inserting the ne. !ey"J
for (i$n& ipos& i11)
#
ptr1!eys)i* $ ptr1!eys)i12*&
ptr1p)i,2* $ ptr1p)i*&
-
J"Qey is inserted at eEact location"J
ptr1!eys)pos* $ ne.Qey&
ptr1p)pos,2* $ ne.>tr&
,,ptr1n& J"incrementing the num0er of !eys in node"J
return Ruccess&
-J"4nd of if "J
J"If !eys in nodes are maEimum and position of node to 0e inserted is
last"J
if (pos $$ M 1 2)
#
lastQey $ ne.Qey&
last>tr $ ne.>tr&
-
else J"If !eys in node are maEimum and position of node to 0e inserted
is not last"J
#
lastQey $ ptr1!eys)M1B*&
last>tr $ ptr1p)M12*&
for (i$M1B& ipos& i11)
#
ptr1!eys)i* $ ptr1!eys)i12*&
ptr1p)i,2* $ ptr1p)i*&
-
ptr1!eys)pos* $ ne.Qey&
ptr1p)pos,2* $ ne.>tr&
-
split>os $ (M 1 2)JB&
("upQey) $ ptr1!eys)split>os*&
("ne.node)$(struct node ")malloc(si7eof(struct node))&J"=ight node after split"J
ptr1n $ split>os& J";o. of !eys for left splitted node"J
("ne.node)1n $ M121split>os&J";o. of !eys for right splitted node"J
for (i$(& i < ("ne.node)1n& i,,)
#
("ne.node)1p)i* $ ptr1p)i , split>os , 2*&
if(i < ("ne.node)1n 1 2)
("ne.node)1!eys)i* $ ptr1!eys)i , split>os , 2*&
else
("ne.node)1!eys)i* $ lastQey&
-
("ne.node)1p)("ne.node)1n* $ last>tr&
return InsertIt&
-J"4nd of ins()"J
void display(struct node "ptr, int 0lan!s)
#
if (ptr)
#
int i&
for(i$2&i<$0lan!s&i,,)
printf(3 3)&
for (i$(& i < ptr1n& i,,)
printf(3%d 3,ptr1!eys)i*)&
printf(35n3)&
for (i$(& i <$ ptr1n& i,,)
display(ptr1p)i*, 0lan!s,2()&
-J"4nd of if"J
-J"4nd of display()"J
void search(int !ey)
#
int pos, i, n&
struct node "ptr $ root&
printf(3Rearch path85n3)&
.hile (ptr)
#
n $ ptr1n&
for (i$(& i < ptr1n& i,,)
printf(3 %d3,ptr1!eys)i*)&
printf(35n3)&
pos $ search>os(!ey, ptr1!eys, n)&
if (pos < n 99 !ey $$ ptr1!eys)pos*)
#
printf(3Qey %d found in position %d of last dispalyednode5n3,!ey,i)&
return&
-
ptr $ ptr1p)pos*&
-
printf(3Qey %d is not availa0le5n3,!ey)&
-J"4nd of search()"J
int search>os(int !ey, int "!eyTarr, int n)
#
int pos$(&
.hile (pos < n 99 !ey !eyTarr)pos*)
pos,,&
return pos&
-J"4nd of search>os()"J
void Del;ode(int !ey)
#
struct node "uproot&
enum QeyRtatus value&
value $ del(root,!ey)&
s.itch (value)
#
case RearchSailure8
printf(3Qey %d is not availa0le5n3,!ey)&
0rea!&
case :essQeys8
uproot $ root&
root $ root1p)(*&
free(uproot)&
0rea!&
-J"4nd of s.itch"J
-J"4nd of delnode()"J
enum QeyRtatus del(struct node "ptr, int !ey)
#
int pos, i, pivot, n ,min&
int "!eyTarr&
enum QeyRtatus value&
struct node ""p,"lptr,"rptr&
if (ptr $$ ;U::)
return RearchSailure&
J"<ssigns values of node"J
n$ptr1n&
!eyTarr $ ptr1!eys&
p $ ptr1p&
min $ (M 1 2)JB&J"Minimum num0er of !eys"J
pos $ search>os(!ey, !eyTarr, n)&
if (p)(* $$ ;U::)
#
if (pos $$ n ++ !ey < !eyTarr)pos*)
return RearchSailure&
J"Rhift !eys and pointers left"J
for (i$pos,2& i < n& i,,)
#
!eyTarr)i12* $ !eyTarr)i*&
p)i* $ p)i,2*&
-
return 11ptr1n $ (ptr$$root W 2 8 min) W Ruccess 8 :essQeys&
-J"4nd of if "J
if (pos < n 99 !ey $$ !eyTarr)pos*)
#
struct node "Xp $ p)pos*, "Xp2&
int n!ey&
.hile(2)
#
n!ey $ Xp1n&
Xp2 $ Xp1p)n!ey*&
if (Xp2 $$ ;U::)
0rea!&
Xp $ Xp2&
-J"4nd of .hile"J
!eyTarr)pos* $ Xp1!eys)n!ey12*&
Xp1!eys)n!ey 1 2* $ !ey&
-J"4nd of if "J
value $ del(p)pos*, !ey)&
if (value /$ :essQeys)
return value&
if (pos ( 99 p)pos12*1n min)
#
pivot $ pos 1 2& J"pivot for left and right node"J
lptr $ p)pivot*&
rptr $ p)pos*&
J"<ssigns values for right node"J
rptr1p)rptr1n , 2* $ rptr1p)rptr1n*&
for (i$rptr1n& i(& i11)
#
rptr1!eys)i* $ rptr1!eys)i12*&
rptr1p)i* $ rptr1p)i12*&
-
rptr1n,,&
rptr1!eys)(* $ !eyTarr)pivot*&
rptr1p)(* $ lptr1p)lptr1n*&
!eyTarr)pivot* $ lptr1!eys)11lptr1n*&
return Ruccess&
-J"4nd of if "J
if (pos min)
#
pivot $ pos& J"pivot for left and right node"J
lptr $ p)pivot*&
rptr $ p)pivot,2*&
J"<ssigns values for left node"J
lptr1!eys)lptr1n* $ !eyTarr)pivot*&
lptr1p)lptr1n , 2* $ rptr1p)(*&
!eyTarr)pivot* $ rptr1!eys)(*&
lptr1n,,&
rptr1n11&
for (i$(& i < rptr1n& i,,)
#
rptr1!eys)i* $ rptr1!eys)i,2*&
rptr1p)i* $ rptr1p)i,2*&
-J"4nd of for"J
rptr1p)rptr1n* $ rptr1p)rptr1n , 2*&
return Ruccess&
-J"4nd of if "J
if(pos $$ n)
pivot $ pos12&
else
pivot $ pos&
lptr $ p)pivot*&
rptr $ p)pivot,2*&
J"merge right node .ith left node"J
lptr1!eys)lptr1n* $ !eyTarr)pivot*&
lptr1p)lptr1n , 2* $ rptr1p)(*&
for (i$(& i < rptr1n& i,,)
#
lptr1!eys)lptr1n , 2 , i* $ rptr1!eys)i*&
lptr1p)lptr1n , B , i* $ rptr1p)i,2*&
-
lptr1n $ lptr1n , rptr1n ,2&
free(rptr)& J"=emove right node"J
for (i$pos,2& i < n& i,,)
#
!eyTarr)i12* $ !eyTarr)i*&
p)i* $ p)i,2*&
-
return 11ptr1n $ (ptr $$ root W 2 8 min) W Ruccess 8 :essQeys&
-J"4nd of del()"J
OUTPUT:
Ireation of @ tree for node 6 2.Insert
B.Delete
C.Rearch
D.Display
6.Uuit
4nter your choice 8 2
4nter the !ey 8 2(
2.Insert
B.Delete
C.Rearch
D.Display
6.Uuit
4nter your choice 8 2
4nter the !ey 8 B(
2.Insert
B.Delete
C.Rearch
D.Display
6.Uuit
4nter your choice 8 D
@tree is 8
2( B(
2.Insert
B.Delete
C.Rearch
D.Display
6.Uuit
4nter your choice 8 B
4nter the !ey 8 2(
2.Insert
B.Delete
C.Rearch
D.Display
6.Uuit
4nter your choice 8 D
@tree is 8
B(
2.Insert
B.Delete
C.Rearch
D.Display
6.Uuit
4nter your choice 86
AIM: Fo implement operations on 0inary heap
PROGRAM:
#include<stdio.h
#include<conio.h
#include<process.h
#deHne arraylength B(
int heapsi7e$(,heap)arraylength*&
void enXueue(int element)
#
int currentnode&
if(heapsi7e$$arraylength12)
#
printf(3<rray is full3)&
return&
-
currentnode$,,heapsi7e&
.hile(currentnode/$(99heap)currentnodeJB*<element)
#
heap)currentnode*$heap)currentnodeJB*&
currentnodeJ$B&
-
heap)currentnode*$element&
-
void deXueue()
#
int lastelement,currentnode,child&
if(heapsi7e$$()
#
printf(3>riority Xueue is empty3)&
return&
-
heap)(*$(&
lastelement$heap)heapsi7e11*&
currentnode$2&
child$B&
.hile(child<$heapsi7e)
#
if(child<heapsi7e99heap)child*<heap)child,2*)
child,,&
if(lastelement$heap)child*)
0rea!&
heap)currentnode*$heap)child*&
currentnode$child&
child"$B&
-
heap)currentnode*$lastelement&
-
void display()
#
int i&
for(i$(&i<arraylength&i,,)
#
printf(3%6d3,heap)i*)&
-
-
void main()
#
int opt,ele&
.hile(2)
#
clrscr()&
printf(35n.............3)&
printf(35n2.4nXueue3)&
printf(35nB.DeXueue3)&
printf(35nC.Display3)&
printf(35nD.4Eit3)&
printf(35n............3)&
printf(35n4nter ur option83)&
scanf(3%d3,9opt)&
s.itch(opt)
#
case 28
printf(34nter the element to insert83)&
scanf(3%d3,9ele)&
enXueue(ele)&
0rea!&
case B8
deXueue()&
printf(34lement is deleted3)&
0rea!&
case C8
display()&
0rea!&
case D8
eEit(()&
-
getch()&
- -
OUTPUT:

.............
2.4nXueue
B.DeXueue
C.Display
D.4Eit
............
4nter ur option82
4nter the element to
insert8 2(
.............
2.4nXueue
B.DeXueue
C.Display
D.4Eit
............
4nter ur option82
4nter the element to
insert8 6
.............
2.4nXueue
B.DeXueue
C.Display
D.4Eit
............
4nter ur option82
4nter the element to
insert8 2O
.............
2.4nXueue
B.DeXueue
C.Display
D.4Eit
............
4nter ur option82
4nter the element to
insert8 BD
.............
2.4nXueue
B.DeXueue
C.Display
D.4Eit
............
4nter ur option8C
BD 2O 2( 6 ( ( (
( ( ( ( ( ( (
( ( ( ( ( (
AIM: Fo implement operations on graphs
i) verteE insertion ii) GerteE deletion iii) Hnding verteE iv)4dge addition and deletion
PROGRAM:
J">rogram to create,insert,delete and vie. the ad'ecency matriE"J
#include<stdio.h
#include<conio.h
#deHne GRIY4 B(
int chec!Vt()&
int chec!Dir()&
void insertGerteE ()&
void deleteGerteE(int vDel)&
void insert4dge(int vRtart,int v4nd)&
void delete4dge(int vRtart, int v4nd)&
void createAraph()&
void vie.Araph()&
void displayTmenu()&
int nGerteE,ad'Mat)GRIY4*)GRIY4*&
void main()
#
char choice$KyK&
int ch,vs,ve,vd&
clrscr()&
displayTmenu()&
.hile((choice$$KyK)++(choice$$KyK))
# printf(35nW3)&
Mush(stdin)&
scanf(3%d3,9ch)&
s.itch(ch)
#
case ( 8displayTmenu()&
0rea!&
case 2 8createAraph()&
0rea!&
case B 8insertGerteE()&
0rea!&
case C 8printf(35n enter the starting 9 ending verteE to insert an edge 83)&
scanf(3%d %d3,9vs,9ve)&
insert4dge(vs,ve)&
0rea!&
case D 8printf(35n enter the verteE to delete 83)&
scanf(3%d3,9vd)&
deleteGerteE(vd)&
0rea!&
case 6 8printf(35n enter the starting 9 ending verteE to delete an edge 83)&
scanf(3%d%d3,9vs,9ve)&
0rea!&
case Z 8vie.Araph()&
0rea!&
case N 8printf(35n end of run of your program...........3)&
eEit(()&
-
-
-
void insertGerteE()
# int rc&
nGerteE,,&
for(rc$(&rc<nGerteE&rc,,)
ad'Mat)rc* )nGerteE12*$ad'Mat)nGerteE12* )rc*$(&
-
void insert4dge(int vRtart,int v4nd)
# int ie&
if(vRtartnGerteE++vRtart<2++v4ndnGerteE++v4nd<2)
return&
printf(3enter .eight of the 4dge from v%d to v%d 83, vRtart ,v4nd)&
scanf(3%d3,9ad'Mat )vRtart12* )v4nd12*)&
-
void deleteGerteE(int vDel)
# int r,c&
if(vDelnGerteE ++ vDel<2)
return&
for(r$vDel12&r<nGerteE& r,,)
for(c$(&c<nGerteE&c,,)
ad'Mat)r*)c*$ad'Mat)r,2*)c*&
for(c$vDel12&r<nGerteE&c,,)
for(r$(&r<nGerteE&r,,)
ad'Mat)r*)c*$ad'Mat)r*)c,2*&
nGerteE11&
-
void delete4dge(int vRtart,int v4nd)
# if(vRtartnGerteE ++ vRtart<2 ++ v4ndnGerteE++ v4nd<2)
return&
if(/chec!Dir()) ad'Mat )vRtart12* )v4nd12* $(&
-
int chec!Dir()
# int r,c&
for(r$(&r<nGerteE&r,,)
for(c$(&c<nGerteE&c,,)
if(ad'Mat)r*)c*/$ad'Mat)c*)r*)
return 2&
return (&
-
int chec!Vt()
# int r,c&
for(r$(&r<nGerteE&r,,)
for(c$(&c<nGerteE&c,,)
if(ad'Mat)r*)c*2)
return 2&
return (&
-
void createAraph()
# int r,c&
printf(35n enter the no. of vertices 8 3)&
scanf(3%d3,9nGerteE)&
for(r$(&r<nGerteE&r,,)
for(c$(&c<nGerteE&c,,)
# ad'Mat)r*)c*$(&
if(r/$c)
insert4dge(r,2,c,2)&
-
-
void vie.Araph()
#
int v,r,c,edge,inDeg)GRIY4*,outDeg)GRIY4*&
for(v$(&v<nGerteE&v,,)
printf(3 v%d3,v,2)&
for(r$(&r<nGerteE&r,,)
# printf(35nv%1Bd 3,r,2)&
for(c$(&c<nGerteE&c,,)
printf(3%1Bd 3,ad'Mat)r*)c*)&
-
for(v$(&v<nGerteE&v,,)
inDeg)v*$outDeg)v*$(&
edge$(&
for(r$(&r<nGerteE&r,,)
for(c$(&c<nGerteE&c,,)
if(ad'Mat)r*)c*/$()
# edge,,&
outDeg)r*,,&
inDeg)c*,,&
-
if(/chec!Dir())
edge$edgeJB&
printf(3n %s Araph3,(chec!Dir())W3DI=4IF4D3 8 3U;DI=4IF4D3)&
printf(3n %s Araph3,(chec!Dir())W3Veighted3 8 3U;Veighted3)&
printf(3total no. of vertices 8%d3,nGerteE)&
printf(35ntotal no.of 4dges 8 %d3,edge)&
printf(3GerteE Indegree ?utdegree3)&
for(r$(&r<nGerteE&r,,)
# printf(35n v%1Bd %1Pd %1Pd3,r,2,inDeg)r*,outDeg)r*)&
if(inDeg)r*$$( 99 outDeg)r*/$()
printf(38 %s3,3R?U=43)&
if(inDeg)r*/$( 99 outDeg)r*$$()
printf(38 %s3,3RQI;3)&
if(inDeg)r*$$2 99 outDeg)r*$$()
printf(38 %s3,3>4;D<;F3)&
if(inDeg)r*$$( 99 outDeg)r*$$()
printf(38 %s3,3IR?:<F4D3)&
-
-
void displayTmenu()
#
printf(35n5n 0asic operation in an ad'acency matriE..........3)&
printf(35n5t (. Display Menu3)&
printf(35n5t 2.Ireation of Araph3)&
printf(35n5t B. Insert a GerteE3)&
printf(35n5t C. Insert an 4dge3)&
printf(35n5t D. Delete aGerteE3)&
printf(35n5t 6. Delete an 4dge 3)&
printf(35n5t Z. Gei. the Araph3)&
printf(35n5t N. 4Eit3)&
-
OUTPUT:
0asic operation in an ad'acency
matriE..........
(. Display Menu
2.Ireation of Araph
B. Insert a GerteE
C. Insert an 4dge
D. Delete aGerteE
6. Delete an 4dge
Z. Gei. the Araph
N. 4Eit
W2
enter the no. of vertices 8 6
enter .eight of the 4dge from v2 to vB 82
enter .eight of the 4dge from v2 to vC 8(
enter .eight of the 4dge from v2 to vD 82
enter .eight of the 4dge from v2 to v6 82
enter .eight of the 4dge from vB to v2 82
enter .eight of the 4dge from vB to vC 82
enter .eight of the 4dge from vB to vD 82
enter .eight of the 4dge from vB to v6 8(
enter .eight of the 4dge from vC to v2 8(
enter .eight of the 4dge from vC to vB 82
enter .eight of the 4dge from vC to vD 82
enter .eight of the 4dge from vC to v6 82
enter .eight of the 4dge from vD to v2 82
enter .eight of the 4dge from vD to vB 82
enter .eight of the 4dge from vD to vC 82
enter .eight of the 4dge from vD to v6 82
enter .eight of the 4dge from v6 to v2 82
enter .eight of the 4dge from v6 to vB 8(
enter .eight of the 4dge from v6 to vC 82
enter .eight of the 4dge from v6 to vD 82
WZ
v2 vB vC vD v6
v2 ( 2 ( 2 2
vB 2 ( 2 2 (
vC ( 2 ( 2 2
vD 2 2 2 ( 2
v6 2 ( 2 2 (
n U;DI=4IF4D Araphn U;Veighted A
raphtotal no. of vertices 86
total no.of 4dges 8 OGerteE Indegree
?utdegree
v2 C C
vB C C
vC C C
vD D D
v6 C C
W N

AIM: Fo implement Depth Sirst Rearch for a graph non recursively
PROGRAM:
#include<stdio.h
#include<conio.h
int 0fs)B(*,rear$12,front$12,vt)B(*&
void store(int)&
int remove()&
void store(int n)
#
0fs),,rear*$n&
-
int removeele()
#
int n&
n$0fs),,front*&
return n&
-
void 0fsearch(int a)*)2(*,int n,int id)
#
int i,'&
for(i$(&i<n&i,,)
#
vt)i*$(&
-
id$id12&
store(id)&
vt)id*$2&
printf(3Gisited nodes are83)&
.hile(front/$rear)
#
id$removeele()&
printf(3%6d3,id,2)&
for(i$(&i<n&i,,)
#
if(a)id*)i*$$2)
#
if(vt)i*$$()
#
store(i)&
vt)i*$2&
-
-
-
-
-
void main()
#
int a)2(*)2(*,n,i,',id$2&
clrscr()&
printf(34nter no.of nodes83)&
scanf(3%d3,9n)&
printf(34nter ad'acency matriE83)&
for(i$(&i<n&i,,)
#
for('$(&'<n&',,)
#
scanf(3%d3,9a)i*)'*)&
-
-
printf(34nter starting node83)&
scanf(3%d3,9id)&
0fsearch(a,n,id)&
getch()&
-
OUTPUT:
4nter no.of nodes8D
4nter ad'acency matriE8
( 2 2 (
2 ( ( 2
2 ( ( 2
( 2 2 (
4nter starting node82
Gisited nodes are8 2 B C D
AIM: Fo implement @readth Sirst Rearch for a graph nonrecursively
PROGRAM:
#include<stdio.h
#include<conio.h
int vt)2(*,dfs)2(*,top$12&
void push(int)&
int pop()&
void push(int n)
#
dfs),,top*$n&
-
int pop()
#
int n&
n$dfs)top11*&
return n&
-
void dfsearch(int a)*)2(*,int n,int id)
#
int i,'&
for(i$(&i<n&i,,)
#
vt)i*$(&
-
id$id12&
push(id)&
vt)id*$12&
printf(3Gisited nodes are83)&
.hile(top/$12)
#
id$pop()&
printf(3%6d3,id,2)&
for(i$(&i<n&i,,)
#
if(a)id*)i*$$2)
#
if(vt)i*$$()
#
push(i)&
vt)i*$2&
-
-
-
-
-
void main()
#
int a)2(*)2(*,m,i,',id$(,n&
clrscr()&
printf(34nter no.of elements83)&
scanf(3%d3,9n)&
printf(34nter the ad'acency matriE83)&
for(i$(&i<n&i,,)
#
for('$(&'<n&',,)
#
scanf(3%d3,9a)i*)'*)&
-
-
printf(34nter searching node83)&
scanf(3%d3,9id)&
dfsearch(a,n,id)&
getch()&
-
OUTPUT:
4nter no.of elements8D
4nter the ad'acency matriE8
( 2 2 (
2 ( ( 2
2 ( ( 2
( 2 2 (
4nter searching node82
Gisited nodes are8 2 C D B

Anda mungkin juga menyukai