顺序表代码总结——SqList

在C++编译器下可直接运行

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//顺序表存储结构
//动态存储结构
typedef int ElemType;
typedef struct SqList
{
    ElemType *pList;
    int length;
    int listSize;
}SqList;

#define INIT_SIZE 10
#define INCRE_SIZE 10
#define N 10
//创建一个顺序表
void initial(SqList &L)
{
    L.pList = (ElemType *) malloc(INIT_SIZE * sizeof(ElemType));
    L.length = 0;
    L.listSize = INIT_SIZE;
}
//从顺序表中删除第i个元素
int deleteIthElem(SqList &L,int ith,int &e)
{
    if(ith <= 0 || ith > L.length)
        return 0;
    if(ith < L.length)
    {
        e = L.pList[ith - 1];
        for(int curIndex = ith;curIndex < L.length;curIndex++)
        {
            L.pList[curIndex - 1] = L.pList[curIndex];
        }
    }
    --L.length;
    return 1;
}
//在i个位置插入e
int insert1(ElemType e,SqList &L,int ith)
{

    //判断i是否合法
    if(ith < 1 || ith > L.length)
        return 0;
    //判断插入空间是否充足,不充足,申请新的空间,并将老表中中的元素复制进去
    if(L.length >= L.listSize)
    {
        ElemType *pNew = (ElemType *) malloc(sizeof(ElemType) * (2 * L.listSize));
        for(int index = 0;index < L.length;index++)
        {
            pNew[index] = L.pList[index];
        }
        L.pList = pNew;
        L.listSize = 2 * L.listSize;
    }
    //插入i
    for(int curIndex = L.length - 1;curIndex >= ith - 1;--curIndex)
    {
        L.pList[curIndex + 1] = L.pList[curIndex];
    }
    L.pList[ith - 1] = e;
    ++ L.length;
    return 1;
}
//从顺序表中删除最小元素,空出位置由最后一个元素填补
void delMinElem(SqList &L)
{
    int minIndex = 0;
    for(int index = 1;index < L.length; ++index)
    {
        if(L.pList[index] < L.pList[minIndex])
        {
            minIndex = index;
        }
    }
    L.pList[minIndex] = L.pList[L.length - 1];
    --L.length;
}
void printFunc(SqList L)
{
    for(int i = 0; i < L.length;i++)
    {
        printf("%d ", L.pList[i]);
    }
    printf("\n");
    printf("Length: %d size: %d", L.length,L.listSize);

    printf("\n");
}

void values(SqList &L)
{
    L.length = N;
    for(int i = 0;i < L.length ;i++)
    {
        if(i < 3)
           L.pList[i] = i;
        else if(i < 7)
           L.pList[i] = 4;
        else
           L.pList[i] = i ;



    }
    //printFunc(L);
}

//在五序表中删除值在S到T之间对所有元素,包含st;
//在原有存储空间上一次访问元素并对访问元素进行过滤操作,达delete到对特定一些元素删除对方法成为 过滤法
//在一个表的存储空间中进行操作
//当前访问子序列长度 >= 当前结果子序列长度
int deleteElem(ElemType s,ElemType t,SqList &L)
{
    int curLength = 0;//当前子序列的长度
    if(s > t)
        return 0;
    if(s <= t)
    {
        for(int i = 0;i < L.length;i++)
        {

            if(L.pList[i] < s || L.pList[i] > t)
            {
                L.pList[curLength] = L.pList[i];
                curLength++;
            }
        }
        L.length = curLength;
    }
    return 1;

}

//在非递减顺序表中删除值在[s,t]的所有元素
//非递减:前驱永远小于等于后继
//非递增:前驱永远大于等于后继////不单调相区别!!!!!!!
//在一个表中不知道哪几个位置要删除,不能明确一次性删除哪几个位置,则用过滤法;
//如果能明确删除位置,一次性确定删除哪些位置,则用 偏移法(两边夹逼)
int deleteElem1(ElemType s,ElemType t,SqList &L)
{
    //从前往后找第一个大于等于s且小于等于t的元素位置
    int sIndex = -1;
    for(int i = 0;i < L.length;i++)
    {
        if(L.pList[i] >= s)
        {
            sIndex = i;
            break;
        }
    }
    //从后往前找第一个小于等于t且大于s的元素的位置
    if(sIndex == -1)
        return 0;
    int tIndex = L.length - 1;
    for(int i = L.length - 1;i >= 0;i-- )
    {
        if(L.pList[i] >= s && L.pList[i] <= t)
        {
            tIndex = i + 1;
            break;
        }
    }

    int dela = tIndex - sIndex ;
    for(int i = tIndex;i < L.length;i++)
    {
        L.pList[i - dela] = L.pList[i];
    }
    L.length -= dela;
    return 1;
}


