九度oj 题目1181:遍历链表
2024-09-06 00:54:52
题目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 ;
}
最新文章
- 最简明的JavaScript闭包解释
- java大数取模
- Displaying a full list of groups in Odoo&#39;s Kanban view
- php 图片处理类
- netstat用法
- swift-01-利用元组判断字符串出现次数
- JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮
- servlet操作数据库
- [Centos] mod_wsgi 安装流程以及遇到问题解决办法。apxs: command not found 或 Sorry, Python developer package does not appear to be installed.
- DOM节点删除之empty和remove区别
- 《Python黑客编程之极速入门》正式开课
- 龙光集团地产跃居“中国房地产500强TOP28”
- IdentityServer4之Implicit(隐式许可)
- Ubuntu install flash
- 解决stackoverflow打开缓慢的问题
- 【软件分析与挖掘】ELBlocker: Predicting blocking bugs with ensemble imbalance learning
- Node.js文件操作二
- OSG漫游到指定坐标点位置
- Jquery源码分析之匿名函数的自执行
- C#典型案例及分析
热门文章
- GC_CONCURRENT freed 循环不停打印日志
- NSSet转成NSArray 以及NSSortDescriptor的使用
- tabBar隐藏方式
- ambari-server启动报错500 status code received on GET method for API:/api/v1/stacks/HDP/versions/2.4/recommendations Error message : Server Error解决办法(图文详解)
- 转】Nodejs对MongoDB模糊查询
- Bmob使用心得
- v使用索引的注意事项及常见场景、案例
- 结对项目--黄金点游戏(邓乐&;曾亮)
- 恩智浦Freescale Cortex-A9 迅为IMX6开发板平台初体验
- bootstrap 整理