8/3/2019 Ds Program Record
1/57
Program Code:
//********SINGLE LINKED LIST********//
#include
#includevoid display();
void creat();void start();
void middle();
void end();void delete();
void insert();
void dmiddle();struct list
{
int item;
struct list*link;};
struct list*head,*n,*list,*dum;
int i,ch;void main()
{
clrscr();printf("SAMPLE INPUT AND OUTPUT:");
printf("\n\t\t SINGLE LINKED LIST \n\n\n");
printf(\n\t\t---------------------\n\n\n);while(1)
{
printf("\t\t\t MAIN MENU\n\n\n");
printf("1.Create\n2.Insert\n 3.Deletion\n");printf("4.Display\n5.Exit\n\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1: creat();break;
case 2: insert();break;case 3: delete();break;
case 4: display();break;
case 5: exit(1);
default :printf("\n Enter a valid number\n\npress any key to continue");}
}
getch();}
8/3/2019 Ds Program Record
2/57
void insert()
{
i=1;while(i)
{
clrscr();printf("\t\t\t INSERTION OPERATION \n\n\n");
printf("\n1.Front insertion\n2.Middle insertion");
printf("\n3.End insertion\n4.Back to main menu\n");printf("5.Enter your choice:");
scanf("%d",&ch);
switch(ch)
{case 1:start();break;
case 2:middle();break;
case 3:end();break;
case 4:display();break;case 5: i=0;
break;
default :printf("\n\n Enter a valid number\n\n press any key to continue...");
}}
}
void delete(){
i=1;
while(i){
clrscr();
printf("\n press 1 for delete at the start\n 2 for delete at the middle\n" );
printf("\n 3 for delete at the end\n 4 for display\n 5 for exit\n");
printf("\n\n enter your choice :");
scanf("%d",&ch);switch(ch)
{
case 1:dum=head;head=head->link;
printf("\ndeleted item: %d",dum->item);
free(dum);break;
case 2:dmiddle();
break;
case 3:dum=head;
8/3/2019 Ds Program Record
3/57
while(dum->link!=NULL)
{
list=dum;dum=dum->link;
}
printf("\n deleted item : %d",dum->item);list->link=NULL;
free(dum);
break;case 4: display();
break;
case 5: i=0;
break;default :printf("\n\n enter a valid number\n\n");
printf("press any key to continue...");
getch();
}}
}
void creat()
{
if(head==NULL){
n=(struct list *)malloc(sizeof(struct list));
printf("\n list was created sucessfully");printf("\n enter the data to insert:");
scanf("%d",&n->item);
n->link=NULL;head=n;
}
else{
printf("List was crearted already\n");
getch();
}}
void start(){
n=(struct list*)malloc(sizeof(struct list));
printf("\nenter the data to insert:");scanf("%d",&n->item);
n->link=NULL;
n->link=head;
head=n;
8/3/2019 Ds Program Record
4/57
}
void middle(){
dum=head;
printf("\n enter the no after which you wish to insert:");scanf("%d",&i);
while(dum->item!=i&&dum!=NULL)
dum=dum->link;if(dum!=NULL)
{
n=(struct list*)malloc(sizeof(struct list));
printf("\n enter the data to insert:");scanf("%d",&n->item);
n->link=dum->link;
dum->link=n;
}else
{printf("\n no such items.");
printf("\n press any key to continue.");
}
getch();}
void dmiddle()
{dum=head;
printf("\n enter the no.which you wish to delete:");
scanf("%d",&i);while(dum->item!=i&&dum!=NULL)
{
list=dum;dum=dum->link;
}
if(dum!=NULL)
{printf("\n the deleted data:%d",dum->item);
list->link=dum->link;
free(dum);}
else
{printf("\n no such item.");
printf("\npress any key to continue...");
getch();
}
8/3/2019 Ds Program Record
5/57
}
void end()
{n=(struct list*)malloc(sizeof(struct list));
printf("enter the data");
scanf("%d",&n->item);n->link=NULL;
dum=head;
while(dum->link!=NULL)dum=dum->link;
dum->link=n;
}
void display()
{
dum=head;
clrscr();printf("\t\t\tDisplay\n\n\n");
while(dum!=NULL)
{
printf("--->|%d|",dum->item);
dum=dum->link;}
printf("--->NULL|");
printf("\n\npress any key to continue...");getch();
}
SAMPLE INPUT AND OUTPUT:
SINGLE LINKED LIST
---------------------------------------
MAIN MENU
1.Create
2.Insert3.Delete
4.Display
5.Exit
Enter your choice:1
Enter a new node:5
8/3/2019 Ds Program Record
6/57
MAIN MENU
1.Create2.Insert
3.Delete
4.Display5.Exit
Enter your choice:2
INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion3.End insertion
4.Back to main menu
Enter your choice:1
Enter the node to be inserted:3
A NODE IS INSERTED IN FRONT
INSERTION OPERTION1.Front insertion
2.MIDDLE insertion
3.End insertion4.Back to main menu
Enter your choice:2
Enter the node to be inserted:1
Enter item after which it has to be inserted:3
A NODE IS INSERTED IN MIDDLE
INSERTION OPERTION1.Front insertion
2.MIDDLE insertion
3.End insertion4.Back to main menu
Enter your choice:3
Enter the node to be inserted:4
A NODE IS INSERTED IN END
8/3/2019 Ds Program Record
7/57
INSERTION OPERTION
1.Front insertion2.MIDDLE insertion
3.End insertion
4.Back to main menu
Enter your choice:4
MAIN MENU
1.Create
2.Insert
3.Delete4.Display
5.Exit
Enter your choice:4
The Element in the list:3->1->5->4->NULL
MAIN MENU
1.Create
2.Insert3.Delete
4.Display
5.Exit
Enter your choice:3
DELETION OPERTION
1.Front deletion
2.Middle deletion3.End deletion
4.Back to main menu
Enter your choice:1
FRONT NODE IS DELETED
DELETION OPERTION
1.Front deletion
2.Middle deletion3.End deletion
4.Back to main menu
Enter your choice:2
8/3/2019 Ds Program Record
8/57
Enter the node to be deleted:5
MIDDLE NODE IS DELETED
DELETION OPERTION1.Front deletion
2.Middle deletion
3.End deletion4.Back to main menu
Enter your choice:3
END NODE IS DELETED
DELETION OPERTION
1.Front deletion2.Middle deletion
3.End deletion4.Back to main menu
Enter your choice:4
MAIN MENU
1.Create
2.Insert3.Delete
4.Display
5.Exit
Enter your choice:4
The element in the list:1->NULL
MAIN MENU
1.Create
2.Insert3.Delete
4.Display
5.Exit
Enter your choice:5
8/3/2019 Ds Program Record
9/57
Program Code:
//********DOUBLY LINKED LIST********//
#include
#includestruct node
{int data;
struct node *next;
struct node *prev;}*start,*ptr,*temp;
void create();
void insert();void finsert();
void minsert();
void einsert();
void delet();void fdelet();
void mdelet();
void edelet();void fdisplay();
void rdisplay();
void main(){
int choice;
clrscr();printf("SAMPLE INPUT AND OUTPUT:");
printf("\n\t\t\tDOUBLY LINKED LIST");
printf("\n\t\t\t---------------------------------");
while(1){
printf("\n\nMAIN MENU");
printf("\n-----------------\n");printf("1.Create\n");
printf("2.Insert\n");
printf("3.Delete\n");printf("4.Forward display\n");
printf("5.Reverse display\n");
printf("6.Exit\n");
printf("\nEnter your choice:");scanf("%d",&choice);
switch(choice)
{case 1:create();break;
case 2:insert();break;
case 3:delet();break;
8/3/2019 Ds Program Record
10/57
case 4:fdisplay();break;
case 5:rdisplay();break;
case 6:exit();break;default:printf("wrong choice");
exit();
}}
}
void create(){
int a;
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter a new node:");scanf("%d",&a);
temp->data=a;
temp->next=NULL;
temp->prev=NULL;if(start==NULL)
start=temp;else
{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
ptr->next=temp;temp->prev=ptr;
}
}void insert()
{
int choice1;while(1)
{
printf("\n\nINSERTION OPERTION");printf("1.Front insertion\n");
printf("2.MIDDLE insertion\n");
printf("3.End insertion\n");
printf("4.Back to main menu\n");printf("\nEnter your choice:");
scanf("%d",&choice1);
switch(choice1){
case 1:finsert();break;
case 2:minsert();break;case 3:einsert();break;
case 4:return;
}
}
8/3/2019 Ds Program Record
11/57
}
void finsert()
{int b;
printf("\nEnter the node to be inserted:");
scanf("%d",&b);temp=(struct node*)malloc(sizeof(struct node));
temp->data=b;
temp->next=start;ptr->prev=temp;
start=temp;
printf("\nA NODE IS INSERTED IN FRONT\n");
}void minsert()
{
int b,c;
printf("\nEnter the node to be inserted:");scanf("%d",&b);
printf("\nEnter item after which it has to be inserted:");scanf("%d",&c);
temp=(struct node*)malloc(sizeof(struct node));
temp->data=b;
temp->next=NULL;for(ptr=start;ptr->data!=c;ptr=ptr->next);
temp->prev=ptr;
temp->next=ptr->next;ptr->next->prev=temp;
ptr->next=temp;
printf("\nA NODE IS INSERTED IN MIDDLE\n");}
void einsert()
{int d;
printf("\nEnter the node to be inserted:");
scanf("%d",&d);
temp=(struct node*)malloc(sizeof(struct node));for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
temp->data=d;
temp->next=NULL;temp->prev=ptr;
ptr->next=temp;
printf("\nA NODE IS INSERTED IN END\n");}
void delet()
{
int choice2;
8/3/2019 Ds Program Record
12/57
while(1)
{
printf("\n\nDELETION OPERTION");printf("1.Front deletion\n");
printf("2.Middle deletion\n");
printf("3.End deletion\n");printf("4.Back to main menu\n");
printf("\nEnter your choice:");
scanf("%d",&choice2);switch(choice2)
{
case 1:fdelet();break;
case 2:mdelet();break;case 3:edelet();break;
case 4:return;
}
}}
void fdelet(){
if(start==NULL)
printf("\nTHE LIST IS EMPTY");
else{
ptr=start;
start=ptr->next;printf("\nFRONT NODE IS DELETED\n");
}
}void mdelet()
{
if(start==NULL)printf("\nTHE LIST IS EMPTY");
else
{
int e;printf("\nEnter the node to be deleted:");
scanf("%d",&e);
for(ptr=start;ptr->data!=e;ptr=ptr->next);ptr->next->prev=ptr->prev;
ptr->prev->next=ptr->next;
printf("\nMIDDLE NODE IS DELETED\n");}
}
void edelet()
{
8/3/2019 Ds Program Record
13/57
if(start==NULL)
printf("\nTHE LIST IS EMPTY");
else{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
ptr->prev->next=NULL;printf("\nEND NODE IS DELETED\n");
}
}void fdisplay()
{
if(start==NULL)
printf("\nTHE LIST IS EMPTY\n");else
{
printf("\nDISPLAY IN FORWARD ORDER:");
printf("NULL");for(ptr=start;ptr->next!=NULL;ptr=ptr->next)
{printf("%d",ptr->data);
}
printf("%d",ptr->data);
printf("NULL");}
}
void rdisplay(){
if(start==NULL)
printf("\nTHE LIST IS EMPTY");else
{
printf("\nDISPLAY IN REVERSE ORDER:");printf("NULL");
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
while(ptr!=start)
{printf("%d",ptr->data);
ptr=ptr->prev;
}printf("%d",ptr->data);
printf("NULL");
}}
8/3/2019 Ds Program Record
14/57
Sample Input and Output:
DOUBLY LINKED LIST
---------------------------------
MAIN MENU1.Create
2.Insert
3.Delete4.Forward display
5.Reverse display
6.Exit
Enter your choice:1
Enter a new node:3
MAIN MENU
1.Create2.Insert
3.Delete
4.Forward display5.Reverse display
6.Exit
Enter your choice:2
INSERTION OPERTION
1.Front insertion2.MIDDLE insertion
3.End insertion
4.Back to main menu
Enter your choice:1
Enter the node to be inserted:4
A NODE IS INSERTED IN FRONT
INSERTION OPERTION1.Front insertion
2.MIDDLE insertion
3.End insertion
8/3/2019 Ds Program Record
15/57
4.Back to main menu
Enter your choice:2
Enter the node to be inserted:5
Enter item after which it has to be inserted:4
A NODE IS INSERTED IN MIDDLE
INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion3.End insertion
4.Back to main menu
Enter your choice:3
Enter the node to be inserted:7
A NODE IS INSERTED IN END
INSERTION OPERTION1.Front insertion
2.MIDDLE insertion
3.End insertion4.Back to main menu
Enter your choice:4
MAIN MENU
1.Create2.Insert
3.Delete
4.Forward display
5.Reverse display6.Exit
Enter your choice:4
DISPLAY IN FORWARD ORDER:NULL4537NULL
MAIN MENU
1.Create
2.Insert
3.Delete
8/3/2019 Ds Program Record
16/57
4.Forward display
5.Reverse display
6.Exit
Enter your choice:5
DISPLAY IN REVERSE ORDER:NULL7354NULL
MAIN MENU1.Create
2.Insert
3.Delete
4.Forward display5.Reverse display
6.Exit
Enter your choice:3
DELETION OPERTION1.Front deletion
2.Middle deletion
3.End deletion
4.Back to main menu
Enter your choice:1
FRONT NODE IS DELETED
DELETION OPERTION1.Front deletion
2.Middle deletion
3.End deletion4.Back to main menu
Enter your choice:3
END NODE IS DELETED
DELETION OPERTION1.Front deletion
2.Middle deletion
3.End deletion4.Back to main menu
Enter your choice:4
8/3/2019 Ds Program Record
17/57
MAIN MENU
1.Create
2.Insert3.Delete
4.Forward display
5.Reverse display6.Exit
Enter your choice: 4
DISPLAY IN FORWARD ORDER:NULL13NULL
MAIN MENU1.Create
2.Insert
3.Delete
4.Forward display5.Reverse display
6.Exit
Enter your choice:5
DISPLAY IS REVERSE ORDER:NULL31NULL
MAIN MENU
1.Create2.Insert
3.Delete
4.Forward display5.Reverse display
6.Exit
Enter your choice:6
8/3/2019 Ds Program Record
18/57
Program Code:
//********BINARY TREE TRAVERSAL********//
# include
# include # include
typedef struct node
{int data;
struct node *left,*right;
}p;
void main(){
void insert(p **,int);
void inorder(p *);
void postorder(p *);void preorder(p *);
struct node *ptr;int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();printf("SAMPLE INPUT AND OUTPUT\n");
printf ("\n\t\tBINARY TREE TRAVERSAL");
printf ("\n\t\t----------------------------------\n");printf("\n\\nEnter the number of nodes in tree:");
scanf("%d",&will);
for(i=0;i
8/3/2019 Ds Program Record
19/57
{
if((*temp)==NULL)
{printf("\n Node created.\n");
(*temp)=malloc(sizeof(struct node));
(*temp)->left = NULL;(*temp)->right = NULL;
(*temp)->data = num;
return;}
else
{
if(num==(*temp)->data){
printf("\n\nNo Two Values are same");
return;
}if(numdata)
{printf("\nDirected To left link\n");
insert(&((*temp)->left),num);
}
else{
printf("\nDirected To right link\n");
insert(&((*temp)->right),num);}
}
return;}
void inorder(p *temp)
{if(temp!=NULL)
{
inorder(temp->left);
printf("\t%d",temp->data);inorder(temp->right);
}
elsereturn;
}
void preorder(p *temp){
if(temp!=NULL)
{
printf("\t%d",temp->data);
8/3/2019 Ds Program Record
20/57
preorder(temp->left);
preorder(temp->right);
}else
return;
}void postorder(p *temp)
{
if(temp!=NULL){
postorder(temp->left);
postorder(temp->right);
printf("\t%d",temp->data);}
else
return;
}
Sample Input and Output:
BINARY TREE TRAVERSAL-----------------------------------------------
Enter the number of nodes in tree:3
Enter the item:5
Node Created.
Enter the item:4
Directed To left link
Node Created.
Enter the item:6
Directed To right link
Node Created.
INORDER TRAVERSAL: 4 5 6
PREORDER TRAVERSAL: 5 4 6
POSTORDER TRAVERSAL: 4 6 5
8/3/2019 Ds Program Record
21/57
Program Code:
//********BINARY SEARCH TREE ********//
#include
#include#include
#define NULL 0
struct t{
struct t*left;
struct t*right;
int data;};
typedef struct t tree;
tree *root,*link,*node,*n,*temp,*parent;
int key;int lm=2,rm=80,x,l=1,l2;
int i=0,j,element,c;tree *addnode(tree*,int);
void display(tree*,int,int,int);
tree *search(tree*,int,tree**);
void delete(tree*,int);void main()
{
int choice;clrscr();
while(1)
{printf("\n \t BINARY SEARCH\n");
printf("1.Creation\n");
printf("2.Insertion\n");printf("3.Deletion\n");
printf("4.Display\n");
printf("5.Searching an element\n");
printf("6.Exit");printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice){
case 1:
printf("\n\t\t\tBinary tree creation\n");printf("Enter the data:");
scanf("%d",&element);
link=(tree*)malloc(sizeof(tree));
link->data=element;
8/3/2019 Ds Program Record
22/57
link->right=NULL;
link->left=NULL;
root=link;scanf("%d",&element);
while(element!=0)
{addnode(root,element);
scanf("%d",&element);
}break;
case 2:
printf("\nInsert the data:");
scanf("%d",&element);n=addnode(root,element);
break;
case 3:printf("\nEnter the element to be deleted:");
scanf("%d",&key);delete(root,key);
break;
case 4:display(root,lm,rm,l);
getch();
clrscr();break;
case 5:printf("\nEnter the element to be search\n");
scanf("%d",&key);
temp=search(root,key,&parent);printf("\nParent of node %d is %d",temp->data,parent->data);
break;
case 6:exit();
break;
}getch();
}
}
void display(tree *t,int lm,int rm,int l)
{
if(t!=NULL)
8/3/2019 Ds Program Record
23/57
{
x=(lm+rm)/2;
gotoxy(x,4*l);
printf("%d\n\n",t->data);
gotoxy(35, 25);display(t->left,lm,x+l2,l+1);
display(t->right,x-4,rm,l+1);
}}
tree *addnode(tree *node,int info)
{
if(node==NULL){
node=(tree*)malloc(sizeof(tree));
node->data=info;
node->left=NULL;node->right=NULL;
return node;}
else
{
if(node->data==info){
printf("\nData already exists");
return node;}
else
{if(node->data>info)
{
node->left=addnode(node->left,info);return node;
}
else
{node->right=addnode(node->right,info);
return node;
}}
}
}tree *search(tree *root,int key,tree **parent)
{
tree *temp;
temp=root;
8/3/2019 Ds Program Record
24/57
while(temp!=NULL)
{
if(temp->data==key){
printf("\nThe %d element is present \n",temp->data);
return temp;}
*parent=temp;
if(temp->data>key)temp=temp->left;
else
temp=temp->right;
}return NULL;
}
void delete(tree *root,int key)
{tree *temp,*parent,*temp_succ;
temp=search(root,key,&parent);if(temp->left!=NULL&&temp->right!=NULL)
{
parent=temp;
temp_succ=temp->right;while(temp_succ->left!=NULL)
{
parent=temp;temp_succ=temp->right;
}
temp->data=temp_succ->data;parent->left=NULL;
printf("The Element is Deleted");
return;}
if(temp->left!=NULL&&temp->right==NULL)
{
if(parent->left==temp)parent->left=temp->left;
else
parent->right=temp->left;temp=NULL;
free(temp);
printf("The Element is Deleted");return;
}
if(temp->left==NULL&&temp->right!=NULL)
{
8/3/2019 Ds Program Record
25/57
if(parent->left==temp)
parent->left=temp->right;
elseparent->right=temp->right;
temp=NULL;
free(temp);printf("The Element is Deleted");
return;
}if(temp->left==NULL&&temp->right==NULL)
{
if(parent->left==temp)
parent->left=NULL;else
parent->right=NULL;
printf("\nThe Element is Deleted");
return;}
}
Sample Input and Output:
BINARY SEARCH
1.Creation
2.Insertion3.Deletion
4.Display
5.Searching an element
6.Exit
Enter your choice:1
Binary tree creation
Enter the data:5530
20
40
600
BINARY SEARCH
1.Creation
2.Insertion
8/3/2019 Ds Program Record
26/57
3.Deletion
4.Display
5.Searching an element6.Exit
Enter your choice:4
55
30 60
20 40
BINARY SEARCH
1.Creation
2.Insertion3.Deletion
4.Display
5.Searching an element6.Exit
Enter your choice:2
Insert the data:56
BINARY SEARCH
1.Creation
2.Insertion3.Deletion
4.Display
5.Searching an element6.Exit
Enter your choice:4
8/3/2019 Ds Program Record
27/57
55
30 60
20 40 56
BINARY SEARCH
1.Creation
2.Insertion
3.Deletion4.Display
5.Searching an element
6.Exit
Enter your choice:3
Enter the element to be deleted:30
The 30 Element is presentThe Element is deleted
BINARY SEARCH
1.Creation
2.Insertion3.Deletion
4.Display
5.Searching an element6.Exit
Enter your choice:4
8/3/2019 Ds Program Record
28/57
55
20 60
40 56
BINARY SEARCH
1.Creation
2.Insertion3.Deletion
4.Display
5.Searching an element6.Exit
Enter your choice:5
Enter the element to be search
56
The 56 Element is present
Parent of node 56 is 60
8/3/2019 Ds Program Record
29/57
Program Code:
//********PRIMS ALGORITHM********//
#include#define MAX 10
#define TEMP 0
#define PERM 1#define FALSE 0
#define TRUE 1
#define infinity 9999
struct node{
int predecessor;
int dist;
int status;};
struct edge
{
int u;
int v;};
int adj[ MAX ][ MAX ];int n;
main(){
int i, j;
int path[ MAX ];int wt_tree, count;
struct edge tree[ MAX ];
create_graph();
printf( "Adjacency matrix is :\n" );
display();
count = maketree( tree, &wt_tree );
printf( "Weight of spanning tree is : %d\n", wt_tree );
printf( "Edges to be included in spanning tree are : \n" );
for ( i = 1;i
8/3/2019 Ds Program Record
30/57
{
printf( "%d->", tree[ i ].u );
printf( "%d\n", tree[ i ].v );}
}
create_graph(){
int i, max_edges, origin, destin, wt;
printf("SAMPLE INPUT AND OUTPUT:\n");printf("\n\t\t PRIMS ALGORITHM\n");
printf("\n\t\t------------------\n");
printf( "Enter number of vertices : " );
scanf( "%d", &n );max_edges = n * ( n - 1 ) / 2;
for ( i = 1;i n || destin > n || origin
8/3/2019 Ds Program Record
31/57
{
int i, j;
for ( i = 1;i
8/3/2019 Ds Program Record
32/57
}
}
}min = infinity;
for ( i = 1;i
8/3/2019 Ds Program Record
33/57
Sample Input and Output:
PRIM'S ALGORITHM--------------------------------
Enter number of vertices : 3
Enter edge 1(0 0 to quit) : 12
Enter weight for this edge : 4
Enter edge 2(0 0 to quit) : 1
3
Enter weight for this edge : 5
Enter edge 3(0 0 to quit) : 23
Enter weight for this edge : 7
Adjacency matrix is :
0 4 5
4 0 75 7 0
Weight of spanning tree is : 9
Edges to be included in spanning tree are :
1->2
1->3
8/3/2019 Ds Program Record
34/57
Program Code:
//********KRUSKALS ALGORITHM********//
#include#define MAX 20
struct edge{
int u;
int v;
int weight;struct edge *link;
}*front = NULL;
int father[ MAX ];
struct edge tree[ MAX ];int n;
int wt_tree = 0;int count = 0;
void make_tree();
void insert_tree( int i, int j, int wt );
void insert_pque( int i, int j, int wt );
struct edge *del_pque();
main()
{
int i;clrscr();
printf("SAMPLE INPUT AND OUTPUT\n");
printf("\n\t\t KRUSKAL'S ALGORITHM\n");printf("\n\t\t----------------------\n");
create_graph();
make_tree();
printf("Edges to be included in spanning tree are :\n");
for ( i = 1;i
8/3/2019 Ds Program Record
35/57
create_graph()
{
int i,wt,max_edges,origin,destin;
printf("Enter number of nodes:");
scanf("%d",&n);max_edges =n*(n-1)/2;
for (i=1;in||destin>n||originv;
8/3/2019 Ds Program Record
36/57
printf("n1=%d",node1);
printf("n2=%d",node2);
while ( node1 > 0 )
{
root_n1=node1;node1=father[node1];
}
while (node2>0)
{
root_n2=node2;
node2=father[node2];}
printf( "rootn1=%d ",root_n1 );
printf( "rootn2=%d\n",root_n2 );
if (root_n1!=root_n2){
insert_tree( tmp->u, tmp->v, tmp->weight );wt_tree = wt_tree + tmp->weight;
father[root_n2] =root_n1;
}
}}
void insert_tree(int i,int j,int wt)
{printf("This edge inserted in the spanning tree\n");
count++;
tree[count].u=i;tree[count].v=j;
tree[count].weight=wt;
}void insert_pque( int i, int j, int wt )
{
struct edge * tmp, *q;
tmp = ( struct edge * ) malloc( sizeof( struct edge ) );tmp->u = i;
tmp->v = j;
tmp->weight = wt;if (front==NULL||tmp->weightweight)
{
tmp->link=front;front=tmp;
}
else
{
8/3/2019 Ds Program Record
37/57
q=front;
while(q->link!=NULL && q->link->weight weight)q = q->link;
tmp->link = q->link;
q->link = tmp;
if (q->link==NULL)
tmp->link = NULL;
}
}
struct edge *del_pque()
{
struct edge *tmp;tmp = front;
printf("Edge processed is %d->%d %d\n",tmp->u,tmp->v,tmp->weight);front = front->link;
return tmp;
}
Sample Input and Output:
KRUSKAL'S ALGORITHM--------------------------------------
Enter number of nodes : 3
Enter edge 1(0 0 to quit): 1
3
Enter weight for this edge : 8
Enter edge 2(0 0 to quit): 23
Enter weight for this edge : 9
Enter edge 3(0 0 to quit): 1
2
Enter weight for this edge : 5
Edge processed is 1->2 5
8/3/2019 Ds Program Record
38/57
n1=1 n2=2 rootn1=1 rootn2=2
This edge inserted in the spanning tree
Edge processed is 1->3 8
n1=1 n2=3 rootn1=1 rootn2=3
This edge inserted in the spanning tree
Edges to be included in spanning tree are :
1->2
1->3
Weight of this minimum spanning tree is : 13
8/3/2019 Ds Program Record
39/57
Program Code:
//********LINEAR PROBING********//
#include
#include
int a[10];main()
{
int i,n,k,j=1,c;
clrscr();printf("SAMPLE INPUT AND OUTPUT \n");
printf("\n\t\t LINEAR PROBING \n");
printf("\n\t\t-------------------\n");
for(i=0;i
8/3/2019 Ds Program Record
40/57
printf("a[%d]=%d\n",i,a[i]);
}
}getch();
}
Sample Input and Output:
LINEAR PROBING-----------------------------
Enter the number to be inserted 4
The values in array
a[0]=-1a[1]=-1
a[2]=-1
a[3]=-1a[4]=4
a[5]=-1
a[6]=-1a[7]=-1
a[8]=-1
a[9]=-1
Enter the number to be inserted 64
The values in array
a[0]=-1a[1]=-1
a[2]=-1
a[3]=-1a[4]=4
a[5]=64
a[6]=-1
a[7]=-1a[8]=-1
a[9]=-1
Enter the number to be inserted 51
8/3/2019 Ds Program Record
41/57
The values in array
a[0]=-1a[1]=51
a[2]=-1
a[3]=-1a[4]=4
a[5]=64
a[6]=-1a[7]=-1
a[8]=-1
a[9]=-1
Enter the number to be inserted 47
The values in array
a[0]=-1a[1]=51
a[2]=-1
a[3]=-1
a[4]=4a[5]=64
a[6]=-1
a[7]=47a[8]=-1
a[9]=-1
Enter the number to be inserted 76
The values in array
a[0]=-1
a[1]=51
a[2]=-1a[3]=-1
a[4]=4
a[5]=64a[6]=76
a[7]=47
a[8]=-1a[9]=-1
8/3/2019 Ds Program Record
42/57
Enter the number to be inserted 98
The values in array
a[0]=-1
a[1]=51a[2]=-1
a[3]=-1
a[4]=4a[5]=64
a[6]=76
a[7]=47
a[8]=98a[9]=-1
Enter the number to be inserted 69
The values in array
a[0]=-1
a[1]=51
a[2]=-1
a[3]=-1a[4]=4
a[5]=64
a[6]=76a[7]=47
a[8]=98
a[9]=69
Enter the number to be inserted 72
The values in array
a[0]=-1
a[1]=51a[2]=72
a[3]=-1
a[4]=4a[5]=64
a[6]=76
a[7]=47a[8]=98
a[9]=69
Enter the number to be inserted 63
8/3/2019 Ds Program Record
43/57
The values in array
a[0]=-1
a[1]=51
a[2]=72a[3]=63
a[4]=4
a[5]=64a[6]=76
a[7]=47
a[8]=98
a[9]=69
Enter the number to be inserted 39
The values in array
a[0]=39a[1]=51
a[2]=72
a[3]=63
a[4]=4a[5]=64
a[6]=76
a[7]=47a[8]=98
a[9]=69
Enter the number to be inserted 56
The table is full
8/3/2019 Ds Program Record
44/57
Program Code:
//********CHAINING WITH REPLACEMENT********//
#include#include
#include
#define MAX 10int a[MAX][2];
void main()
{
int num,key,i;char ans;
int create(int);
void chaining(int,int),display();
clrscr();printf("SAMPLE INPUT AND OUTPUT:\n");
printf("\n\t\t CHAINING WITH REPLACEMENT\n");printf("\n\t\t--------------------------------\n");
for(i=0;i
8/3/2019 Ds Program Record
45/57
void display();
int match(int,int);
flag=0;i=0;
while(i
8/3/2019 Ds Program Record
46/57
break;
}
}}
}
else{
if(ch==-1)
{temp=a[key][0];
for(i=key+1;i
8/3/2019 Ds Program Record
47/57
if(match(a[key][0],num))
{
if(ch==-1){
for(i=0;i
8/3/2019 Ds Program Record
48/57
else
{
for(j=0;j
8/3/2019 Ds Program Record
49/57
Sample Input and Output:
CHAINING WITH REPLACEMENT
----------------------------------------------------
CHAINING WITH REPLACEMENT
Enter the number:56
The hash table is...
0 -1 -1
1 -1 -12 -1 -1
3 -1 -1
4 -1 -1
5 -1 -16 56 -1
7 -1 -1
8 -1 -19 -1 -1
Do u wish to continue?(y/n)y
Enter the number:
31
The hash table is...
0 -1 -11 31 -1
2 -1 -1
3 -1 -14 -1 -1
5 -1 -1
6 56 -17 -1 -1
8 -1 -1
9 -1 -1
Do u wish to continue?(y/n)y
Enter the number:
72
8/3/2019 Ds Program Record
50/57
The hash table is...
0 -1 -11 31 -1
2 72 -1
3 -1 -14 -1 -1
5 -1 -1
6 56 -17 -1 -1
8 -1 -1
9 -1 -1
Do u wish to continue?(y/n)y
Enter the number:
82
The hash table is...
0 -1 -1
1 31 -1
2 72 3
3 82 -14 -1 -1
5 -1 -1
6 56 -17 -1 -1
8 -1 -1
9 -1 -1
Do u wish to continue?(y/n)y
Enter the number:
83
The hash table is...
0 -1 -1
1 31 -12 72 4
3 83 -1
4 82 -15 -1 -1
6 56 -1
7 -1 -1
8 -1 -1
8/3/2019 Ds Program Record
51/57
9 -1 -1
Do u wish to continue?(y/n)y
Enter the number:
68
The hash table is...
0 -1 -1
1 31 -1
2 72 4
3 83 -14 82 -1
5 -1 -1
6 56 -1
7 -1 -18 -1 -1
9 -1 -1
Do u wish to continue?(y/n)y
Enter the number:68
The hash table is...
0 -1 -1
1 31 -12 72 4
3 83 -1
4 82 -15 -1 -1
6 56 -1
7 -1 -1
8 68 -19 -1 -1
Do u wish to continue?(y/n)y
Enter the number:
37
The hash table is...
0 -1 -1
8/3/2019 Ds Program Record
52/57
1 31 -1
2 72 4
3 83 -14 82 -1
5 -1 -1
6 56 -17 37 -1
8 68 -1
9 -1 -1
Do u wish to continue?(y/n)y
Enter the number:29
The hash table is...
0 -1 -11 31 -1
2 72 43 83 -1
4 82 -1
5 -1 -1
6 56 -17 37 -1
8 68 -1
9 29 -1
Do u wish to continue?(y/n)y
Enter the number:
19
The hash table is...
0 19 -1
1 31 -1
2 72 43 83 -1
4 82 -1
5 -1 -16 56 -1
7 37 -1
8 68 -19 29 0
Do u wish to continue?(y/n)n
8/3/2019 Ds Program Record
53/57
Program Code:
//********ALL SOURCE SHORTEST PATH USING FLOYDS ALGORITHM********//
#include
#include
#include
#include
void main()
{
int minimum(int,int);
int i,j,n,a[25][25],q[25][25],k;
char pth[25][25][25];
char *tmp,p1[25],p2[25],p3[25];
clrscr();printf("Enter the total no. of vertices:");
scanf("%d",&n);
printf("Enter the adjacency matrices:");
for(i=0;i
8/3/2019 Ds Program Record
54/57
strcat(p1,p2);
strcpy(pth[i][j],p1);
}
}
}
for(i=0;i
8/3/2019 Ds Program Record
55/57
for(i=0;i
8/3/2019 Ds Program Record
56/57
1-0 1-1 1-2 1-3 1-4
2-0 2-1 2-2 2-3 2-4
3-0 3-0-0-1 3-0-0-2 3-3 3-0-0-4
4-0 4-1 4-2 4-3 4-4
999 4 3 13 6
999 999 4 9 999
999 999 999 4 2
4 8 7 17 10
999 7 11 1 999
0-0 0-1 0-2 0-1-1-3 0-4
1-0 1-1 1-2 1-3 1-4
2-0 2-1 2-2 2-3 2-4
3-0 3-0-0-1 3-0-0-2 3-0-0-1-1-3 3-0-0-4
4-0 4-1 4-1-1-2 4-3 4-4999 4 3 7 5
999 999 4 8 6
999 999 999 4 2
4 8 7 11 9
999 7 11 1 13
0-0 0-1 0-2 0-2-2-3 0-2-2-4
1-0 1-1 1-2 1-2-2-3 1-2-2-4
2-0 2-1 2-2 2-3 2-4
3-0 3-0-0-1 3-0-0-2 3-0-0-2-2-3 3-0-0-2-2-44-0 4-1 4-1-1-2 4-3 4-1-1-2-2-4
11 4 3 7 5
12 16 4 8 6
8 12 11 4 2
4 8 7 11 9
5 7 8 1 10
0-2-2-3-3-0 0-1 0-2 0-2-2-3 0-2-2-4
1-2-2-3-3-0 1-2-2-3-3-0-0-1 1-2 1-2-2-3 1-2-2-4
2-3-3-0 2-3-3-0-0-1 2-3-3-0-0-2 2-3 2-4
3-0 3-0-0-1 3-0-0-2 3-0-0-2-2-3 3-0-0-2-2-4
4-3-3-0 4-1 4-3-3-0-0-2 4-3 4-3-3-0-0-2-2-4
0-2-2-3-3-0 0-1 0-2 0-2-2-3 0-2-2-4
8/3/2019 Ds Program Record
57/57
1-2-2-3-3-0 1-2-2-3-3-0-0-1 1-2 1-2-2-3 1-2-2-4
2-3-3-0 2-3-3-0-0-1 2-3-3-0-0-2 2-3 2-4
3-0 3-0-0-1 3-0-0-2 3-0-0-2-2-3 3-0-0-2-2-4
4-3-3-0 4-1 4-3-3-0-0-2 4-3 4-3-3-0-0-2-2-4
10 4 3 6 5
11 13 4 7 6
7 9 10 3 2
4 8 7 10 9
5 7 8 1 10
0-2-2-4-4-3-3-0 0-1 0-2 0-2-2-4-4-3 0-2-2-4
1-2-2-4-4-3-3-0 1-2-2-4-4-1 1-2 1-2-2-4-4-3 1-2-2-4
2-4-4-3-3-0 2-4-4-1 2-4-4-3-3-0-0-2 2-4-4-3 2-4
3-0 3-0-0-1 3-0-0-2 3-0-0-2-2-4-4-3 3-0-0-2-2-4
4-3-3-0 4-1 4-3-3-0-0-2 4-3 4-3-3-0-0-2-2-4
Top Related