师--链表的结点插入(PS:纯C代码)
来源:互联网 发布:压力传感器的数据采集 编辑:程序博客网 时间:2024/05/16 04:33
师–链表的结点插入
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
给出一个只有头指针的链表和 n 次操作,每次操作为在链表的第 m 个元素后面插入一个新元素x。若m 大于链表的元素总数则将x放在链表的最后。
Input
多组输入。每组数据首先输入一个整数n(n∈[1,100]),代表有n次操作。
接下来的n行,每行有两个整数Mi(Mi∈[0,10000]),Xi。
Output
对于每组数据。从前到后输出链表的所有元素,两个元素之间用空格隔开。
Example Input
4
1 1
1 2
0 3
100 4
Example Output
3 1 2 4
Hint
#include <stdio.h>#include <stdlib.h>struct node{ int data; struct node *next;};struct node* _add(int m, int x,struct node *head){ struct node *q = (struct node *)malloc(sizeof(struct node)); q->data = x; q->next = NULL; struct node *p = head->next; struct node *pp = head; while(m-- && p) { p = p->next; pp = pp->next; } if(m) { pp->next = q; q->next = p; } else { pp ->next = q; q->next = NULL; } return head;}int main(){ int n; int m, x; while(scanf("%d",&n)!=EOF){ struct node *head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; while(n--) { scanf("%d%d",&m,&x); head = _add(m,x,head); } head = head->next; while(head){ printf("%d",head->data); head = head->next; if(head) printf(" "); } printf("\n"); } return 0;}
0 0
- 师--链表的结点插入(PS:纯C代码)
- 链表C 链表的结点插入
- C语言实现双向非循环链表(带头结点尾结点)的节点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 师--链表的结点插入
- 关于链表的新建,删除结点,插入结点的代码
- 链表结点的插入
- C语言实现链表之单向链表(五)头结点前插入结点
- C语言实现链表之单向链表(七)尾结点后插入结点
- C语言实现链表之双向链表(五)头结点前插入结点
- 希望能够坚持下去!
- Tomcat配置虚拟主机的两种方式
- 使用edittext搜索时,更改软键盘的回车为搜索
- 【c语言基础】字符串数组与字符串指针的理解心得
- XML,schema,dtd文件结构
- 师--链表的结点插入(PS:纯C代码)
- about abs guide
- JS的prototype和__proto__
- MongoDB readConcern readPreference writeConcern 原理解析
- 《数据结构和算法》之逆波兰表达式
- 洛谷2658 汽车拉力比赛 二分
- Win7,64位,Python2.7.2下的opencv配置
- atexit函数和粘制位问题及file
- OpenCV中SVM参数选择