【问题标题】:Why i am getting errors while passing array of pointer to struct node to createHash() function?为什么在将指向结构节点的指针数组传递给 createHash() 函数时出现错误?
【发布时间】:2020-06-02 06:21:02
【问题描述】:

最近我学习了散列的单独链接技术并尝试编写一个 C 程序,当我将散列数组(指向结构节点的指针数组)传递给 createHash() 函数时,该 C 程序将按垂直顺序打印二叉树出现以下错误。 在此先感谢,让我知道我实际上做错了什么。

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* next;
};
struct BTnode
{
    int data;
    struct BTnode* Lchild;
    struct BTnode* Rchild;
};
void findmin_max(struct BTnode* root,int *min,int *max,int hd)
{
    if(root==NULL)
    return;
    if(hd<*min) 
    {
        *min=hd;
    }
    else if(hd>*max) 
    {
        *max=hd;
    }
    findmin_max(root->Lchild,min,max,hd-1);
    findmin_max(root->Rchild,min,max,hd+1);
}   
struct BTnode* newNode(int data)
{
    struct BTnode* newnode=(struct BTnode*)malloc(sizeof(struct BTnode));
    newnode->data=data;
    newnode->Lchild=NULL;
    newnode->Rchild=NULL;
    return newnode;
}
struct node* insertnode(struct BTnode* root,int ln,int hd)
{
    if(root==NULL);
    return root;
        else if(hd==ln)
    {       
        struct node* nw=(struct node*)malloc(sizeof(struct node));
        nw->data=root->data;
        nw->next=NULL;
        return nw;
    }
    insertnode(root,ln,hd-1);
    insertnode(root,ln,hd+1);
}   
void createHash(struct BTnode* root,struct node* hash,int min,int max,int hd)
{       
    int key=0;
    for(int line_no=min;line_no<=max;line_no++)
    {        
         key=line_no+abs(min);
         if((hash[key])==NULL)
             (hash[key])=insertnode(root,line_no,0);
         else
         {
             struct node* temp=(hash[key]);
             while(temp->next!=NULL)
             {
                 temp=temp->next;
             }
             temp->next=insertnode(root,line_no,0);
         }
    }
}
int main()
{       
    struct BTnode* root;
    int min=0;
    int max=0;
        root = newNode(1); 
        root->Lchild = newNode(2); 
        root->Rchild = newNode(3); 
        root->Lchild->Rchild = newNode(4); 
        root->Lchild->Rchild = newNode(5); 
        root->Rchild->Lchild = newNode(6); 
        root->Rchild->Rchild = newNode(7); 
        root->Rchild->Lchild->Rchild = newNode(8); 
        root->Rchild->Rchild->Rchild = newNode(9); 
    findmin_max(root,&min,&max,0);
    int N=abs(min-max);

    struct node* hash[N];

        for(int i=0;i<N;i++)
        hash[i]=NULL;
    createHash(root,hash,min,max,0);
    return 0;
} 

目前我的程序不包含打印哈希表的打印函数,我正在编译 看看我上面的代码写对了吗。

这些是我在 Linux 终端中编译时遇到的错误。

BT_vertical.c: In function ‘insertnode’:
BT_vertical.c:40:9: warning: returning ‘struct BTnode *’ from a function with incompatible return type ‘struct node *’ [-Wincompatible-pointer-types]
  return root;
         ^~~~
BT_vertical.c:41:9: error: ‘else’ without a previous ‘if’
         else if(hd==ln)
         ^~~~
BT_vertical.c: In function ‘createHash’:
BT_vertical.c:57:18: error: invalid operands to binary == (have ‘struct node’ and ‘void *’)
    if((hash[key])==NULL)
       ~~~~~~~~~~~^~
BT_vertical.c:58:19: error: incompatible types when assigning to type ‘struct node’ from type ‘struct node *’
        (hash[key])=insertnode(root,line_no,0);
                   ^
BT_vertical.c:61:23: error: incompatible types when initializing type ‘struct node *’ using type ‘struct node’
     struct node* temp=(hash[key]);
                       ^
BT_vertical.c: In function ‘main’:
BT_vertical.c:91:18: warning: passing argument 2 of ‘createHash’ from incompatible pointer type [-Wincompatible-pointer-types]
  createHash(root,hash,min,max,0);
                  ^~~~
BT_vertical.c:51:50: note: expected ‘struct node *’ but argument is of type ‘struct node **’
 void createHash(struct BTnode* root,struct node* hash,int min,int max,int hd)
                                     ~~~~~~~~~~~~~^~~~

【问题讨论】:

  • insertnode 被声明为返回 struct node *,但你有 return root;root 被声明为 struct BTnode* root;
  • 您在if(root==NULL); 末尾有一个额外的;,这会导致else without previous if 错误。
  • @Jacob 没有丢失,if 块中只有一条语句。
  • 您可能希望获得一个带有语法高亮功能的 IDE,并且可能需要针对此类错误的某种智能感知。
  • 大部分错误都是不言自明的。请仔细阅读列表,我相信在发布问题之前,您将能够比您想象的更多。

标签: c hash


【解决方案1】:
  • struct node* insertnode(struct BTnode* root,int ln,int hd) 返回 struct node* 类型的值,但 return root; 将返回 struct BTnode* 类型的变量 - 所以您可以简单地将其更改为 return NULL;
  • 同样在insertnode内:当条件root == NULLhd == ln都不为真时,该函数的返回值完全缺失
  • 进一步您将hashstruct node* 类型的数组)传递给void createHash(struct BTnode* root,struct node* hash,int min,int max,int hd) - 为了使其编译,您需要将函数参数更改为struct node** hashstruct node* hash[]

但是你真的应该看看编译器告诉你什么,并试着理解你做错了什么——这些失败是很明显的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多