//删除非递减顺序表L 中的重复元素
//当前访问序列 》= 删除后对结果子序列  过滤法
//当前访问元素跟结果子序列末尾元素相同,则为重复元素,将其过滤;不相同则追加至结果子序列
void deleteRepeatElem(SqList &L)
{
    int curLength = 0;
    for(int i = 0;i < L.length;i++)
    {
        if(curLength == 0 || L.pList[i] != L.pList[curLength - 1])//不等于其前驱×××××××××××××!!!!!!!!!!
        {
            L.pList[curLength] = L.pList[i];
            curLength++;
        }
    }
    L.length = curLength;

}

//最小值/最大值法
//依次取两个表中最小者插入到新表,直到其中一个表的元素都被插入到新表中为止,最后将剩余表的元素从小到大顺序依次追加到新表末尾

void combine(SqList &La,SqList &Lb,SqList &Lc)
{
    Lc.pList = (ElemType *) malloc(sizeof(ElemType) * (La.length + Lb.length));
    Lc.listSize = La.length + Lb.length;
    Lc.length = 0;

    int i = 0;//A 表从第一个位置开始取
    int j = Lb.length - 1;//B 表从末尾开始取

    while( i < La.length && j >= 0)
    {
        if(La.pList[i] < Lb.pList[j])
        {
            Lc.pList[Lc.length++] = La.pList[i++];
        }
         else
        {
            Lc.pList[Lc.length++] = Lb.pList[j--];
        }
    }

    while(i < La.length)
    {
        Lc.pList[Lc.length++] = La.pList[i++];
    }
    while(j >= 0)
    {
        Lc.pList[Lc.length++] = Lb.pList[j--];
    }
    free(La.pList);
    free(Lb.pList);
    La.length = 0;
    Lb.length = 0;
    La.listSize = 0;
    Lb.listSize = 0;
}
//A 有m+n个存储空间,A递增表,B递减表,无相同元素,合并两表
//最大值法  最大值存在最后位置
//有序表 多表归并 :最小值/最大值法×××××××××××××××××××××××××××

void combineplus(SqList &La,SqList &Lb)
{
    int i = La.length - 1;//指向A表最后一个位置
    int j = 0;//指向B表第一个位置

    int curLength = 0;//当前结果序列长度
    while(i >= 0 && j < Lb.length)
    {
        if(La.pList[i] > Lb.pList[j])
        {
            La.pList[La.listSize - curLength++ - 1] = La.pList[i--];
           // curLength++;
           // i--;
        }
        else
        {
            La.pList[La.listSize - curLength++ - 1] = Lb.pList[j++];
          //  curLength++;
            //j++;
        }
    }
    while(j < Lb.length )
    {
        La.pList[La.listSize - curLength++ - 1] = Lb.pList[j++];
        //curLength++;
       // j++;
    }
    La.length += Lb.length;
    Lb.length = 0;
}

//表A 前r个元素递增有序,后 n-r个元素递减有序,将表A进行升序排序
//插入排序
//取到的待排序列与当前元素比较,
//如果当前元素小于等于待排元素,打么就将其插入到当前元素后面

void insertSort(int r,SqList &La)
{
    int insertIndex = r;
    while(insertIndex < La.length)//待排序元素索引范围
    {
        int i = insertIndex - 1;//排序好序列最后一个位置

        ElemType e = La.pList[insertIndex];

        while(i >= 0)//依次访问排序好序列中对元素
        {
            if(La.pList[i] <= e)//找到插入位置
            {
                break;
            }
            La.pList[i+1] = La.pList[i];//往后挪动一个位置
            i--;//访问前一个元素
        }
        La.pList[i + 1] = e;
        insertIndex++;//取下一个待排元素
    }

}

//给定两个非空集合A和B,分别用升序表La和Lb存储,
//设计算法求解A交B(共同元素只能在求解过程中出现一次)
//最小值法
//算法思想:指针indexLa、indexLb分别指向La和Lb的第一个元素。如果indexLa
//指向的元素小于indexLb指向的元素,移动indexLa访问下一个元素;反之,则移动indexLb
//访问下一个元素;如果两个指针所指元素相同,则发现共有元素,将其保存后再同时移动两个指针
//重复上述步骤,直到一个表的元素访问完为止。

void intersect(SqList &La,SqList Lb)//存储到A表
{
    int curLength = 0;
    int i = 0;
    int j = 0;
    while(i < La.length && j < Lb.length)//过滤法
    {
        if(La.pList[i] < Lb.pList[j])
        {
            i++;
        }
        if(La.pList[i] > Lb.pList[j])
        {
            j++;
        }
//        if(La.pList[i] == Lb.pList[j])//均为单调递增
//        {
//            La.pList[curLength] = La.pList[i];
//            curLength++;
//            ++i;
//            ++j;
//        }
        if(La.pList[i] == Lb.pList[j])//均为非递减
        {
            if(curLength == 0 || La.pList[curLength - 1] != La.pList[i])//排除相同
            {
                La.pList[curLength] = La.pList[i];
                curLength++;
            }
            i++;
            j++;
        }
    }
    La.length = curLength;
}



