c - 链表产生警告的函数

标签 c function data-structures linked-list singly-linked-list

在循环单链表开头插入节点的函数会产生错误:第 21 行的[警告]来自不兼容指针类型的赋值

这是什么意思以及如何解决它?

#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int info;
    struct node *next;
};
typedef struct Node node;
node *head = NULL;
node *tail = NULL;

void insertAtBeginning()
{
    int i;
    node *temp = head;
    node *NewPtr = NULL;
    NewPtr = (node *)malloc(sizeof(node));
    printf("\nEnter a value\n");
    scanf("%d", &i);
    NewPtr -> info = i;
    NewPtr -> next = NewPtr;                 //Line 21
    if(head == NULL)
    {
        head = NewPtr;
        NewPtr-> next = NewPtr;
    }
    else
    {
        while(temp -> next != head)
        {
            temp = temp->next;
        }
        NewPtr -> next = head;
        temp -> next = NewPtr;
        head = NewPtr;
    }
}

最佳答案

您混合了类型和 typedefstruct node 不存在,但您尝试在 struct Node 定义中使用它。

要解决该问题,您可以将 nodetypedef 放在 struct Node 的实际定义之前并在任何地方使用node(不是struct node,因为它不存在)。

typedef struct Node node;

struct Node
{
    int info;
    node *next; // note: node, not struct node
};

关于c - 链表产生警告的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76662920/

相关文章:

c - 2DRange 中每个工作组的 OpenCL 工作项

function - 如何在 React-Native 中从另一个函数调用一个函数?

javascript - 如何从特定时间(例如上午 9 点到下午)以分钟为单位开始计时

c# - C# 中的保序数据结构

C - 表达式必须是可修改的左值

c - 预处理器 #if a==b 不受支持或错误?

c - 为家庭作业制作 shell

javascript - 替换新函数内的arguments.callee

function - Julia -如何将kwargs从函数传递到宏

python - 用于存储键值对并快速检索最低值的键的数据结构