邻接列表的图形声明

Graph Declaration for Adjacency List

本文关键字:图形 声明 列表      更新时间:2023-10-16

我正在得到

错误:从类型"struct listnode*"分配给类型"struct listnode"时类型不兼容

错误:'->' have('struct listnode')的类型参数无效

我在这里的指针做错了什么?提前致谢

     #include <stdio.h>
     #include <stdlib.h>
     struct listnode{
     int vertex;
    struct listnode *next;
     };
 struct Graph{
  int V;
  int E;
  struct listnode *adj;
 };

 void print(struct Graph *G);
  struct Graph* initategraph(){
    int i,source,dest;
    struct listnode *temp;
    struct Graph *G=(struct Graph*)malloc(sizeof(struct Graph));
    if(!G)
       {printf("Memory not available");
         return;
       }
    printf("Enter the vertex no. and edges n");
    scanf("%d %d",&G->V,&G->E);
    G->adj = malloc(G->V*sizeof(struct listnode));
    for(i=0;i<G->V;i++)
    {
        G->adj[i] =(struct listnode *)malloc(sizeof(struct listnode));
        G->adj[i]->vertex = i;
        G->adj[i]->next=G->adj[i];
    }
    for(i=0;i<G->E;i++){
        scanf("%d %d",&source,&dest);
      temp= (struct listnode *)malloc(sizeof(struct listnode));
        temp->vertex=dest;
        temp->next=G->adj[source];
        G->adj[source]->next=temp;
        temp =(struct listnode *)malloc(sizeof(struct listnode));
        temp->vertex=source;
        temp->next=G->adj[dest];
        G->adj[dest]->next=temp;

      }
    return G;
 } 
  void print(struct Graph *G){
   int i;
   struct listnode *temp;
    for(i=0;i<G->V;i++){
    temp=G->adj[i];
    while(temp)
    {
        printf("%d --> ",temp->vertex);
        temp=temp->next;
     }
     printf("nn");
   }
 }
 void main()
   {
   struct Graph *G;
   G=initategraph();
   print(G);

   }
首先,

G->adj = malloc(G->V*sizeof(struct listnode));已经分配了一个动态内存,其中包含 struct listnode 类型的 G-V 元素。因此,请从循环内部删除此语义不正确的语句:G->adj[i] =(struct listnode *)malloc(sizeof(struct listnode));

G->adj[i]的类型struct listnode不是struct listnode*。将G->adj[i]->vertex更改为G->adj[i].vertex

G->adj[i].next的类型是struct listnode*G->adj[i]的类型是struct listnode。将G->adj[i]->next=G->adj[i];更改为G->adj[i].next = &( G->adj[i] );G->adj[i]是第 i 个元素本身,而不是指向第 i 个元素的指针。

注意 &( G->adj[i] )G->adj + i 类似。 adj[i]类似于*(adj + i)&(adj[i])类似于adj + i

注意G->adj[i].next = &( G->adj[i] );后的成员next引用自己的struct listnode。我不知道这是否是你想做的。

像这样调整你的代码:

struct Graph* initategraph(){
    int i,source,dest;
    struct listnode *temp;
    struct Graph *G=(struct Graph*)malloc(sizeof(struct Graph));
    if( G =0 NULL )
    {
        printf("Memory not available");
        return NULL;
    }
    printf("Enter the vertex no. and edges n");
    scanf("%d %d",&G->V,&G->E);
    G->adj = malloc(G->V*sizeof(struct listnode));
    for( i=0; i<G->V; i++ )
    {
        G->adj[i].vertex = i;
        G->adj[i].next = &( G->adj[i] );
    }
    for( i=0; i<G->E; i++ ){
        scanf("%d %d",&source,&dest);
        temp= (struct listnode *)malloc(sizeof(struct listnode));
        temp->vertex=dest;
        temp->next = &( G->adj[source] );
        G->adj[source].next = temp;
        temp =(struct listnode *)malloc(sizeof(struct listnode));
        temp->vertex=source;
        temp->next = &( G->adj[dest] );
        G->adj[dest].next = temp;
    }
    return G;
} 

注意:我不知道算法是否在做你想要它做的事情。我只修复了编译错误。