要求:
输入若干个整数编号,输入编号为0时结束,用单向链表组织这些编号信息后,再按顺序输出。
正确运行参考示例:
输入1:
1005
1002
1003
0
输出1:
The single linked list is:
1005
1002
1003
运行代码如下:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int num;
struct Node *next;
};
int main(void)
{
struct Node *head,*tail, *p;
int num;
int size = sizeof(struct Node);
head=tail=NULL;
printf("请输入编号:\n");
scanf("%d", &num);
/*建立单向链表*/
while(num != 0)
{
p = (struct Node *) malloc(size);
if(head==NULL)
head=p;
else
tail->next=p;
p->num = num;
tail=p;
scanf("%d", &num);
}
p->next=NULL;
/*输出单向链表*/
printf("The single linked list is:\n");
for(p=head; p!=NULL; p=p->next)
printf("%d\n", p->num);
return 0;
}