题目1181:遍历链表

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3483

解决:1465

题目描述:

建立一个升序链表并遍历输出。

输入:

输入的每个案例中第一行包括1个整数:n(1<=n<=1000),接下来的一行包括n个整数。

输出:

可能有多组测试数据,对于每组数据,
将n个整数建立升序链表,之后遍历链表并输出。

样例输入:
4
3 5 7 9
样例输出:
3 5 7 9
#include <iostream>
#include <cstdio>
#include <cstdlib> using namespace std; typedef struct node{
int data;
struct node* next;
} LinkNode, *LinkList; void traversal(LinkList L){
LinkNode* tmp = L->next;
if(tmp != NULL)
printf("%d", tmp->data);
tmp = tmp->next;
while(tmp != NULL){
printf(" %d", tmp->data);
tmp = tmp->next;
}
printf("\n");
} void creat(LinkList &L, int data){
LinkNode* temp = (LinkNode*)malloc(sizeof(LinkNode));
temp->data = data;
temp->next = NULL;
if(L->next == NULL){
L->next = temp;
return;
}
LinkNode* cur = L->next, *pre = L;
while(cur && cur->data < temp->data){
pre = cur;
cur = cur->next;
}
if(cur == NULL){
temp->next = cur;
pre->next = temp;
} else {
temp->next = cur;
pre->next = temp;
}
return;
} int main(){
int n, i, num;
while(scanf("%d", &n) != EOF){
LinkList L;
L->next = NULL;
for(i = ; i < n; i++){
scanf("%d", &num);
creat(L, num);
}
traversal(L);
}
return ;
}
 

最新文章

  1. 最简明的JavaScript闭包解释
  2. java大数取模
  3. Displaying a full list of groups in Odoo&#39;s Kanban view
  4. php 图片处理类
  5. netstat用法
  6. swift-01-利用元组判断字符串出现次数
  7. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮
  8. servlet操作数据库
  9. [Centos] mod_wsgi 安装流程以及遇到问题解决办法。apxs: command not found 或 Sorry, Python developer package does not appear to be installed.
  10. DOM节点删除之empty和remove区别
  11. 《Python黑客编程之极速入门》正式开课
  12. 龙光集团地产跃居“中国房地产500强TOP28”
  13. IdentityServer4之Implicit(隐式许可)
  14. Ubuntu install flash
  15. 解决stackoverflow打开缓慢的问题
  16. 【软件分析与挖掘】ELBlocker: Predicting blocking bugs with ensemble imbalance learning
  17. Node.js文件操作二
  18. OSG漫游到指定坐标点位置
  19. Jquery源码分析之匿名函数的自执行
  20. C#典型案例及分析

热门文章

  1. GC_CONCURRENT freed 循环不停打印日志
  2. NSSet转成NSArray 以及NSSortDescriptor的使用
  3. tabBar隐藏方式
  4. ambari-server启动报错500 status code received on GET method for API:/api/v1/stacks/HDP/versions/2.4/recommendations Error message : Server Error解决办法(图文详解)
  5. 转】Nodejs对MongoDB模糊查询
  6. Bmob使用心得
  7. v使用索引的注意事项及常见场景、案例
  8. 结对项目--黄金点游戏(邓乐&amp;曾亮)
  9. 恩智浦Freescale Cortex-A9 迅为IMX6开发板平台初体验
  10. bootstrap 整理