gpt4 book ai didi

c - 在已排序的链表中插入一个元素

转载 作者:行者123 更新时间:2023-11-30 18:45:29 25 4
gpt4 key购买 nike

有人可以帮我调试这个程序吗,我是初学者,找不到解决方案,我收到错误:

incompatible types when returning type 'node * {aka struct linked_list *}' but 'node {aka struct linked_list}' was expected

该程序在已排序的链表中插入一个元素

struct linked_list
{
int number;
struct linked_list *next;
};

typedef struct linked_list node;

int main()
{
int n;
node *head;
void create(node *p);
node insert(node *p, int n);
void print(node *p);
head= (node *)malloc(sizeof(node));
create(head);
printf("\n");
printf("original list : ");
print(head);
printf("\n\n");
printf("input the number to be inserted: ");
scanf("%d",&n);
head=insert(head, n);
printf("\n");
printf("new list after insertion : ");
print(head);*/
return 0;
}

void create(node *list)
{
printf("input a number : \n");
printf("type -999 at the end");
scanf("%d", &list->number);
if(list->number == 999)
list->next= NULL;
else
list->next=(node*)malloc(sizeof(node));
create(list->next);
return;
}

void print(node *list)
{
while(list->next != 0)
{
printf("%d",list->number);
list= list->next;

}
return;
}

node insert(node *head,int x)
{
node *p1,*p2,*p;
p1=NULL;
p2=head;
for(;p2->number<x;p2=p2->next)
{
p1=p2;
if(p2->next==NULL)
p2=p2->next;
break;
}
p=(node*)malloc(sizeof(node));
p->number=x;
p->next=p2;
if(p1==NULL)
{
head=p;
}
else
{
p1->next=p;
return head;
}

node insert(node *head,int x) 无法正常工作,它应该返回什么?

最佳答案

您正在从 insert() 返回指针,因此正确的签名是

node* insert(node *head,int x)

关于c - 在已排序的链表中插入一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54659283/

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