单链表的插入(单链表)
来源:互联网 发布:淘宝买家内衣秀大尺度 编辑:程序博客网 时间:2024/06/07 07:09
1.题目:
Problem Description
建立单链表,在第n个结点后插入指定结点,并完成遍历操作。
Input
输入数据有多组,每组数据占两行:
第一行有两个数字(n,m),第一个数n表示结点位置,第二个数表示指定需插入的数。后跟单链表各结点(不会超过100),以0结束一个单链表。
例如:
3 5
1 2 3 4 5 0
0 0
遇到0 0,结束程序。
第一行有两个数字(n,m),第一个数n表示结点位置,第二个数表示指定需插入的数。后跟单链表各结点(不会超过100),以0结束一个单链表。
例如:
3 5
1 2 3 4 5 0
0 0
遇到0 0,结束程序。
Output
输出插入后的单链表,每组输出占一行,每个元素之间有一个空格。
Sample Input
3 51 2 3 4 5 00 0
Sample Output
1 2 3 5 4 5
2.参考代码:
#include <iostream>using namespace std;struct Node{int data;Node* next;};class LinkList{private:Node* head;public:LinkList(int* a,int n);~LinkList();void insert(int i,int x);void show();};LinkList::LinkList(int* a,int n){Node* r,* s;head=new Node;r=head;for(int i=0;i<n;i++){s=new Node;s->data=a[i];r->next=s;r=s;}r->next=NULL;}LinkList::~LinkList(){Node* p,* q;p=head;while(p){q=p;p=p->next;delete q;}}void LinkList::insert(int i,int x){Node* p,* s;p=head->next;int count=0;while(p && count<i){p=p->next;count++;}if(p){s=new Node;s->data=x;s->next=p->next;p->next=s;}}void LinkList::show(){Node* p=head->next;if(p){cout<<p->data;p=p->next;while(p){cout<<" "<<p->data;p=p->next;}cout<<endl;}}int main(){int n,m,a[1111];while(cin>>n>>m){if(n==0 || m==0)break;int i=0;while(cin>>a[i]){if(a[i]==0)break;elsei++;}LinkList w(a,i);w.insert(n,m);w.show();}return 0;}
- 单链表的插入(单链表)
- 单链表的插入(单链表)
- 单链表的插入
- 单链表的插入
- 单链表的插入
- 单链表的插入排序
- 单链表的插入删除
- 单链表的插入
- 单链表的插入删除
- 单链表的插入
- 单链表节点的插入
- 单链表的插入排序
- 单链表的插入操作
- 实现单链表的插入
- 单链表的插入操作
- 单链表的插入问题
- 单链表的插入删除
- 单链表的插入
- Pascal's Triangle -leetcode
- 小小代码模拟售票-创建开启进程1
- Advanced Topics in Python
- 保持一颗阳光的心
- sscanf,sprintf
- 单链表的插入(单链表)
- android Intent的Action、Category属性
- 占位符文本(Placeholder text),html5里面的一个知识点!
- ubuntu 10.04 下安装 kscope1.6.2
- Sicily 1028. Hanoi Tower Sequence
- HDU1011Starship Troopers(DP+DFS)深度优先搜索+动态规划
- android Toast
- [Python]用正则表达式进行word Count
- HDU 4279 2012网络赛Number(数论 欧拉函数结论约数个数)