博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
师--链表的结点插入
阅读量:3948 次
发布时间:2019-05-24

本文共 1061 字,大约阅读时间需要 3 分钟。

师–链表的结点插入

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

给出一个只有头指针的链表和 n 次操作,每次操作为在链表的第 m 个元素后面插入一个新元素x。若m 大于链表的元素总数则将x放在链表的最后。
Input
多组输入。每组数据首先输入一个整数n(n∈[1,100]),代表有n次操作。
接下来的n行,每行有两个整数Mi(Mi∈[0,10000]),Xi。
Output
对于每组数据。从前到后输出链表的所有元素,两个元素之间用空格隔开。
Sample Input
4
1 1
1 2
0 3
100 4
Sample Output
3 1 2 4

代码如下:

#include
#include
struct node{ int data; struct node *next;};int main(){ int n,m,x; struct node *head,*p,*q; while(~scanf("%d",&n)) { head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; while(n--) { q=head; scanf("%d %d",&m,&x); while(m--&&q->next) q=q->next; p=(struct node *)malloc(sizeof(struct node)); p->data=x; p->next=NULL; p->next=q->next; q->next=p; } q=head->next; while(q) { if(q->next==NULL) printf("%d\n",q->data); else printf("%d ",q->data); q=q->next; } } return 0;}

转载地址:http://gwhwi.baihongyu.com/

你可能感兴趣的文章
Using ViewPager for Screen Slides 使用屏幕幻灯片ViewPager
查看>>
Displaying Card Flip Animations 显示卡片翻转动画
查看>>
Zooming a View 缩放视图
查看>>
Animating Layout Changes 动画布局的更改
查看>>
Controlling Your App’s Volume and Playback 控制应用程序的音量和播放
查看>>
Managing Audio Focus 管理音频焦点
查看>>
Dealing with Audio Output Hardware 处理音频输出硬件设备
查看>>
Monitoring the Battery Level and Charging State 监测电池电量和充电状态
查看>>
Determining and Monitoring the Docking State and Type 判断并监测设备的停驻状态与类型
查看>>
Determining and Monitoring the Connectivity Status 根据网络连接状况去省电
查看>>
Manipulating Broadcast Receivers On Demand 按需操控广播接收
查看>>
Creating a View Class 创建一个视图类
查看>>
Custom Drawing 自定义绘制
查看>>
Making the View Interactive 视图互动
查看>>
Optimizing the View 优化视图
查看>>
Setting Up the Search Interface 设置搜索界面
查看>>
Storing and Searching for Data 数据存储和搜索
查看>>
Remaining Backward Compatible 保持向后兼容
查看>>
Remembering Your User 记住你的用户
查看>>
Authenticating to OAuth2 Services 验证OAuth2服务
查看>>