7-1 sdut-C语言实验-顺序建立链表
分数 20
全屏浏览
切换布局
作者 马新娟
单位 山东理工大学
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
输入格式:
第一行输入整数的个数N;
第二行依次输入每个整数。
输出格式:
输出这组整数。
输入样例:
8
12 56 4 6 55 15 33 62
输出样例:
12 56 4 6 55 15 33 62
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node*next;
};
int main()
{
int n,i;
scanf("%d",&n);
struct node *p,*tail,*head;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
scanf("%d",&p->data);
tail->next=p;
tail=p;
}
p=head->next;
while(p!=NULL)
{
if(p->next==NULL)
{
printf("%d\n",p->data);
}
else{
printf("%d ",p->data);
}
p=p->next;
}
}