gpt4 book ai didi

函数 "remove"的冲突类型

转载 作者:太空狗 更新时间:2023-10-29 16:05:52 24 4
gpt4 key购买 nike

我有我正在做的链接列表的代码。在添加删除功能之前,它运行良好。添加后,弹出帖子后面的错误。如您所见,我已经对其进行了初始化,所以我想不出是什么导致了问题。

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

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

typedef struct node Node;
typedef Node* NodePtr;

void push(int value, NodePtr *start);
void add(int value, NodePtr *start);
void pop(NodePtr* start);
void remove(NodePtr* start); //line 16
void traverse(NodePtr* start);
int main(void)
{
NodePtr first = NULL;
push(2, &first);
add(3, &first);
printf("%d, %p\n", first -> value, first -> next);
printf("%d, %p\n", first -> next -> value, first -> next -> next);

push(4, &first);
add(5, &first);
printf("%d, %p\n", first -> value, first -> next);
printf("%d, %p\n", first -> next -> value, first -> next -> next);

pop(&first);
pop(&first);
printf("%d, %p\n", first -> value, first -> next);
printf("%d, %p\n", first -> next -> value, first -> next -> next);

remove(&first);
add(6, &first);
printf("%d, %p\n", first -> value, first -> next);
printf("%d, %p\n", first -> next -> value, first -> next -> next);
return 0;
}

//push node to beginning
void push(int value, NodePtr *start)
{
NodePtr newStart = malloc(sizeof(Node));
if(newStart == NULL)
{
return;
}
newStart -> value = value;
newStart -> next = *start;
*start = newStart;
}
//add node to end
void add(int value, NodePtr *start)
{
NodePtr newNode = malloc(sizeof(Node));

if(newNode == NULL)
{
return;
}

newNode -> value = value;
newNode -> next = NULL;

NodePtr current = *start;

while((current)->next != NULL)
{
current = current -> next;
}

current -> next = newNode;
}
//pop beginning node
void pop(NodePtr* start)
{
NodePtr trash = *start;
(*start) = (*start)->next;
free(trash);
}

//remove last node
void remove(NodePtr* start) //line 87
{
NodePtr current = *start;

while((current)->next != NULL)
{
if(current->next == NULL)
{
break;
}
current = current -> next;
}
NodePtr trash = current -> next;
current -> next = current;
free(trash);
}

//goes through list
void traverse(NodePtr* start)
{
NodePtr current = *start;
while((current -> next) != NULL)
{
current = current -> next;
}
}

这里是错误

~/C-Programs> make zelda
cc -g -Wall -Wextra -lm -std=c99 zelda.c -o zelda
zelda.c:16: error: conflicting types for ‘remove’
/usr/include/stdio.h:177: note: previous declaration of ‘remove’ was here
zelda.c:87: error: conflicting types for ‘remove’
/usr/include/stdio.h:177: note: previous declaration of ‘remove’ was here
make: *** [zelda] Error 1

我认为这与我如何初始化它有关,但我没有发现拼写错误/不正确的参数。有谁知道是什么原因吗?

最佳答案

有一个 C 标准函数叫做 remove() <stdio.h>这与你自己的冲突 remove .最简单的解决方案是将您的函数重命名为类似 my_remove() 的名称.

关于函数 "remove"的冲突类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40290674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com