#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
char info;
int status;
struct node *next;
struct adjlist *adj;
}*start,*temp,*temp1,*q,*svar;
char queue[100];
int front=-1;
int rear=-1;
struct adjlist
{
char dest;
int status;
struct adjlist *link;
}*edge1,*p,*r;
struct node *findnode(struct node *,char);
void input(char);
void inputedge(char,char);
void dfsinitial(struct node *);
void dfssearch(struct node *);
void bfsinitial(struct node *);
void bfssearch(struct node *);
void queueinsert(char);
char queuedelete();
void backs();
void main()
{
int ch,choice=1;
char n,n1,n2;
clrscr();
do
{
printf("1. Input \n");
printf("2. input edge\n");
printf("3. bfs search\n");
printf("Enter your choice\n");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1: printf("Enter the node\n");
scanf("%c",&n);
input(n);
break;
case 2: printf("Enter the nodes between whom you want an edge\n");
scanf("%c\t%c",&n1,&n2);
inputedge(n1,n2);
break;
case 3: printf("The bfs search is as follows\n");
bfsinitial(start);
backs();
break;
default: printf("Wrong choice\n");
}
printf("\nDo you want to enter more??\n");
scanf("%d",&choice);
}while(choice==1);
getch();
}
void backs()
{
printf("\b");
}
void input(char item) //TO ADD NODES
{
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("Memory full\n");
}
if(start==NULL)
{
ptr->info=item;
ptr->next=NULL;
ptr->adj=NULL;
start=ptr;
}
else
{
ptr->info=item;
ptr->next=start;
ptr->adj=NULL;
start=ptr;
}
ptr->status=1;
}
void inputedge(char item1,char item2) // TO ADD AN EDGE
{
temp=findnode(start,item1);
temp1=findnode(start,item2);
if(temp==NULL || temp1==NULL)
{
printf("Node not present\n");
}
edge1=(struct adjlist *)malloc(sizeof(struct adjlist));
edge1->dest=temp1->info;
if(temp->adj==NULL)
{
edge1->link=NULL;
temp->adj=edge1;
}
else
{
edge1->link=temp->adj;
temp->adj=edge1;
}
edge1->status=1;
}
struct node *findnode(struct node *graph,char item) // TO FIND A NODE
{
while(graph!=NULL && graph->info!=item)
{
graph=graph->next;
}
if(item==graph->info)
{
return graph;
}
return 0;
}
void bfsinitial(struct node *start)
{
char a;
q=start;
queueinsert(q->info);
q->status=2;
a=queuedelete();
printf("%c",a);
bfssearch(q);
}
void bfssearch(struct node *yum)
{
char b,d;
r=yum->adj;
if(r->status==1)
{
while(r!=NULL && (r->status==1))
{
d=r->dest;
queueinsert(d);
r->status=2;
r=r->link;
}
b=queuedelete();
printf("%c",b);
svar=findnode(start,b);
bfssearch(svar);
}
}
void queueinsert(char s)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=s;
}
else
{
rear++;
queue[rear]=s;
}
}
char queuedelete()
{
char rtn;
rtn=queue[front];
if(front==-1 && rear==-1)
{
printf("The queue is empty\n");
}
else if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front++;
}
return rtn;
}
No comments:
Post a Comment