深度优先搜索和C++中的breafth优先搜索实现

Depth first search & breafth first search implementation in C++

本文关键字:搜索 实现 breafth C++ 中的 深度优先搜索      更新时间:2023-10-16

我是一个初级编码器,正在编码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;

还可以尝试将变量名更改为更有意义的名称,以便其他人可以调试。还要在代码中添加一些注释。你写这篇文章的初衷是为了消除编译错误。这是固定的。现在我建议您重构代码并开始自己调试。