深度优先搜索和C++中的breafth优先搜索实现
Depth first search & breafth first search implementation in C++
我是一个初级编码器,正在编码dfs-bfs,bt它最终显示错误,我已经为它分配了一个节点数组,我使用了指针对指针,但是当我为每个数组成员的元素分配值时,它显示错误。根据我的说法,这是唯一的错误,请帮忙!!
struct node
{
int dat;
int vstd;
struct node *nxt;
};
struct node **graph=(struct node**)malloc(5*sizeof(struct node*));
void initial()
{
for(int i=0;i<5;i++)
struct node *(graph[i])=(struct node*)malloc(sizeof(struct node));
}
struct node *head=NULL;
void visited(struct node *strt)
{
struct node *temp;
for(int i=0;i<5;i++)
{
temp=graph[i];
while(temp!=NULL)
{
if(temp->dat==strt->dat)
temp->vstd=1;
temp=temp->nxt;
}
}
}
struct node* find(struct node *gelem)
{
for(int i=0;i<5;i++)
{
if(graph[i]->dat==gelem->dat)
return(graph[i]);
}
return NULL;
}
void dfs(struct node *gelem)
{
struct node *temp;
visited(gelem);
cout<<gelem->dat<<endl;
temp=find(gelem);
while(temp!=NULL)
{
if(temp->vstd!=1)
{
dfs(temp);
}
temp=temp->nxt;
}
}
void dpthfrst()
{
struct node *temp;
for(int v=0;v<5;v++)
{
temp=graph[v];
while(temp!=NULL)
{
temp->vstd=0;
temp=temp->nxt;
}
}
for(int v=0;v<5;v++)
{
temp=graph[v];
while(temp!=NULL)
{
if(temp->vstd!=1)
dfs(temp);
temp=temp->nxt;
}
}
}
void bfs(struct node *grtemp)
{
struct node *head,*temp,*tmp2,*tmp3;
ins(head,grtemp);
do
{
temp=del(head);
visited(temp);
cout<<temp->dat<<endl;
tmp2=temp->nxt;
while(tmp2!=NULL)
{
tmp3=find(tmp2);
ins(head,tmp3);
}
}while(!isemp(head));
}
void brthfrst()
{
struct node *temp;
for(int v=0;v<5;v++)
{
temp=graph[v];
while(temp!=NULL)
{
temp->vstd=0;
temp=temp->nxt;
}
}
for(int v=0;v<5;v++)
{
temp=graph[v];
while(temp!=NULL)
{
if(temp->vstd!=1)
bfs(temp);
temp=temp->nxt;
}
}
}
void insrtnode()
{
struct node *temp;
struct node *nnode=(struct node*)malloc(sizeof(struct node));
struct node *tnode=(struct node*)malloc(sizeof(struct node));
int s,d,f1=0,f2=0;
cout<<"Insert the connection"<<endl;
cin>>s;
cin>>d;
nnode->dat=s;
nnode->nxt=NULL;
nnode->vstd=0;
tnode->dat=d;
tnode->nxt=NULL;
tnode->vstd=0;
for(int i=0;i<5;i++)
{
if(graph[i]->dat==s)
f1=1;
if(graph[i]->dat==d)
f2=1;
}
if(f1==0 && f2==0 && cnt<3)
{
for(int i=0;i<5;i++)
{
if(graph[i]==NULL)
{
graph[i]=nnode;
graph[i]->nxt=tnode;
graph[i++]=tnode;
cnt+=2;
break;
}
}
}
else if((f1==1 && f2==0 && cnt<4)||(f1==0 && f2 ==1 && cnt<4 ))
{
if(f1==1)
{
for(int i=0;i<5;i++)
{
if(graph[i]->dat==s)
{
temp=graph[i];
while(temp->nxt!=NULL)
temp=temp->nxt;
temp->nxt=tnode;
}
if(graph[i]==NULL)
{
graph[i]=tnode;
cnt++;
return;
}
}
}
if(f2==1)
{
for(int i=0;i<5;i++)
{
if(graph[i]==NULL)
{
graph[i]=nnode;
graph[i]->nxt=tnode;
cnt++;
return;
}
}
}
}
else
{
cout<<"Not a valid insertion";
return;
}
}
int main()
{
initial();
int ch;
char ans;
do
{
cout<<"1.INSERTn 2.DFS n 3.BFSn";
cin>>ch;
switch(ch)
{
case 1: insrtnode();
break;
case 2:dpthfrst();
break;
case 3:brthfrst();
break;
default :cout<<"Invalid n";
}
cout<<"Do you want to continue?(y/n)";
cin>>ans;
}
while(ans=='y'||ans=='Y');
return 0;
}
void initial()
{
for(int i=0; i<5; i++)
struct node *(graph[i])=(struct node*)malloc(sizeof(struct node));
}
在struct node *(graph[i])
中,当您声明它时,您正在访问它的i
位置。应该像这个
graph[i]=(struct node*)malloc(sizeof(struct node));
内部节点功能
if(f1==0 && f2==0 && cnt<3)
{
for(int i=0;i<5;i++)
{
if(graph[i]==NULL)
{
graph[i]=nnode;
graph[i]->nxt=tnode;
graph[i++]=tnode;
cnt+=2;
break;
}
}
}
线路
graph[i++]=tnode;
应该是
graph[++i]=tnode;
还可以尝试将变量名更改为更有意义的名称,以便其他人可以调试。还要在代码中添加一些注释。你写这篇文章的初衷是为了消除编译错误。这是固定的。现在我建议您重构代码并开始自己调试。
相关文章:
- std::unordered_map 搜索算法是如何实现的?
- 我可以在没有堆栈的情况下在二叉搜索树中实现迭代器吗?
- 二叉搜索树 - 实现"search"函数
- 二叉搜索树的 C++ 实现中的EXC_BAD_ACCESS错误
- 在给定的项目列表上实现搜索?
- 使用openmp实现并行广度优先搜索
- 尝试实现二叉搜索算法,似乎无法使其工作
- C++二叉搜索树实现不会添加每个元素
- 在排序文本文件中实现二叉搜索?
- 在C++中实现二叉搜索树,搜索不起作用.尝试打印节点的元素会导致输出崩溃
- C 中的快速范围搜索实现
- 双向链表插入方法实现 - 正在搜索哪个节点
- C++二叉搜索树实现,动态数组或结构/类
- 如何在C++中实现函数上的二叉搜索?
- 使用深度第一次搜索的无向图中的图形调查无法实现的节点
- 二进制搜索树的数组实现
- 二叉搜索树实现C++运行时错误
- 深度使用两个链接列表的邻接列表的首次搜索C 实现
- 深度优先搜索和C++中的breafth优先搜索实现
- 递归二叉搜索实现崩溃.为什么