c - C 中与 ‘operator*’ (操作数类型为 ‘node’ )不匹配

标签 c pointers struct

我正在尝试在c中实现链表。我使用双链表将头结构节点传递给所有函数。我正在寻求帮助 How do I modify a pointer that has been passed into a function in C? 。我不知道我错在哪里。

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

void choice(struct node **);
void Insert(struct node **);
void Display(struct node **);
void Search();


struct node 
{
    int value;
    struct node *link;
};



int main(int argc, char const *argv[])
{


    struct node *head = NULL;

    choice(&head);

    return 0;
}


void choice(struct node **head)
{
    int choice;
    while(1)
    {
        printf("Enter your choice: \n");

        printf(" 1. Insert a list\n 2. Display the list\n 3. Search the list\n : ");

        scanf("%d",&choice);

        switch(choice)
        {
            case 1: Insert(head);break;
            case 2: Display(head);break;
            case 3: Search();break;
        }
    }

}

void Insert(struct node *head)
{
    int new_value;
    printf("Enter the value to be inserted: \n");
    scanf("%d",&new_value);
    if ( **head == NULL )
    {
        printf("head is NULL\n");
        **head = (struct node *)malloc(sizeof(struct node));
        (**head) -> value = new_value;
        **head -> link = NULL;

    }

    else
    {
        struct node *new_node = (struct node *)malloc(sizeof(struct node));

        new_node -> value = new_value;

        new_node -> link = NULL;

        struct node *temp = *head;

        while ( temp -> link != NULL )
        {
            temp = temp -> link;
        }

        temp -> link = new_node;
    }
    Display(head);


}

void Display(struct node *head)
{
    struct node *temp = *head;

    while ( temp -> link != NULL )
    {
        printf("The value is: %d\n", temp -> value );
    }

}

void Search()
{

}

我收到如下错误:

  /home/shahjahan/Desktop/interview/datastructure/ll.c: In function ‘void Insert(node*)’:
/home/shahjahan/Desktop/interview/datastructure/ll.c:56:7: error: no match for ‘operator*’ (operand type is ‘node’)
  if ( **head == NULL )
       ^
/home/shahjahan/Desktop/interview/datastructure/ll.c:59:3: error: no match for ‘operator*’ (operand type is ‘node’)
   **head = (struct node *)malloc(sizeof(struct node));
   ^
/home/shahjahan/Desktop/interview/datastructure/ll.c:60:4: error: no match for ‘operator*’ (operand type is ‘node’)
   (**head) -> value = new_value;
    ^
/home/shahjahan/Desktop/interview/datastructure/ll.c:61:3: error: no match for ‘operator*’ (operand type is ‘node’)
   **head -> link = NULL;
   ^
/home/shahjahan/Desktop/interview/datastructure/ll.c:73:24: error: cannot convert ‘node’ to ‘node*’ in initialization
   struct node *temp = *head;
                        ^
/home/shahjahan/Desktop/interview/datastructure/ll.c:82:14: error: cannot convert ‘node*’ to ‘node**’ for argument ‘1’ to ‘void Display(node**)’
  Display(head);
              ^
/home/shahjahan/Desktop/interview/datastructure/ll.c: In function ‘void Display(node*)’:
/home/shahjahan/Desktop/interview/datastructure/ll.c:89:23: error: cannot convert ‘node’ to ‘node*’ in initialization
  struct node *temp = *head;
                       ^
[Finished in 0.0s with exit code 1]

最佳答案

看起来您缺少插入函数定义中的第二个 *。您将其传递为双间接并将其用作双间接,但将其定义为单间接。我会改变路线

void Insert(struct node *head)

阅读

void Insert(struct node **head)

然后将末尾的 Display 调用更改为

Display(*head)

此外, Display() 中的循环是无限的,因为它不执行任何计数并且无法中断。

关于c - C 中与 ‘operator*’ (操作数类型为 ‘node’ )不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32893943/

相关文章:

c - strtoull ("-1", NULL, 0) 和其他负值的惊人行为

c - 使用指针数组的内存映射外围寄存器

c - c中的右对齐数字?

pointers - 在 C 中将指针复制到指针 'contents'

当数据复制/扫描/读取到未初始化的指针时崩溃或 "segmentation fault"

C 编程结构体初始化

json - 类型 '[String:Any]' 在 Swift 中没有下标成员

c++ - 使用 C++ 方式对结构和数组进行别名处理

c - Jpeglib 代码给出乱码输出,甚至是捆绑的示例代码?

c - malloc() : memory corruption