//给定两个非空集合A和B,分别用升序表La和Lb存储,
//设计算法求解A-B(共同元素只能在求解过程中出现一次)
//最小值法

void except(SqList La,SqList Lb,SqList &Lc)//当非递减时有bug!!!
{
    int indexLa = 0;
    int indexLb = 0;
    int curLength = 0;

    while(indexLa < La.length && indexLb < Lb.length)
    {
        if(La.pList[indexLa] < Lb.pList[indexLb] )
        {
            if(La.pList[indexLa] == La.pList[indexLa - 1])
            {
                indexLa++;
            }
            else if(La.pList[indexLa] < Lb.pList[indexLb])
            {
                Lc.pList[curLength] = La.pList[indexLa];
                curLength++;
                indexLa++;
            }

        }
        else if(La.pList[indexLa] > Lb.pList[indexLb] )
        {
            if(Lb.pList[indexLb] == Lb.pList[indexLb - 1])
            {
                indexLb++;
            }
            else if(La.pList[indexLa] > Lb.pList[indexLb])
            {
                Lc.pList[curLength] = Lb.pList[indexLb];
                curLength++;
                indexLb++;
            }

        }
        else{

                indexLa++;
                indexLb++;
        }
    }
    while(indexLa < La.length)
    {
        Lc.pList[curLength] = La.pList[indexLa];
        curLength++;
        indexLa++;
    }
    while(indexLb < Lb.length)
    {
        if(Lb.pList[indexLb] == Lb.pList[indexLb - 1])
        {
            indexLb++;
            if(indexLb >= Lb.length)
            {
                Lb.length++;
            }
        }

        Lc.pList[curLength] = Lb.pList[indexLb];
        curLength++;
        indexLb++;


    }

    Lc.length = curLength;//Lc.length超限?
}

//设计算法逆置顺序表L

int reversel(SqList L,int low,int high)//指针传递地址值不需要引用
{
    //low = 0;
    //high = L.length - 1;
    if(high <=0)
    {
        return 0;
    }
    while(low < high)
    {
        ElemType temp = L.pList[low];
        L.pList[low] = L.pList[high];
        L.pList[high] = temp;
        low++;
        high--;
    }
    return 1;
}
//循环左移

void rol(int r,SqList L)
{
    reversel(L,0,L.length - 1);
    reversel(L,0,L.length - 1 - r);
    reversel(L,L.length - r,L.length - 1);

}

int main()
{
//    SqList L;
//    initial(L);
//    insert(1,L,0);
//    insert(2,L,1);
//    insert(3,L,2);
//    values(L);
//    printFunc(L);
//    int ret = insert(100,L,1);
//    if(ret == 1)
//    {
//        //printf("success\n");
//        printFunc(L);
//    }
//
//    delMinElem(L);
//    printFunc(L);
//    int e;
//    int ret1 = deleteIthElem(L,1,e);
//    if(ret1 == 1)
//    {
//        printf("delete: %d\n",e);
//        printFunc(L);
//    }

//    int ret2 = deleteElem(4,6,L);
//    if(ret2 == 1)
//        printFunc(L);
//
//    int ret3 = deleteElem1(3,5,L);
//    if(ret3 == 1)
//    {
//        printFunc(L);
//    }
//    deleteRepeatElem(L);
//    printFunc(L);

    SqList La;
    SqList Lb;
    SqList Lc;
    La.pList = (ElemType *) malloc(sizeof(ElemType) * 10);
    Lb.pList = (ElemType *) malloc(sizeof(ElemType) * 10);
    Lc.pList = (ElemType *) malloc(sizeof(ElemType) * 20);
    La.listSize = 10;
    Lb.listSize = 10;
    Lc.listSize = 20;

    La.length = 10;
    Lb.length = 10;
    Lc.length = 0;
    for(int i = 0;i < 10;i++)
    {
        La.pList[i] = i;
    }
//    for(int i = 0;i < 10;i++)
//    {
//        Lb.pList[i] = i * 2;
//    }

    printFunc(La);
    //printFunc(Lb);
    //SqList Lc;
    //combineplus(La,Lb);
    //insertSort(10,La);
    //intersect(La,Lb);
    //except(La,Lb,Lc);
    rol(4,La);
    printFunc(La);
    //printFunc(Lb);
   // printFunc(Lc);

}

posted @ 2021-11-20 21:23  叶梓渔  阅读(532)  评论(0)    收藏  举报