3329-顺序表应用5:有序顺序表归并

来源:互联网 发布:淘宝免单活动在哪里看 编辑:程序博客网 时间:2024/05/29 04:46
#include <bits/stdc++.h>#define Max 1010000using namespace std;typedef int ElemType;class List{public:    ElemType *elem;    int maxSize;    int size;    List() /*List类的无参构造函数*/    {        elem = (ElemType *)malloc(Max * sizeof(ElemType));        maxSize = Max;        size = 0;    }    void CreatList(int n) /*建表*/    {        int x;        for(int i = 0; i < n; i++)        {            cin >> x;            elem[size++] = x;        }    }    void MergeList(List a, List b)    {        int i,j;        size = 0;        i = 0;        j = 0;        while(i < a.size && j < b.size)        {            if(a.elem[i] < b.elem[j])            {                elem[size++] = a.elem[i];                i++;            }            else            {                elem[size++] = b.elem[j];                j++;            }        }        while(i < a.size)        {            elem[size++] = a.elem[i];            i++;        }        while(j < b.size)        {            elem[size++] = b.elem[j];            j++;        }    }    void OutputList() /*输出*/    {        for(int i = 0; i < size; i++)            i == size - 1? cout << elem[i] << endl : cout << elem[i] << " ";    }    void DestroyList() /*销毁顺序表*/    {        delete elem;    }};int main(){    List a, b, c;    int n, m;    cin>>n>>m;    a.CreatList(n);    b.CreatList(m);    c.MergeList(a,b);    a.DestroyList();    b.DestroyList();    c.OutputList();    c.DestroyList();    return 0;}
原创粉丝点击