I have this code for a linked list I am doing. Before adding the remove function it was working nicely. After I added it, the error that is later in the post is popping up. I already initialized it as you can see so I cannot think of what is causing the problem.
#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;
}
}
Here is the error
~/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
I would think it has something to do with how I initialized it but I did not find spelling mistakes/incorrect parameters. Anyone know what is the reason?
There's a C standard function called remove()
in <stdio.h>
which conflicts with your own remove
. Easiest solution is to rename your function to something like my_remove()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.