gpt4 book ai didi

c - 编译时链表创建问题以这种方式解决

转载 作者:行者123 更新时间:2023-11-30 21:36:25 26 4
gpt4 key购买 nike

帮助解决//PLEASE TELL ME PROBLEM IN THIS LINE

包括

#include <stdlib.h>   

// A linked list node
typedef struct Node
{
int info;
struct Node *link;
}node;

//insert in a linked list

void insert(node* head, int k)
{
node* temp = (node*) malloc(sizeof(node));
if(temp==NULL)
{
printf("malloc was unsuccessfull");
exit(0);
}
else
{
temp->info=k;
temp->link=NULL;
if(head==NULL)
head = temp;
else
{
temp->link=head;
head=temp;
}
}

// This function prints contents of linked list starting from head
void print(node* a)
{
while (a != NULL)
{
printf(" %d ", a->link); //PLEASE TELL ME PROBLEM IN THIS LINE
a = a->link;
}
}

}
void main()
{
/* Start with the empty list */
node* head = NULL;

insert(head, 7);
insert(head, 9);

printf("\n Created Linked list is: ");
print(head); //PLEASE TELL ME PROBLEM IN THIS LINE

}

错误消息

prog.c: In function 'print':

prog.c:40:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'struct Node *' [-Wformat=]
printf(" %d ", a->link);
^
prog.c: In function 'main':

prog.c:55:3: warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]
print(head);
^


/tmp/cc1HDBBm.o: In function `main':
3192773816853040ef42d0aa4269a062.c:(.text+0xeb): undefined reference to `print'
collect2: error: ld returned 1 exit status

最佳答案

  1. printf("%d ", a->link); 的类型为 %d ,参数为 node .这里你想要的是打印整数

    printf(" %d ", a->info);
  2. 您未对齐 insert 函数的括号。在原始代码中,print 的定义位于insert 的定义内部。您需要正确关闭 insert 函数的括号,并且调用 print(head); 将收到正确的定义。并正常工作。

  3. 您正在使用 void main() 不带任何命令行参数的 main 的正确格式是 int main(void)

关于c - 编译时链表创建问题以这种方式解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53296710/

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