gpt4 book ai didi

创建并显示单链表

转载 作者:行者123 更新时间:2023-11-30 19:41:05 25 4
gpt4 key购买 nike

我的代码:

    #include <stdio.h>

node * create(int);
void disp(node *,int);

typedef struct node
{
int data;
struct node *next;
};
node * create(int);
void disp(node *,int);
typedef struct node *head , *p , *c;
int i,n;
int main()
{
printf("\n Enter the number of nodes:");
scanf("%d",&n);
c=create(n);
disp(head,n);
return 0;
}

node * create(int n)
{
head = (node *)malloc(sizeof(node));

scanf("%d", &head->data);
head->next = NULL;
p=head;
for(i=1;i<n;i++)
{
p=(node*)malloc(sizeof(node));
scanf("%d",&p->data);
p=p->next;
p->next=NULL;
}
return head;
}

void disp(node *head , int n)
{
p=head;
while(p!=NULL)
{
for(i=0;i<n;i++)
{
printf("%d",p->data);
p=p->next;
}
}
}

输出:

User@Sujata:~/Desktop]$ gcc ll.c 
ll.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:4: error: expected ‘)’ before ‘*’ token
ll.c:10: warning: useless storage class specifier in empty declaration
ll.c:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:12: error: expected ‘)’ before ‘*’ token
ll.c: In function ‘main’:
ll.c:19: error: expected identifier or ‘(’ before ‘=’ token
ll.c:20: error: expected expression before ‘head’
ll.c: At top level:
ll.c:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:41: error: expected ‘)’ before ‘*’ token

得到这个输出。也尝试多次使用 typedef 关键字。但不起作用。提前致谢!

最佳答案

.....你的代码很乱......

您需要添加

 typedef struct node node;

在函数的前向声明之前,以便编译器知道类型node。另外,从

中删除 typedef
  typedef struct node *head , *p , *c;

关于创建并显示单链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33932770/

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