两个有序链表序列的合并(20 分)
2017-09-29 01:00
471 查看
两个有序链表序列的合并(20 分)
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的并集新非降序链表S3。输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL。
输入样例:
1 3 5 -1
2 4 6 8 10 -1
输出样例:
1 2 3 4 5 6 8 10
思路
哈哈,这道题不能用stl的set_XXX方法解决。源码
/* Name:7-5 两个有序链表序列的合并(20 分) Author: shou1651312 Date:2017年9月18日 00:55:02 Description:数据结构实验1-2 */ #include<iostream> #include<cmath> #include<string> #include<cstring> #include<algorithm> #include<vector> #include<set> #include<map> #include<numeric> using namespace::std; //int main() //version1 STL 11/20; //主要问题是STL中的union和题中要求的“合并”不是一个意思 //{ // vector<int> V1; // vector<int> V2; // while(true) // { // int input; // scanf("%d",&input); // if(input!=-1) // V1.push_back(input); // else // break; // } // while(true) // { // int input; // scanf("%d",&input); // if(input!=-1) // V2.push_back(input); // else // break; // } // int l1=V1.size(),l2=V2.size(); // if(l1==0&&l2==0) // { // printf("NULL"); // return 0; // } // vector<int> V3(l1+l2); // set_union(V1.begin(),V1.end(),V2.begin(),V2.end(),V3.begin()); // auto iterator=unique(V3.begin(),V3.end()); // V3.erase(iterator,V3.end()); // auto it=V3.begin(); // printf("%d",*it); // it++; // for(;it!=V3.end();it++) // printf(" %d",*it); // return 0; //} struct Node //version2 chainlist 20/20 { int num; struct Node *next; }; typedef struct Node * PNODE; PNODE read()//读入数据 { PNODE t,pTile,phead = (PNODE)malloc(sizeof(struct Node)); phead->next = NULL; pTile = phead; int number; do { scanf("%d",&number); if(number != -1) { PNODE pnew = (PNODE)malloc(sizeof(struct Node)); pnew->num = number; pnew->next = NULL; pTile->next = pnew; pTile = pnew; } } while(number != -1); t = phead; phead = t->next; free(t);//删除临时头结点 return phead; } void Attach(int number,PNODE * prear) { PNODE pnew = (PNODE)malloc(sizeof(struct Node)); pnew->num = number; pnew->next = NULL; (*prear)->next = pnew; *prear = pnew; } PNODE combination(PNODE p1,PNODE p2)//将p1,p2按非降序一个一个接上 { PNODE t,rear,p = (PNODE)malloc(sizeof(struct Node)); p->next = NULL; rear = p; while(p1 && p2) { if(p1->num > p2->num) { Attach(p2->num,&rear); p2 = p2->next; } else if(p1->num == p2->num)//当数相等时,要将p1,p2的数都接入,非降序列每项不一定严格大于它的前一项 { Attach(p1->num,&rear); Attach(p2->num,&rear); p1 = p1->next; p2 = p2->next; } else { Attach(p1->num,&rear); p1 = p1->next; } } while(p1) { Attach(p1->num,&rear); p1 = p1->next; } while(p2) { Attach(p2->num,&rear); p2 = p2->next; } t = p; p = p->next; free(t); return p; } void print(PNODE p) { if(!p) { printf("NULL"); return; } int flag = 0; while(p) { if(flag == 0) { printf("%d",p->num); flag = 1; } else printf(" %d",p->num); p = p->next; } printf("\n"); } int main(void) { PNODE p1,p2,p; p1 = read(); p2 = read(); p = combination(p1,p2); print(p); return 0 ; }
相关文章推荐
- 7-15 两个有序链表序列的合并(20 分)
- 7-1 两个有序链表序列的合并(20 分)
- 7-1 两个有序链表序列的合并(20 分)
- 7-1 两个有序链表序列的合并(20 分)
- 5-51 两个有序链表序列的合并(20分)
- 7-2 两个有序链表序列的交集(20 分)
- Programming Ability Test学习 2-11. 两个有序链表序列的合并(15)
- Programming Ability Test学习 2-12. 两个有序链表序列的交集(20)
- 6-4 两个有序链表序列的合并
- 5-51 两个有序链表序列的合并
- 数据结构 02-线性结构1 两个有序链表序列的合并
- PATl两个有序链表序列的合并
- 2-11. 两个有序链表序列的合并(15)
- 7-2 两个有序链表序列的合并
- PAT数据结构_02-线性结构1 两个有序链表序列的合并 (15分)
- 两个有序链表序列的合并
- 02-线性结构1 两个有序链表序列的合并 (15分)
- 02-线性结构1 两个有序链表序列的合并
- 两个有序链表序列的合并 PAT
- 7-16 两个有序链表序列的交集(20 分)