数据结构实验之链表一:顺序建立链表
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
Input
第一行输入整数的个数N;
第二行依次输入每个整数。
第二行依次输入每个整数。
Output
输出这组整数。
Example Input
8 12 56 4 6 55 15 33 62
Example Output
12 56 4 6 55 15 33 62
Code realization
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct LNode
{
elemtype data;
struct LNode *next;
}LNode,*LinkList;
int main()
{
int n, i;
LinkList p, head, tail;
scanf("%d",&n);
head = (LNode*)malloc(sizeof(LNode));
head->next = NULL;
tail = head;
for(i=0;i<n;i++)
{
p = (LNode*)malloc(sizeof(LNode));
scanf("%d",&p->data);
tail->next = p;
tail = p;
tail->next = NULL;
}
p = head->next;
for(i=0;i<n;i++)
{
if(i==n-1)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
return 0;
}

本文介绍了一种使用C语言实现顺序输入整数并构建单链表的方法,并通过遍历链表输出所有节点的数据。该程序首先读取整数的数量,然后逐一读取整数并将其插入到链表中,最后按顺序打印出链表中的所有元素。
1440

被折叠的 条评论
为什么被折叠?



