#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int data;
struct ListNode *next;
};
void readlist()
{
struct ListNode *head,*p,*q;
int i;
head=(struct ListNode*)malloc(sizeof(struct ListNode));
head->next=NULL;
p=head;
while(1)
{
scanf("%d",&i);
if(i==-1)
break;
q=(struct ListNode*)malloc(sizeof(struct ListNode));
q->next=NULL;
q->data=i;
q=p;
p=p->next;
}
p->next=NULL;
}
int main ()
{
struct ListNode *head,*p;
readlist();
p=head;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
}
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int data;
struct ListNode *next;
};
struct ListNode * readlist()
{
struct ListNode *head=NULL,*p,*q;
int i;
while(1)
{
scanf("%d",&i);
if(i==-1)
break;
q=(struct ListNode*)malloc(sizeof(struct ListNode));
q->next=NULL;
q->data=i;
if(head == NULL)
p = head = q;
else
{
p->next = q;
p = p->next;
}
}
p->next=NULL;
return head;
}
int main ()
{
struct ListNode *head,*p;
head = readlist();
p=head;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}
你readlist()函数应该返回head啊
你题目的解答代码如下:
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int data;
struct ListNode *next;
};
struct ListNode *readlist()
{
struct ListNode *head, *p, *q;
int i;
head = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next = NULL;
p = head;
while (1)
{
scanf("%d", &i);
if (i == -1)
break;
q = (struct ListNode *)malloc(sizeof(struct ListNode));
q->next = NULL;
q->data = i;
p->next = q;
p = q;
}
p->next = NULL;
return head;
}
int main()
{
struct ListNode *head, *p;
head = readlist();
p = head->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
}
如有帮助,望采纳!谢谢!