ширина первая поисковая проблема - PullRequest
0 голосов
/ 10 мая 2019

Я создал программу, которая должна прочитать количество вершин, которые должны быть в графе, и у меня возникла проблема с созданием связей между вершинами с использованием списка ссылок.я получаю его для создания вершин, и он создает связи между некоторыми узлами, но по какой-то причине он падает, когда я пытаюсь ввести определенную вершину в качестве ссылки.

, например, если я даю число вершин в виде 4 ивведите входные данные как 1 2 3 4, затем рядом с вершиной для связи я введите 1, а вершины для связи с -1 -1 3 происходит сбой после ввода 3. Почему?когда я ввожу 2 в качестве вершины, я могу связать ее с любыми вершинами.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct BFS_node
{
    int data,vis;
    struct BFS_node *linknodes;



};
typedef struct BFS_node node;
node *head,*N=NULL;
    int num;

void create_node(node **Node)
{

    if(*Node==NULL)
   {
       *Node=(node*)malloc(sizeof(node));
head=*Node;
 }
 else
    *Node=*Node+1;

 printf("%d enter the value \n",*Node);
 scanf("%d",&(*Node)->data);
 (*Node)->linknodes=(node *)malloc(num*sizeof(node));
 printf(" %d \n",(*Node)->linknodes);


}

node * search_node(int num,node *head2)
 {

    while(head2)
    {

        if(head2->data==num)
            return head2;
            head2++;
    }
 }
void linking()
{
node *Dnode,**Lnode;
int num,i=0;
char Snum[10];
    printf("enter the number you want to link ");
scanf("%d",&num);
Dnode=search_node(num,head);
printf("%d",Dnode);
while(getchar() != '\n' && getchar()!=EOF);
Lnode=Dnode->linknodes;

printf("enter the linked numbers");
while(fgets(Snum,sizeof(Snum),stdin))
{

if(sscanf(Snum,"%d",&num)!=1)
break;

*Lnode=search_node(num,head);
printf("%d %d",Lnode,*Lnode);
Lnode++;
}

}
BFStraversal()
{
    int num,i=0;
    node *queue[10],*link;
    printf("enter the starting number");
    scanf("%d",&num);

    queue[i]=search_node(num,head);


    link=queue[i]->linknodes;
       printf("%d",link->data);
queue[i]->vis=1;
while(queue[i]!=NULL)
{

    int j=1;
  link=queue[i]->linknodes;
   printf("%d",queue[i]->linknodes->data);
  while(link->data !=NULL)
  {
if(link->vis!=1)
     {
      queue[i+j]=link;
      link->vis=1;
      j++;
      }
      link++;
  }
  printf("%d",queue[i]->data);
  i++;
}
}
int main()
{

    printf("enter the number of vertices \n ");
    scanf("%d",&num);

    for(int i=0;i<num;i++)
        create_node(&N);

    for(int i=0;i<num;i++)
        linking();

        BFStraversal();
    return 0;
}

1 Ответ

0 голосов
/ 11 мая 2019

Вот полный рабочий код. Чувствует себя таким облегчением, что это начинает раздражать.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct BFS_node
{
    int data,vis;
    struct BFS_node *linknodes;



};
typedef struct BFS_node node;


node *head,*N=NULL;
    int num,i=0,count=-1;
    int queue[20];


void create_node(node **Node)
{

    if(*Node==NULL)
   {
       *Node=(node*)malloc(num*sizeof(node));
head=*Node;
 }
 else
    *Node=*Node+1;

 printf(" enter the value \n");
 scanf("%d",&(*Node)->data);
 (*Node)->vis=1;
 (*Node)->linknodes=(node *)malloc(num*sizeof(node));
 printf("address- %d link- %d \n",*Node,(*Node)->linknodes);


}

node * search_node(int num,node *head2)
 {


    while(head2)
    {
        if(head2->data==num)
            return head2;
            head2++;
    }
 }
void linking()
{

node *Dnode,**Lnode;
int num2,i=0;
char Snum[10];
    printf("enter the number you want to link ");
scanf("%d",&num2);
Dnode=search_node(num2,head);

while(getchar() != '\n' && getchar()!=EOF);
Lnode=Dnode->linknodes;
printf("enter the linked numbers\n");
while(fgets(Snum,sizeof(Snum),stdin))
{
 node *head2=head;
if(sscanf(Snum,"%d",&num2)!=1)
break;

*Lnode=search_node(num2,head);
printf(" linknode-%d link-%d\n ",Lnode,*Lnode);
Lnode++;
}
*Lnode=0;
}
BFStraversal(int vertex)
{
    node *node,*head2=head,**links;

    node=search_node(vertex,head);
    links=node->linknodes;
    node->vis=0;
    printf(" %d ",vertex);
    while(*links!=0)
    {
        if((*links)->vis)
        {
       queue[i]=(*links)->data;
       (*links)->vis=0;
        i++;
        }

        links++;
    }


    if(++count<i)
        BFStraversal(queue[count]);

}
int main()
{

    printf("enter the number of vertices \n ");
    scanf("%d",&num);

    for(int i=0;i<num;i++)
        create_node(&N);



    for(int i=0;i<num;i++)
          linking();

printf("enter the source vertex");
int source;
scanf("%d",&source);
        BFStraversal(source);
    return 0;
}

    enter code here
...