2207: 实现链表(线性表)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 276 Solved: 188
[ Submit][ Status][ Web Board]
Description
(线性表)顺序结构线性表LA与LB的结点关键字为整数。LA与LB的元素按非递减有序,线性表空间足够大。试用给出一种高效算法,将LB中元素合到LA中,使新的LA的元素仍保持非递减有序。高效指最大限度的避免移动元素。
Input
输入LA长度m:7
输入数据:3 7 11 15 57 68 99
输入LB长度n:7
输入数据:6 7 8 9 10 23 67
Output
3 6 7 8 9 10 11 15 23 57 67 68 99
Sample Input
7
4 6 7 9 10 16 23
8
1 2 4 7 8 13 15 44
Sample Output
1 2 4 6 7 8 9 10 13 15 16 23 44
#include <iostream> #include<malloc.h> using namespace std; int a[100],b[100]; typedef int ElemType; typedef struct { ElemType data[100]; int length; }SqList; void InitList(SqList *&L) { L=(SqList *)malloc(sizeof(SqList)); L->length=0; } void CreatList(SqList *&L,ElemType a[],int n) { int i; L=(SqList *)malloc(sizeof(SqList)); for(i=0;i<n;i++) L->data[i]=a[i]; L->length=n; } int UnionList(SqList *LA,SqList *LB,SqList *&LC) { int i=0,j=0,k=0; LC=(SqList *)malloc(sizeof(SqList)); while(i<LA->length&&j<LB->length) { if(LA->data[i]<LB->data[j]) { LC->data[k]=LA->data[i]; i++;k++; } else if(LA->data[i]==LB->data[j]) { LC->data[k]=LA->data[i]; i++;k++;j++; } else { LC->data[k]=LB->data[j]; j++;k++; } } while(i<LA->length) { LC->data[k]=LA->data[i]; i++;k++; } while(j<LB->length) { LC->data[k]=LB->data[j]; j++,k++; } LC->length=k; return 1; } void OutputList(SqList *&L) { int i; for(i=0;i<L->length;i++) cout<<L->data[i]<<' '; cout<<endl; } int main() { SqList *LA,*LB,*LC; int m,n,i; cin>>m; for(i=0;i<m;i++) cin>>a[i]; cin>>n; for(i=0;i<n;i++) cin>>b[i]; CreatList(LA,a,m); CreatList(LB,b,n); if(UnionList(LA,LB,LC)) OutputList(LC); return 0; }