[数据结构一]线性表:顺序实现

// 数据结构——1、顺序线性列表

#include <iostream>
using namespace std;

#define INIT_LIST_SIZE 100  // 初始的列表容量大小
#define INCREACE_SIZE 10    // 空间不够时增加的容量大小

#define OK 1
#define ERROR -1


typedef int ElemType;

typedef struct  
{
    ElemType *elem; //数据元素
    int length;        //线性表长度
    int listSize;   //线性表容量
}SqList;


// 构造空表
int InitList_Sq(SqList &L) // 创建空表
{
    L.elem = new ElemType[INIT_LIST_SIZE * sizeof(ElemType)];
    if(L.elem == NULL) return ERROR;
    L.length = 0;
    L.listSize = INIT_LIST_SIZE;
    return OK;
}

// 销毁表
int DestroyList_Sq(SqList &L) // 销毁列表
{
    if(!L.elem) return ERROR;
    delete [] L.elem;    
    L.length = L.listSize = 0;
    return OK;
}

// 清空表
int ClearList_Sq(SqList &L) // 清空列表
{
    if(!L.elem) return ERROR;
    L.length = 0;
    return OK;
}

// 判断表是否为空
int ListEmpty_Sq(SqList L) // 判断列表是否为空
{
    if(!L.elem) return ERROR;
    if(L.length == 0) return OK;
    return ERROR;
}

// 求出表的长度
int ListLength_Sq(SqList L) // 获取列表长度
{
    if(!L.elem) return ERROR;
    return L.length;
}

// 把下标为i 的元素赋值给 e
int GetElem(SqList L, int i, ElemType &e) // 用e返回i处的值
{
    if(!L.elem) return ERROR;
    if(i >= 0 && i < L.length)
    {
        e = L.elem[i];
        return OK;
    }
    return ERROR;
}

// 返回第一个和e相同的元素
int LocatElem(SqList L, ElemType e/*, compare()*/)// 返回L中第一个与e满足compare()的数据元素的下标
{
    int i = 0;
    while(i < L.length)
    {
        if(e == L.elem[i])
        {
            return i;
        }
        i++;
    }
    return ERROR;
}

// 返回前一个元素个 pre_e
int PriorElem(SqList L, ElemType e, ElemType &pre_e) // 返回e前面一个元素
{
    if(!L.elem) return ERROR;
    if(LocatElem(L, e) != ERROR)
    {
        int i = 1;
        ElemType temp_pre = 0;
        while(i++ < (L.length-1) )
        {
            temp_pre = L.elem[i];
            if(e == L.elem[i+1]) pre_e = temp_pre;
            return OK;
        }
    }
    return ERROR;
}

int NextElem(SqList L, ElemType e, ElemType &next_e) // 返回e后面一个元素
{
    if(!L.elem) return ERROR;
    if(LocatElem(L,e) != (L.length -1))
    {
        int i = 1;
        ElemType temp_next = 0;
        while(i++ < L.length )
        {
            temp_next = L.elem[i - 1];
            if(e == L.elem[i]) next_e = temp_next;
            return OK;
        }
    }
    return ERROR;
}

int ListInsert_Sq(SqList &L, int index, ElemType e) // 在index 之前插入e,length+1
{
    if(!L.elem) return ERROR;
    if(index < 0 || index > L.length) return ERROR;
    // 因为每次增加内存是增加 10 ,所以单位是10,每次判断是否需要分配新内存,就看当前的length是否为 10 的整数倍
    if(L.length == L.listSize) // 最后一个元素,需要增加list的长度
    {
        ElemType * tempElem = new ElemType(L.length * sizeof(ElemType));
        if(!tempElem) return ERROR;
        int i = 0;
        while(i++ < L.length)
        {
            tempElem[i] = L.elem[i];
        }
        delete [] L.elem;
        L.elem = new ElemType(L.length + INCREACE_SIZE);
        if(!L.elem) return ERROR;
        i = 0;
        while(i++ < L.length)
        {
             L.elem[i] = tempElem[i];
        }

        L.listSize += INCREACE_SIZE;
    }

    for(int i = L.length -1; i >= index; --i)
    {
        L.elem[i+1] = L.elem[i];
    }

    L.elem[index] = e;
    L.length ++;
    return OK;
}

int ListDelet_Sq(SqList &L, int index, ElemType &e) // 删除index位置的元素
{
    if(!L.elem) return ERROR;
    if(index >= 0 && index < L.length)
    {
        int i = index;
        while(i++ < (L.length-1))
        {
            L.elem[i] = L.elem[i+1];
        }
        L.length--;
        return OK;
    }
    return ERROR;
}


//////////////////////////////////////////////////////////////////////////
// 其他算法
//////////////////////////////////////////////////////////////////////////

// 求集合A B的并集,把B中的元素但没在A中的并到A中
void UnionList(SqList &La, SqList Lb)
{
    for(int i = 0; i < ListLength_Sq(Lb); ++i)
    {
        ElemType e;
        GetElem(Lb, i, e);
        if(LocatElem(La, e) == -1)
        {
            ListInsert_Sq(La, ListLength_Sq(La), e);
        }
    }
}

