#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;
}
}
无头结点的单链表尾插法创建输不出结果
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
CSDN专家-link 2021-12-11 21:28关注#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; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录