顺序表应用5:有序顺序表归并
題目描述
已知順序表A與B是兩個(gè)有序的順序表,其中存放的數(shù)據(jù)元素皆為普通整型,將A與B表歸并為C表,要求C表包含了A、B表里所有元素,并且C表仍然保持有序。輸入
輸入分為三行:第一行輸入m、n(1<=m,n<=10000)的值,即為表A、B的元素個(gè)數(shù);
第二行輸入m個(gè)有序的整數(shù),即為表A的每一個(gè)元素;
第三行輸入n個(gè)有序的整數(shù),即為表B的每一個(gè)元素;
輸出
輸出為一行,即將表A、B合并為表C后,依次輸出表C所存放的元素。示例輸入
5 3 1 3 5 6 9 2 4 10示例輸出
1 2 3 4 5 6 9 10
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LISTINCREASMENT 100 ? ? ? ? ? ? ?
#define ?LISTSIZE 10 ? ? ? ? ? ? ? ? ? ? ? ? ?
#define ?OVERFLOW -1
#define ?OK 1
typedef int ElemType;
typedef struct ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
{
? ? ElemType * elem;
? ? int length;
? ? int listsize;
} Sqlist;
int SqInitial(Sqlist &L) ? ? ? ? ? ? ? ? ? ? ? ? ?
{
? ? L.elem=(ElemType *) malloc (LISTSIZE*sizeof(ElemType));
? ? if (!L.elem) ?exit(OVERFLOW); // 當(dāng)前存儲(chǔ)空間已滿;
? ? L.length=0;
? ? L.listsize=LISTSIZE;
? ? return OK;
}
int ListInsert(Sqlist &L,int i,ElemType e) ? ? ?// 在順序表L的第 i 個(gè)元素之前插入新的元素e, ? ? ?
{
? ? if(i<1|| i > L.length+1) printf("ERROR!");// 插入位置不合法
? ? if(L.length>=L.listsize)
? ? {
? ? ? ? ElemType*newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREASMENT)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *sizeof(ElemType));
? ? ? ? if(!newbase) ? return ?OVERFLOW; 當(dāng)前存儲(chǔ)空間已滿;
? ? ? ? L.elem=newbase;
? ? ? ? L.listsize+=LISTINCREASMENT; ? ?
? ? }
? ? ElemType * ?q=&(L.elem[i-1]);
? ? ElemType * ?p;
? ? for(p=&(L.elem[L.length-1]); p>=q; --p)
? ? ? ? *(p+1)=*p; // 插入位置及之后的元素右移;
? ? *q=e; // 插入e;
? ? ++L.length; ?// 表長(zhǎng)增1;
? ? return OK;
}
void he(Sqlist &Lb,Sqlist L,Sqlist La)//將表L表La的元素按從小到大插入表Lb;
{
? ? int i=0,k=0,j=0;
? ? while(L.length&&La.length)//表L,表La不為空時(shí);
? ? {
? ? ? ? if(L.elem[i]<La.elem[j])
? ? ? ? {
? ? ? ? ? ? Lb.elem[k]=L.elem[i];
? ? ? ? ? ? L.length--;i++;k++;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? ?Lb.elem[k]=La.elem[j];
? ? ? ? ? ? La.length--;j++;k++;
? ? ? ? }
? ? }
? ? while(L.length)//表L不為空表La為空
? ? {
? ? ? ? Lb.elem[k]=L.elem[i];i++;k++;
? ? ? ? L.length--;
? ? }
? ? while(La.length)//表La不為空L為空;
? ? {
? ? ? ? ?Lb.elem[k]=La.elem[j];
? ? ? ? ? ? La.length--;j++;k++;
? ? }
}
int main()
{
? ? int i,j,k,m,n,h,len;
? ? ? ? Sqlist L;//表的定義;
? ? ? ? SqInitial(L);//表的初始化;
? ? ? ? Sqlist La;
? ? ? ? SqInitial(La);
? ? ? ? Sqlist Lb;
? ? ? ? SqInitial(Lb);
? ? ? ? scanf("%d%d",&len,&m);
? ? ? ? for(j=1;j<=len; j++)
? ? ? ? {
? ? ? ? ? ? scanf("%d",&k);
? ? ? ? ? ? ListInsert(L,j,k);//表元素的插入;
? ? ? ? }
? ? ? ? for(h=1;h<=m;h++)
? ? ? ? {
? ? ? ? ? ? scanf("%d",&k);
? ? ? ? ? ? ListInsert(La,h,k);//表元素的插入;
? ? ? ? }
? ? ? ? he(Lb,L,La);//兩表合成一表;
? ? ? ? for(j=1;j<=Lb.length;j++)//表元素的輸出;
? ? ? ? {
? ? ? ? ? ? if(j!=Lb.length) printf("%d ",Lb.elem[j-1]);
? ? ? ? ? ? else
? ? ? ? ? ? ? ? printf("%d\n",Lb.elem[j-1]);
? ? ? ? }
}
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=20010;
typedef struct
{
? ? int *emue;
? ? int lengh;
}lis;
void initlist(lis *L)
{
? ? L->emue=new int [maxn];
? ? L->lengh=0;
}
void creat(lis &L, int k)
{
? ? L.lengh=k;
? ? for(int i=0;i<L.lengh;i++)
? ? {
? ? ? ? cin>>L.emue[i];
? ? }
}
void merger(lis &L1, lis &L2, lis &L3)
{
? ? int k=0, i=0, j=0;
? ? while(i<L1.lengh&&j<L2.lengh)
? ? {
? ? ? ? if(L1.emue[i]>L2.emue[j])
? ? ? ? {
? ? ? ? ? ? L3.emue[k]=L2.emue[j];
? ? ? ? ? ? j++;
? ? ? ? ? ? k++;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? L3.emue[k]=L1.emue[i];
? ? ? ? ? ? i++;
? ? ? ? ? ? k++;
? ? ? ? }
? ? }
? ? for(int n=i;n<L1.lengh;n++)
? ? {
? ? ? ? L3.emue[k]=L1.emue[n];
? ? ? ? k++;
? ? }
? ? for(int n=j;n<L2.lengh;n++)
? ? {
? ? ? ? L3.emue[k]=L2.emue[n];
? ? ? ? k++;
? ? }
? ? L3.lengh=k;
}
int main()
{
? ? lis L1, L2, L3;
? ? int m, n;
? ? cin>>n>>m;
? ? initlist(&L1);
? ? creat(L1, n);
? ? initlist(&L2);
? ? creat(L2, m);
? ? initlist(&L3);
? ? merger(L1, L2, L3);
? ? for(int i=0;i<L3.lengh;i++)
? ? ? ? cout<<L3.emue[i]<<" ";
? ? return 0;
}
#include <iostream>
using namespace std;
?int a[10010],b[10010],c[10100];
?int i,j;
?int m,n,k;
void merger(int a[],int b[],int c[])
{
? ? ? k=0;
? ? ?i=0,j=0;
? ? ?while(i<n&&j<m)
? ? ?{
? ? ? ? ? if(a[i]>b[j])
? ? ? ? ? {
? ? ? ? ? ? ? c[k++]=b[j++];
? ? ? ? ? }
? ? ? ? ? else
? ? ? ? ? ? c[k++]=a[i++];
? ? ?}
? ? ?while(i<n)
? ? ?{
? ? ? ? ?c[k++]=a[i++];
? ? ?}
? ? ?while(j<m)
? ? ?{
? ? ? ? ?c[k++]=b[j++];
? ? ?}
}
int main()
{
? ? cin>>n>>m;
? ? for(i=0;i<n;i++)
? ? ? ? cin>>a[i];
? ? for(j=0;j<m;j++)
? ? ? ? cin>>b[j];
? ? merger(a,b,c);
? ? ?for(i=0;i<k-1;i++)
? ? ? ? cout<<c[i]<<" ";
? ? ? ? cout<<c[k-1]<<endl;
? ? return 0;
}
總結(jié)
以上是生活随笔為你收集整理的顺序表应用5:有序顺序表归并的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言fgets函数了解
- 下一篇: 来淄博旅游