Problem Description
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
Input
第一行输入整数的个数N;
第二行依次输入每个整数。
Output
输出这组整数。
Sample Input
8
12 56 4 6 55 15 33 62
Sample Output
12 56 4 6 55 15 33 62
Hint
不得使用数组!
Source
#include <stdio.h>
#include <stdlib.h>
struct lqc//定义结构体
{
int data;//数值
struct lqc *next;//next的位置
};
int main()
{
int i,n;
struct lqc *head,*p,*tail;//定义指针
head=(struct lqc*)malloc(sizeof(struct lqc));//malloc函数,从堆内存中分配一块size大小的内存
head->next=NULL;//head先指向空白
tail=head;//tail的应用
scanf("%d",&n);
for(i=0;i<n;i++)
{
p=(struct lqc*)malloc(sizeof(struct lqc));//分配内存
scanf("%d",&p->data);//输入p的数值
p->next=NULL;//插入
tail->next=p;//插入
tail=p;//插入
}
p=head;//从head开始输出
while(p->next!=NULL)
{
printf("%d ",p->next->data);
p=p->next;
}
return 0;
}
题解中有部分细节错误,请不要直接复制,有部分符号可能无法显示!!!!