C中与'operator *'(操作数类型为'node')不匹配

时间:2015-10-01 17:58:14

标签: 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]

1 个答案:

答案 0 :(得分:1)

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

void Insert(struct node *head)

阅读

void Insert(struct node **head)

然后将最后的Display调用更改为

Display(*head)

另外,你在Display()中的循环是无限的,因为它没有做任何计数而且不能被打破。