我有这个代码用于我正在做的链表。在添加删除功能之前,它运行良好。添加后,后面的错误会弹出。我已经初步确定了它,因为我无法想到造成问题的原因。
#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
我认为它与我如何初始化它有关,但我没有发现拼写错误/不正确的参数。有谁知道是什么原因?