SLIP
SLIP
Implement a Binary search tree (BST) library (btree.h) with operations – create,
insert, inorder. Write a menu driven program that performs the above operations.
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
}bnode;
bnode *create();
bnode *create()
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return(root);
if(T!=NULL)
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
{
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
T->left=insert(T->left,x);
return(T);
return(T);
void main()
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Inorder");
printf("\n\t4.Exit)");
scanf("%d",&ch);
switch(ch)
{
case 1: root=create();break;
scanf("%d",&x);
root=insert(root,x);
break;
case 3: inorder(root);
break;
case 4: exit(0);
}while(ch!=4);
}
1.2
Write a C program that accepts the vertices and edges of a graph and store it
graph.
#include<stdio.h>
#define MAX 10
int adj[MAX][MAX];
int n;
int main()
int graph_type;
scanf("%d", &n);
if(graph_type==1)
max_edges = n*(n-1)/2;
else
max_edges = n*(n-1);
break;
else
printf("\nInvalid vertex!\n");
i--;
adj[origin][destin] = 1;
if(graph_type == 1)
adj[destin][origin] = 1;
printf("%4d", adj[i][j]);
printf("\n");
printf("\nIndegree, ");
if(adj[j][i] == 1)
ind++;
}
printf("\nv%d %5d \n",i+1,ind);
}
3.1
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
h[index]=key;
break;
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
void search()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
break;
if(i == TABLE_SIZE)
void display()
int i;
main()
int opt,i;
while(1)
scanf("%d",&opt);
switch(opt)
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);
3.2
C program to implement graph traversal method using depth first search.
#include<stdio.h>
#include<stdlib.h>
int w;
visited[v]=1;
printf("v%d",v+1);
for(w=0;w<n;w++)
if((m[v][w]==1) &&(visited[w]==0))
recdfs(m,n,w);
void main()
int m[5][5]={{0,0,1,1,0},{0,0,1,0,1},{0,1,0,0,0},{0,0,0,0,1},{0,0,0,0,0}};
recdfs(m,5,0);
4.1
#include<stdio.h>
#include<stdlib.h>
void recdfs(int m[5][5],int n ,int v)
int w;
visited[v]=1;
printf("v%d",v+1);
for(w=0;w<n;w++)
if((m[v][w]==1) &&(visited[w]==0))
recdfs(m,n,w);
void main()
int m[5][5]={{0,0,1,1,0},{0,0,1,0,1},{0,1,0,0,0},{0,0,0,0,1},{0,0,0,0,0}};
recdfs(m,5,0);
4.2
. Write a C program that accepts the vertices and edges of a graph and stores
#include<stdio.h>
#include<stdlib.h>
{
int i,j;
char ans;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
m[i][i]=0;
if(i!=j)
scanf("%d",&m[i][j]);
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%5d",m[i][j]);
printf("\n");
void main()
{
int m[10][10],n;
scanf("%d",&n);
create(m,n);
display(m,n);
}
5.1
#include <stdio.h>
#include <stdlib.h>
#include "btree.h"
if (!newNode) {
return NULL;
newNode->data = data;
return newNode;
return createNode(data);
} else {
return root;
if (root != NULL) {
preorder(root->left);
preorder(root->right);
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
#include <stdio.h>
#include <stdlib.h>
#include "btree.h"
int displayMenu() {
int choice;
printf("4. Exit\n");
scanf("%d", &choice);
return choice;
int main() {
while (1) {
choice = displayMenu();
switch (choice) {
case 1:
scanf("%d", &data);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
postorder(root);
printf("\n");
break;
case 4:
printf("Exiting...\n");
exit(0);
break;
default:
return 0;
}
5.2
. Write a C program that accepts the vertices and edges of a graph and store it
as an adjacency list. Implement function to traverse the graph using Breadth First
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
int vertex;
}NODE;
NODE *head[10];
typedef struct
int data[MAXSIZE];
}QUEUE;
void createlist(int n)
int i, j, x;
{
head[i]=NULL;
printf("is there any edge between %d and %d (1/0): ", i+1, j+1);
scanf("%d", &x);
if(x==1)
newnode=(NODE*)malloc(sizeof(NODE));
newnode->vertex=j+1;
newnode->next=NULL;
if(head[i]==NULL)
head[i]=temp=newnode;
else
temp->next=newnode;
temp=newnode;
pq->data[++pq->rear]=num;
}
int removeq(QUEUE *pq)
return pq->data[++pq->front];
pq->front=pq->rear=-1;
return (pq->rear==pq->front);
int v;
int visited[20]={0}, x;
NODE *temp;
QUEUE pq;
initq(&pq);
v=0;
visited[v]=1;
isempty(&pq))
v=removeq(&pq);
printf(" v%d ", v+1);
temp=list[v];
while(temp)
x=temp->vertex-1;
if(visited[x]==0)
addq(&pq, x);
visited[x]=1;
temp=temp->next;
void displaylist(int n)
NODE *temp;
temp=head[i];
while(temp)
printf("NULL");
printf("\n");
void main()
int n;
scanf("%d", &n);
createlist(n);
displaylist(n);
bfs(head, n);
6.1
create, insert, preorder. Write a menu driven program that performs the above
operations
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
}bnode;
bnode *create();
bnode *create()
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return(root);
if(T!=NULL)
{
printf("%d\t",T->data);
preorder(T->left);
preorder(T->right);
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
{
T->left=insert(T->left,x);
return(T);
return(T);
void main()
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Exit)");
scanf("%d",&ch);
switch(ch)
case 1: root=create();break;
scanf("%d",&x);
root=insert(root,x);
break;
case 3: preorder(root);
break;
case 4: exit(0);
}while(ch!=4);
}
6.2
Write a C program that accepts the vertices and edges of a graph. Create
adjacency list
#include<stdio.h>
#include<stdlib.h>
int vertex;
} NODE;
NODE *head[10];
void createlist(int n)
int i, j, x;
head[i]=NULL;
printf("is there any edge between %d and %d (1/0): ", i+1, j+1);
scanf("%d", &x);
if (x==1)
newnode=(NODE*)malloc(sizeof(NODE));
newnode->vertex=j+1;
newnode->next=NULL;
if (head[i]==NULL)
head[i]=temp=newnode;
else
temp->next=newnode;
temp=newnode;
void displaylist(int n)
NODE *temp;
temp=head[i];
while(temp)
temp=temp->next;
}
printf("NULL");
printf("\n");
void main()
int n;
scanf("%d", &n);
createlist(n);
displaylist(n);
7.1
create, insert, preorder. Write a menu driven program that performs the above
operations
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
}bnode;
bnode *insert(bnode *,int);
bnode *create();
bnode *create()
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return(root);
if(T!=NULL)
printf("%d\t",T->data);
preorder(T->left);
preorder(T->right);
}
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
T->left=insert(T->left,x);
return(T);
return(T);
}
void main()
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Exit)");
scanf("%d",&ch);
switch(ch)
case 1: root=create();break;
scanf("%d",&x);
root=insert(root,x);
break;
case 3: preorder(root);
break;
case 4: exit(0);
}while(ch!=4);
}
7..2
Write a C program that accepts the vertices and edges of a graph and store it
include <stdio.h>
#include <stdlib.h>
/* ADJACENCY MATRIX */
int source,V,E,time,visited[20],G[20][20];
void DFS(int i)
int j;
visited[i]=1;
printf(" %d->",i+1);
for(j=0;j<V;j++)
if(G[i][j]==1&&visited[j]==0)
DFS(j);
int main()
int i,j,v1,v2;
printf("\t\t\tGraphs\n");
scanf("%d",&E);
printf("Enter the no of vertices:");
scanf("%d",&V);
for(i=0;i<V;i++)
for(j=0;j<V;j++)
G[i][j]=0;
/* creating edges :P */
for(i=0;i<E;i++)
scanf("%d%d",&v1,&v2);
G[v1-1][v2-1]=1;
for(i=0;i<V;i++)
for(j=0;j<V;j++)
printf(" %d ",G[i][j]);
printf("\n");
scanf("%d",&source);
DFS(source-1);
return 0;
}
8.1
Write a C program that accepts the vertices and edges of a graph and store it
include <stdio.h>
#include <stdlib.h>
/* ADJACENCY MATRIX */
int source,V,E,time,visited[20],G[20][20];
void DFS(int i)
int j;
visited[i]=1;
printf(" %d->",i+1);
for(j=0;j<V;j++)
if(G[i][j]==1&&visited[j]==0)
DFS(j);
int main()
int i,j,v1,v2;
printf("\t\t\tGraphs\n");
scanf("%d",&E);
printf("Enter the no of vertices:");
scanf("%d",&V);
for(i=0;i<V;i++)
for(j=0;j<V;j++)
G[i][j]=0;
/* creating edges :P */
for(i=0;i<E;i++)
scanf("%d%d",&v1,&v2);
G[v1-1][v2-1]=1;
for(i=0;i<V;i++)
for(j=0;j<V;j++)
printf(" %d ",G[i][j]);
printf("\n");
scanf("%d",&source);
DFS(source-1);
return 0;
}
8.2
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
h[index]=key;
break;
if(i == TABLE_SIZE)
}
void search()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
break;
if(i == TABLE_SIZE)
void display()
int i;
}
main()
int opt,i;
while(1)
scanf("%d",&opt);
switch(opt)
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);
9..1
#include<stdio.h>
#include<stdlib.h>
int w;
visited[v]=1;
printf("v%d",v+1);
for(w=0;w<n;w++)
if((m[v][w]==1) &&(visited[w]==0))
recdfs(m,n,w);
void main()
int m[5][5]={{0,0,1,1,0},{0,0,1,0,1},{0,1,0,0,0},{0,0,0,0,1},{0,0,0,0,0}};
recdfs(m,5,0);
}
9.2
C program to implement BST to perform following operations on BST- a) Create b) Counting leaf
nodes.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int info;
struct node*left,*right;
} NODE;
if(root==NULL)
root=(NODE *)malloc(sizeof(NODE));
root->left=root->right=NULL;
root->info=item;
return root;
else
if(item<root->info)
root->left=createbst(root->left, item);
else
if(item>root->info)
root->right=createbst(root->right,item);
else
return root;
}
if(root==NULL)
return 0;
else
if((root->left==NULL)&&(root->right==NULL))
return 1;
else
return (countleaf(root->left)+countleaf(root->right));
int main()
int i,n,choice,item,key;
NODE *temp,*root=NULL;
while(1)
printf("\n1.Create bst");
printf("\n3.Exit");
scanf("%d",&choice);
switch (choice)
{
case 1:
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&item);
root=createbst(root,item);
break;
case 2:
break;
case 3:
exit(0);
default:
}
10..1
#include<stdio.h>
#include<stdlib.h>
int w;
visited[v]=1;
printf("v%d",v+1);
for(w=0;w<n;w++)
if((m[v][w]==1) &&(visited[w]==0))
recdfs(m,n,w);
void main()
int m[5][5]={{0,0,1,1,0},{0,0,1,0,1},{0,1,0,0,0},{0,0,0,0,1},{0,0,0,0,0}};
recdfs(m,5,0);
}
10.2
. Write a C program that accepts the vertices and edges of a graph and store it
graph.
#include<stdio.h>
#define MAX 10
int adj[MAX][MAX];
int n;
int main()
int graph_type;
scanf("%d", &graph_type);
scanf("%d", &n);
if(graph_type==1)
max_edges = n*(n-1)/2;
else
max_edges = n*(n-1);
break;
else
printf("\nInvalid vertex!\n");
i--;
adj[origin][destin] = 1;
if(graph_type == 1)
adj[destin][origin] = 1;
printf("%4d", adj[i][j]);
}
printf("\n");
printf("\nIndegree, ");
if(adj[j][i] == 1)
ind++;
}
11.1
. Write a C program that accepts the vertices and edges of a graph and store it
graph.
#include<stdio.h>
#define MAX 10
int adj[MAX][MAX];
int n;
int main()
int graph_type;
scanf("%d", &graph_type);
scanf("%d", &n);
if(graph_type==1)
max_edges = n*(n-1)/2;
else
max_edges = n*(n-1);
break;
else
{
printf("\nInvalid vertex!\n");
i--;
adj[origin][destin] = 1;
if(graph_type == 1)
adj[destin][origin] = 1;
printf("%4d", adj[i][j]);
printf("\n");
printf("\nIndegree, ");
if(adj[j][i] == 1)
ind++;
}
}
11.2
Implement a Binary search tree (BST) library (btree.h) with operations – create,
insert, inorder. Write a menu driven program that performs the above operations.
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
}bnode;
bnode *create();
bnode *create()
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
scanf("%d",&x);
root=insert(root,x);
return(root);
if(T!=NULL)
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
T->left=insert(T->left,x);
return(T);
return(T);
void main()
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Inorder");
printf("\n\t4.Exit)");
printf("\n enter your choice:-->");
scanf("%d",&ch);
switch(ch)
case 1: root=create();break;
scanf("%d",&x);
root=insert(root,x);
break;
case 3: inorder(root);
break;
case 4: exit(0);
}while(ch!=4);
}
12.1
Write a C program that accepts the vertices and edges of a graph and store it
include <stdio.h>
#include <stdlib.h>
/* ADJACENCY MATRIX */
int source,V,E,time,visited[20],G[20][20];
void DFS(int i)
int j;
visited[i]=1;
printf(" %d->",i+1);
for(j=0;j<V;j++)
if(G[i][j]==1&&visited[j]==0)
DFS(j);
int main()
int i,j,v1,v2;
printf("\t\t\tGraphs\n");
scanf("%d",&E);
scanf("%d",&V);
for(i=0;i<V;i++)
for(j=0;j<V;j++)
G[i][j]=0;
/* creating edges :P */
for(i=0;i<E;i++)
G[v1-1][v2-1]=1;
for(i=0;i<V;i++)
for(j=0;j<V;j++)
printf(" %d ",G[i][j]);
printf("\n");
scanf("%d",&source);
DFS(source-1);
return 0;
12.2
create, insert, preorder. Write a menu driven program that performs the above
operations
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
bnode *create();
bnode *create()
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return(root);
if(T!=NULL)
printf("%d\t",T->data);
preorder(T->left);
preorder(T->right);
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
T->left=insert(T->left,x);
return(T);
}
return(T);
void main()
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Exit)");
scanf("%d",&ch);
switch(ch)
case 1: root=create();break;
scanf("%d",&x);
root=insert(root,x);
break;
case 3: preorder(root);
break;
case 4: exit(0);
}
}while(ch!=4);
13.2
Write a C program that accepts the vertices and edges of a graph and stores
#include<stdio.h>
#include<stdlib.h>
int i,j;
char ans;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
m[i][i]=0;
if(i!=j)
scanf("%d",&m[i][j]);
int i,j;
printf("\n \t Adjacency matrix is : \n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%5d",m[i][j]);
printf("\n");
void main()
int m[10][10],n;
scanf("%d",&n);
create(m,n);
display(m,n);
}
14.1
Write a C program that accepts the vertices and edges of a graph and stores
#include<stdio.h>
#include<stdlib.h>
int i,j;
char ans;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
m[i][i]=0;
if(i!=j)
scanf("%d",&m[i][j]);
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%5d",m[i][j]);
printf("\n");
void main()
int m[10][10],n;
create(m,n);
display(m,n);
}
14.2
Write a program which uses binary search tree library and counts the total
nodes and total leaf nodes in the tree. int count Leaf(T) – returns the total
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
if(root==NULL)
root->LC=root->RC=NULL;
root->data=item;
return root;
else
{
if(item<root->data)
root->LC=create(root->LC,item);
else if(item>root->data)
root->RC=create(root->RC,item);
else
printf("Invalid item");
return root;
if(root==NULL)
return 0;
else
return(1+totalnodes(root->LC)+totalnodes(root->RC));
int count=0;
if(newnode != NULL)
leafnodes(newnode->LC);
count++;
}
leafnodes(newnode->RC);
return count;
void main()
int choice,i,n,item;
printf("1.Create\n");
printf("2.totalnodes\n");
printf("3.countLeaf\n");
printf("4.Exit\n");
while(1)
scanf("%d",&choice);
switch(choice)
case 1: root=NULL;
scanf("%d",&n);
for(i=1;i<=n;i++)
root=create(root,item);
break;
totalnodes(root);
break;
leafnodes(root);
break;
case 4: exit(0);
}
15.1
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
h[index]=key;
break;
if(i == TABLE_SIZE)
void search()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
if(i == TABLE_SIZE)
void display()
int i;
main()
int opt,i;
while(1)
scanf("%d",&opt);
switch(opt)
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);
15.2
Write a program which uses binary search tree library and counts the total
nodes and total leaf nodes in the tree. int count Leaf(T) – returns the total
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
if(root==NULL)
{
root=(struct node *)malloc(sizeof(struct node));
root->LC=root->RC=NULL;
root->data=item;
return root;
else
if(item<root->data)
root->LC=create(root->LC,item);
else if(item>root->data)
root->RC=create(root->RC,item);
else
printf("Invalid item");
return root;
if(root==NULL)
return 0;
else
return(1+totalnodes(root->LC)+totalnodes(root->RC));
int count=0;
if(newnode != NULL)
leafnodes(newnode->LC);
count++;
leafnodes(newnode->RC);
return count;
void main()
int choice,i,n,item;
printf("1.Create\n");
printf("2.totalnodes\n");
printf("3.countLeaf\n");
printf("4.Exit\n");
while(1)
scanf("%d",&choice);
switch(choice)
{
case 1: root=NULL;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&item);
root=create(root,item);
break;
totalnodes(root);
break;
leafnodes(root);
break;
case 4: exit(0);
}
16.1
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
typedef struct
int data[MAXSIZE];
int front,rear;
}QUEUE;
pq->front=pq->rear=-1;
pq->data[++pq->rear]=n;
return pq->data[++pq->front];
return(pq->front==pq->rear);
int i,j;
printf("\n*PRESS 1 FOR YES(edge present) & 0 FOR NO(edge
not present)*\n");
for(i=0;i<n;i++)
// list[i]=NULL;
for(j=0;j<n;j++)
if(i!=j)
scanf("%d",&m[i][j]);
int v=0;
int visited[20]={0};
QUEUE q;
initq(&q);
visited[v]=1;
addq(&q,v);
while(!isempty(&q))
{
v=removeq(&q);
printf("v%d\t",v+1);
for(int w=0;w<n;w++)
if((a[v][w]==1)&&(visited[w]==0))
addq(&q,w);
visited[w]=1;
int main()
int a[10][10],n;
scanf("%d",&n);
createlist(a,n);
bfs(a,n);
16.2
create, insert, preorder. Write a menu driven program that performs the above
operations
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
}bnode;
bnode *create();
bnode *create()
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return(root);
}
if(T!=NULL)
printf("%d\t",T->data);
preorder(T->left);
preorder(T->right);
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
T->left=insert(T->left,x);
return(T);
return(T);
void main()
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Exit)");
scanf("%d",&ch);
switch(ch)
case 1: root=create();break;
root=insert(root,x);
break;
case 3: preorder(root);
break;
case 4: exit(0);
}while(ch!=4);
18.1
Write a program which uses binary search tree library and counts the total
nodes and total leaf nodes in the tree. int count Leaf(T) – returns the total
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
if(root==NULL)
root->data=item;
return root;
else
if(item<root->data)
root->LC=create(root->LC,item);
else if(item>root->data)
root->RC=create(root->RC,item);
else
printf("Invalid item");
return root;
if(root==NULL)
return 0;
else
return(1+totalnodes(root->LC)+totalnodes(root->RC));
int count=0;
{
if(newnode != NULL)
leafnodes(newnode->LC);
count++;
leafnodes(newnode->RC);
return count;
void main()
int choice,i,n,item;
printf("1.Create\n");
printf("2.totalnodes\n");
printf("3.countLeaf\n");
printf("4.Exit\n");
while(1)
scanf("%d",&choice);
switch(choice)
{
case 1: root=NULL;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&item);
root=create(root,item);
break;
totalnodes(root);
break;
leafnodes(root);
break;
case 4: exit(0);
}
18.2
Write a C program that accepts the vertices and edges of a graph and store it
graph.
#include<stdio.h>
#define MAX 10
int adj[MAX][MAX];
int n;
int main()
int graph_type;
scanf("%d", &graph_type);
scanf("%d", &n);
if(graph_type==1)
max_edges = n*(n-1)/2;
else
max_edges = n*(n-1);
{
break;
else
printf("\nInvalid vertex!\n");
i--;
adj[origin][destin] = 1;
if(graph_type == 1)
adj[destin][origin] = 1;
printf("%4d", adj[i][j]);
printf("\n");
}
printf("\nIndegree, ");
if(adj[j][i] == 1)
ind++;
19.1
. Write a C program that accepts the vertices and edges of a graph. Create
adjacency list
#include<stdio.h>
#include<stdlib.h>
int vertex;
} NODE;
NODE *head[10];
void createlist(int n)
{
int i, j, x;
head[i]=NULL;
printf("is there any edge between %d and %d (1/0): ", i+1, j+1);
scanf("%d", &x);
if (x==1)
newnode=(NODE*)malloc(sizeof(NODE));
newnode->vertex=j+1;
newnode->next=NULL;
if (head[i]==NULL)
head[i]=temp=newnode;
else
temp->next=newnode;
temp=newnode;
void displaylist(int n)
NODE *temp;
temp=head[i];
while(temp)
temp=temp->next;
printf("NULL");
printf("\n");
void main()
int n;
scanf("%d", &n);
createlist(n);
displaylist(n);
}
19.2
. Implement a Binary search tree (BST) library (btree.h) with operations –
create, insert, preorder. Write a menu driven program that performs the above
operations
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
}bnode;
bnode *create();
bnode *create()
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return(root);
if(T!=NULL)
printf("%d\t",T->data);
preorder(T->left);
preorder(T->right);
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
{
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
T->left=insert(T->left,x);
return(T);
return(T);
void main()
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Exit)");
scanf("%d",&ch);
switch(ch)
{
case 1: root=create();break;
scanf("%d",&x);
root=insert(root,x);
break;
case 3: preorder(root);
break;
case 4: exit(0);
}while(ch!=4);
}
20.1
Implement a Binary search tree (BST) library (btree.h) with operations – create,
insert, inorder. Write a menu driven program that performs the above operations.
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
}bnode;
bnode *create();
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return(root);
if(T!=NULL)
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
T->left=insert(T->left,x);
return(T);
return(T);
void main()
{
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Inorder");
printf("\n\t4.Exit)");
scanf("%d",&ch);
switch(ch)
case 1: root=create();break;
scanf("%d",&x);
root=insert(root,x);
break;
case 3: inorder(root);
break;
case 4: exit(0);
}while(ch!=4);
20.2
Write a C program that accepts the vertices and edges of a graph and store it
as an adjacency matrix. Implement functions to print indegree of all vertices of
graph.
#include<stdio.h>
#define MAX 10
int adj[MAX][MAX];
int n;
int main()
int graph_type;
scanf("%d", &graph_type);
scanf("%d", &n);
if(graph_type==1)
max_edges = n*(n-1)/2;
else
max_edges = n*(n-1);
{
printf("\nEnter edge[%d](-1 -1 to quit): ", i);
break;
else
printf("\nInvalid vertex!\n");
i--;
adj[origin][destin] = 1;
if(graph_type == 1)
adj[destin][origin] = 1;
{
printf("%4d", adj[i][j]);
printf("\n");
printf("\nIndegree, ");
if(adj[j][i] == 1)
ind++;
}
22.1
#include <stdio.h>
#include <stdlib.h>
#include "btree.h"
if (!newNode) {
return NULL;
}
newNode->data = data;
return newNode;
if (root == NULL) {
return createNode(data);
} else {
return root;
if (root != NULL) {
preorder(root->left);
preorder(root->right);
}
if (root != NULL) {
postorder(root->left);
postorder(root->right);
#include <stdio.h>
#include <stdlib.h>
#include "btree.h"
int displayMenu() {
int choice;
printf("4. Exit\n");
scanf("%d", &choice);
return choice;
int main() {
while (1) {
choice = displayMenu();
switch (choice) {
case 1:
scanf("%d", &data);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
printf("\n");
break;
case 4:
printf("Exiting...\n");
exit(0);
break;
default:
return 0;
}
22.2
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
h[index]=key;
break;
if(i == TABLE_SIZE)
void search()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
break;
if(i == TABLE_SIZE)
void display()
int i;
main()
int opt,i;
while(1)
scanf("%d",&opt);
switch(opt)
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);
}
23..1
Write a C program that accepts the vertices and edges of a graph. Create
adjacency list
#include<stdio.h>
#include<stdlib.h>
int vertex;
} NODE;
NODE *head[10];
void createlist(int n)
int i, j, x;
head[i]=NULL;
printf("is there any edge between %d and %d (1/0): ", i+1, j+1);
scanf("%d", &x);
if (x==1)
newnode=(NODE*)malloc(sizeof(NODE));
newnode->vertex=j+1;
newnode->next=NULL;
if (head[i]==NULL)
head[i]=temp=newnode;
else
temp->next=newnode;
temp=newnode;
void displaylist(int n)
NODE *temp;
temp=head[i];
while(temp)
temp=temp->next;
}
printf("NULL");
printf("\n");
void main()
int n;
scanf("%d", &n);
createlist(n);
displaylist(n);
23.2
C program to implement BST to perform following operations on BST- a) Create b) Counting leaf
nodes.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int info;
struct node*left,*right;
} NODE;
{
if(root==NULL)
root=(NODE *)malloc(sizeof(NODE));
root->left=root->right=NULL;
root->info=item;
return root;
else
if(item<root->info)
root->left=createbst(root->left, item);
else
if(item>root->info)
root->right=createbst(root->right,item);
else
return root;
if(root==NULL)
return 0;
else
if((root->left==NULL)&&(root->right==NULL))
return 1;
else
return (countleaf(root->left)+countleaf(root->right));
int main()
int i,n,choice,item,key;
NODE *temp,*root=NULL;
while(1)
printf("\n1.Create bst");
printf("\n3.Exit");
scanf("%d",&choice);
switch (choice)
case 1:
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENter the node info");
scanf("%d",&item);
root=createbst(root,item);
break;
case 2:
break;
case 3:
exit(0);
default:
}
24.1
#include <stdio.h>
#include <stdlib.h>
#include "btree.h"
if (!newNode) {
return NULL;
newNode->data = data;
return newNode;
if (root == NULL) {
return createNode(data);
} else {
root->right = insertNode(root->right, data);
return root;
if (root != NULL) {
preorder(root->left);
preorder(root->right);
if (root != NULL) {
postorder(root->left);
postorder(root->right);
#include <stdio.h>
#include <stdlib.h>
#include "btree.h"
int displayMenu() {
int choice;
printf("4. Exit\n");
scanf("%d", &choice);
return choice;
int main() {
while (1) {
choice = displayMenu();
switch (choice) {
case 1:
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
postorder(root);
printf("\n");
break;
case 4:
printf("Exiting...\n");
exit(0);
break;
default:
}
return 0;
}
24.2
Write a C program that accepts the vertices and edges of a graph and store it
#include<stdio.h>
#include<stdlib.h>
struct q
int data[20];
int front,rear;
}q1;
void add(int n)
q1.rear++;
q1.data[q1.rear]=n;
int del()
q1.front++;
return q1.data[q1.front];
void initq()
q1.front=q1.rear=-1;
}
int emptyq()
return (q1.rear==q1.front);
int i,j;
char ans;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
m[i][i]=0;
if(i!=j)
scanf("%d",&m[i][j]);
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%5d",m[i][j]);
printf("\n");
int i,j,v,w;
int visited[20];
initq();
for(i=0;i<n;i++)
visited[i]=0;
v=0;
visited[v]=1;
add(v);
while(! emptyq())
v=del();
printf("\n");
for(w=0;w<n;w++)
if((m[v][w]==1) &&(visited[w]==0))
{
add(w);
visited[w]=1;
void main()
int m[10][10],n;
scanf("%d",&n);
create(m,n);
display(m,n);
bfs(m,n);
25.1
create, insert, preorder. Write a menu driven program that performs the above
operations
#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
int data;
bnode *create();
bnode *create()
int n,x,i;
bnode *root;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return(root);
if(T!=NULL)
printf("%d\t",T->data);
preorder(T->left);
preorder(T->right);
bnode *temp;
if(T==NULL)
temp=(bnode*)malloc(sizeof(bnode));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
if(x>T->data)
else
T->right=insert(T->right,x);
return(T);
if(x<T->data)
T->left=insert(T->left,x);
return(T);
}
return(T);
void main()
bnode *root=NULL,*p;
int x,ch;
do
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Exit)");
scanf("%d",&ch);
switch(ch)
case 1: root=create();break;
scanf("%d",&x);
root=insert(root,x);
break;
case 3: preorder(root);
break;
case 4: exit(0);
}
}while(ch!=4);
}
25.2
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
h[index]=key;
break;
if(i == TABLE_SIZE)
}
void search()
int key,index,i,flag=0,hkey;
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
break;
if(i == TABLE_SIZE)
void display()
int i;
}
main()
int opt,i;
while(1)
scanf("%d",&opt);
switch(opt)
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);