2116数据结构实验之链表一:顺序建立链表
2016-07-28 16:28
369 查看
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
第二行依次输入每个整数。
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
int main()
{
struct node *head,*p,*q,*tail;
int i,n;
scanf("%d",&n);
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
while(n--) /*第一个插入的结点为第一个结点,然后依次往后插入结点 */
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p; /*p结点已达到链表结尾*/
}
p=head->next; /*重新遍历应该从头开始*/
while(p!=NULL)
{
if(p->next!=NULL)
printf("%d ",p->data);
else printf("%d\n",p->data);
p=p->next;
}
}
数据结构实验之链表一:顺序建立链表
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。输入
第一行输入整数的个数N;第二行依次输入每个整数。
输出
输出这组整数。示例输入
8 12 56 4 6 55 15 33 62
示例输出
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,*q,*tail;
int i,n;
scanf("%d",&n);
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
while(n--) /*第一个插入的结点为第一个结点,然后依次往后插入结点 */
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p; /*p结点已达到链表结尾*/
}
p=head->next; /*重新遍历应该从头开始*/
while(p!=NULL)
{
if(p->next!=NULL)
printf("%d ",p->data);
else printf("%d\n",p->data);
p=p->next;
}
}
相关文章推荐
- 数据结构实验之栈:行编辑器
- 2117数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表五:单链表的拆分
- 数据结构与算法JavaScript - 集合
- [从头学数学] 第251节 Python实现数据结构:二叉查找树
- 最长公共子序列求解:递归与动态规划方法
- 前端接口的数据结构
- 选择排序
- 数据结构与算法JavaScript - 散列
- java数据结构之子最大子数组和
- 数据结构实验之栈五:下一较大值(一)
- [综合面试] 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- SDUT2131数据结构实验之栈一:进制转换(栈)
- 数据结构实验之栈一:进制转换
- 重温数据结构(一)
- 有序链表的建立
- POJ 2442 优先队列
- 数据结构与算法JavaScript - 字典
- Linux内核之数据结构--链表
- 数据结构——红黑树