A - 数据结构实验之链表一:顺序建立链表
Description
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
Input
第一行输入整数的个数N;
第二行依次输入每个整数。
Output
输出这组整数。
Sample
Input
8 12 56 4 6 55 15 33 62
Output
12 56 4 6 55 15 33 62
Hint
不得使用数组!
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};\\ 定义既有数据,也有地址的结点
int main()
{
struct node *head,*q,*p;\\头结点,尾结点,移动结点
head=(struct node *)malloc(sizeof(struct node));\\定义命名空间
head->next=NULL;\\地址
\\建立头结点
q=head;\\设置尾结点
int n;
scanf("%d",&n);
while(n--)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;\\建立游动结点
q->next=p;\\挂游动结点
q=p;\\尾结点后移
}\\
q=head->next;
while(q!=NULL)
{
if(q->next!=NULL)
printf("%d ",q->data);
else
printf("%d",q->data);
q=q->next;
} \\输出链表,用条件判断终止以及空格
return 0;
}