九度OJ 1181:遍历链表 (链表、排序)
来源:互联网 发布:java中的restfull接口 编辑:程序博客网 时间:2024/05/16 18:03
- 题目描述:
建立一个升序链表并遍历输出。
- 输入:
输入的每个案例中第一行包括1个整数:n(1<=n<=1000),接下来的一行包括n个整数。
- 输出:
可能有多组测试数据,对于每组数据,
将n个整数建立升序链表,之后遍历链表并输出。
- 样例输入:
43 5 7 9
- 样例输出:
3 5 7 9
- 来源:
- 2000年华中科技大学计算机研究生机试真题
代码:
#include <stdio.h>#include <stdlib.h> #define N 1000 struct node { int key; struct node *next;}; struct node *insert(struct node *head, int key){ if (head == NULL) { head = (struct node *)malloc(sizeof(struct node)); head->key = key; head->next = NULL; return head; } struct node *p = head, *p0; p0 = p; while (p && p->key < key) { p0 = p; p = p->next; } struct node *pnew = (struct node *)malloc(sizeof(struct node)); pnew->key = key; pnew->next = p; if (p == head) return pnew; p0->next = pnew; return head;} int main(void){ int n, i, key; struct node *head; while (scanf("%d", &n) != EOF) { head = NULL; for(i=0; i<n; i++) { scanf("%d", &key); head = insert(head, key); } printf("%d", head->key); head = head->next; while (head) { printf(" %d", head->key); head = head->next; } printf("\n"); } return 0;}/************************************************************** Problem: 1181 User: liangrx06 Language: C Result: Accepted Time:170 ms Memory:6192 kb****************************************************************/
0 0
- 九度OJ 1181:遍历链表 (链表、排序)
- 九度oj-1181-遍历链表
- 九度OJ 题目1181:遍历链表
- 九度OJ 题目1181:遍历链表
- 【九度OJ】题目1181:遍历链表 解题报告
- 九度OJ 1202:排序 (排序)
- 九度oj-合并两个排序的链表
- 九度OJ 1014:排名 (排序)
- 九度OJ 1339:ACM (排序)
- 九度oj 1181 升序链表
- 九度oj 文献排序
- 九度OJ 1202 排序
- 九度OJ 1202:排序
- 【九度OJ】1041【排序】
- 九度OJ 1202 排序
- 九度OJ 1202 排序
- 九度OJ-1202:排序
- 【九度OJ】1202:排序
- JQ基础使用备忘技巧(一)
- Android Hook神器:XPosed入门与登陆劫持演示
- 向量空间初步
- Ubuntu12.04配置JDK1.8
- HttpClient和HttpURLConnection的区别
- 九度OJ 1181:遍历链表 (链表、排序)
- HTML1-排版,字体,列表,图像,超链接
- 【Servlet】Javaweb中文字转图片
- 《C程序设计语言》学习笔记——练习3-3
- LeetCode_Remove Duplicates from Sorted Array
- Hibernate在进行one-to-many时的级联设置-cascade
- getDimension,getDimensionPixelOffset和getDimensionPixelSize的一点说明
- Qt的三方库
- 327 (Div. 2) B