//////////////////////////////////////////////////////////////////////////
//  La,Lb 非递减排列的元素,合并到Lc,仍然非递减
void MergeList(SqList La, SqList Lb, SqList &Lc)
{
    int Len_La = ListLength_Sq(La);
    int Len_lb = ListLength_Sq(Lb);

    int la = 0;
    int lb = 0;
    int lc = 0;
    while(la < Len_La && lb < Len_lb)
    {
        if(La.elem[la] < Lb.elem[lb])
            Lc.elem[lc++] = La.elem[la++];
        else
            Lc.elem[lc++] = Lb.elem[lb++];
    }
    while(la < Len_La)
        Lc.elem[lc++] = La.elem[la++];
    while(lb < Len_lb)
        Lc.elem[lc++] = Lb.elem[lb++];
}

//////////////////////////////////////////////////////////////////////////
// 判断表的初始化 赋值
void testInit_Insert_Del()
{
    cout << endl << "开始测试 Init Destroy Insert: 创建销毁" << endl;

    SqList L;
    cout<<"开始创建顺序列表"<<endl;
    if(InitList_Sq(L) == OK)
    {
        cout << "空列表创建成功"<<endl;
        cout << "L.elem: " << L.elem[0] << "; "
            << "L.lenght: " << L.length << "; "
            <<"L.listSize: " << L.listSize << "." <<endl;

        cout << "开始插入数据" << endl;
        for(int i = 0; i < 10; ++i)
        {
            ListInsert_Sq(L, i,i);
        }
        cout << "L.elem: " ;
        for(int i = 0; i <L.length ;++i)
        {
            int j = 0;
            GetElem(L,i, j);
            cout << j << "; " ;
        }
        cout << "L.lenght: " << L.length << "; "
            <<"L.listSize: " << L.listSize << "." <<endl;

        cout<< "查询表中是否存在某个元素:8,并返回下标值:" ;
        cout << LocatElem(L, 8) << endl;
        DestroyList_Sq(L);
    }
}

//////////////////////////////////////////////////////////////////////////
// 合并集合A,B
void  testUnionList()
{
    cout <<endl << "开始测试 UnionList: 求并集" << endl;
    SqList La, Lb;

    InitList_Sq(La);
    La.length = 5;
    for(int i = 0; i < 5; ++i)
        La.elem[i] = i;
    cout<< "La: ";
    for(int i = 0; i < 5; ++i)
        cout << La.elem[i] << ", ";
    cout << "La.length: " << ListLength_Sq(La) <<  endl;

    InitList_Sq(Lb);
    Lb.length = 5;
    for(int i = 0; i < 5; ++i)
        Lb.elem[i] = i + 2;
    cout<< "Lb: ";
    for(int i = 0; i < 5; ++i)
        cout << Lb.elem[i] << ", ";
    cout << "Lb.length: " << ListLength_Sq(Lb) <<  endl;

    // 求并集
    UnionList(La, Lb);
    cout << "完成求并集后" << endl;
    cout<< "La: ";
    for(int i = 0; i < ListLength_Sq(La); ++i)
        cout << La.elem[i] << ", ";
    cout << "La.length: " << ListLength_Sq(La) <<  endl;
}

void testMergeList()
{
    cout << endl <<"开始测试 MergeList :排序合并List" << endl;
    SqList La, Lb;

    InitList_Sq(La);
    La.length = 5;
    for(int i = 0; i < 5; ++i)
        La.elem[i] = i;
    cout<< "La: ";
    for(int i = 0; i < 5; ++i)
        cout << La.elem[i] << ", ";
    cout << "La.length: " << ListLength_Sq(La) <<  endl;

    InitList_Sq(Lb);
    Lb.length = 5;
    for(int i = 0; i < 5; ++i)
        Lb.elem[i] = i + 2;
    cout<< "Lb: ";
    for(int i = 0; i < 5; ++i)
        cout << Lb.elem[i] << ", ";
    cout << "Lb.length: " << ListLength_Sq(Lb) <<  endl;

    SqList Lc;
    Lc.length = ListLength_Sq(La) + ListLength_Sq(Lb);
    Lc.listSize = Lc.length;
    Lc.elem = new ElemType [Lc.listSize * sizeof(ElemType)];
        
    MergeList(La, Lb, Lc);
    cout << "完成MergeList后" << endl;
    cout<< "Lc: ";
    for(int i = 0; i < ListLength_Sq(Lc); ++i)
        cout << Lc.elem[i] << ", ";
    cout << "Lc.length: " << ListLength_Sq(Lc) <<  endl;
}

void main()
{
    //////////////////////////////////////////////////////////////////////////
    // 判断表的初始化 赋值
    testInit_Insert_Del();

    //////////////////////////////////////////////////////////////////////////
    // 合并集合A,B
    testUnionList();

    //////////////////////////////////////////////////////////////////////////
    // 合并非降序的表到一个新表
    testMergeList();

    cout << "sucess"<<endl;
    system("pause");
}

猜你喜欢

转载自blog.csdn.net/zishuiyi/article/details/25980539