我只是尝试使用 GCC 编译器在 C 中实现链表,但我收到了这些警告。我得到的大多数警告都是“来自不兼容的指针类型的赋值”:
linklist.c: In function 'insertatbegin':
linklist.c:20:8: warning: assignment from incompatible pointer type [enabled by default]
linklist.c:24:18: warning: assignment from incompatible pointer type [enabled by default]
linklist.c:25:8: warning: assignment from incompatible pointer type [enabled by default]
linklist.c: In function 'display':
linklist.c:37:7: warning: assignment from incompatible pointer type [enabled by default]
运行代码后,我什么也没得到。
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
typedef struct
{
int data;
struct node *next;
}node;
void insertatbegin(node **head,int item)
{
node *nextnode=(node *)malloc(sizeof(node));
if(nextnode!=NULL)
{
nextnode->data=item;
nextnode->next=head; //warning line 24
head=nextnode; // warning line 25
}
else
printf("memory not allocated\n");
}
void display(node * head)
{
node *temp=head;
while(temp!=NULL)
{
printf(" %d ",temp->data);
temp=temp->next; //warning line 37
}
}
void main()
{
node *head=NULL;
insertatbegin(&head,20);
insertatbegin(&head,30);
insertatbegin(&head,40);
display(head);
getch();
}
这似乎是正确的,但我没有得到任何输出。