题目描述
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
输入
第一行输入整数的个数N;
第二行依次输入每个整数。
第二行依次输入每个整数。
输出
输出这组整数。
示例输入
# include <stdio.h>
#include <stdlib.h>
struct node
{
int data; //数据域
struct node *next;
};
int main()
{
int n, a;
scanf("%d",&n);
struct node * head, *tail, *p;
int i;
head = (struct node *)malloc(sizeof(struct node)); //头指针
head->next = NULL; //头指针指向NULL
tail=head; //移动指针从头开始移动
for(i=0;i<n;i++)
{
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = NULL;
tail->next = p;
tail=p;
}
tail=head->next; //使移动指针从头开始指
while(tail!=NULL)
{
printf("%d",tail->data);
if(tail->next!=NULL) //判断是否为最后一个
printf(" ");
tail=tail->next; //使移动指针后移
}
printf("\n");
return 0;
}