数据结构实验之链表一:顺序建立链表
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
第一种:
#include <stdio.h>
#include <stdlib.h>
struct node//定义链表类型
{
int data;//数据域
struct node *next;//指针域
};
int main()
{
struct node *head,*p,*tail;//定义头指针、游动指针和尾指针
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;//建立只包含头结点的空链表,头指针、尾指针均指向头结点。初始化tail指向head,tail指针总是只想最后一个结点。
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)//循环建立新结点p,并插入到尾结点tail的后面
{
p=(struct node *)malloc(sizeof(struct node))