CPP实现一个顺序表

所有可见数据都是从1开始数的,但是我们在方法实现上是从0开始数的

那么规定位置参数从1开始,索引参数从0开始

判断条件要看运算的结果

顺序表

这里是操作一个结构体内部的数组成员来进行增删改查,也就是说在程序中只存在一个结构体变量

思路

整个程序中只操作一个结构体变量,主要的数据在结构体变量的的成员数组里,并且通过成员有效长度来指示可以操作的数据

组成单元

最大长度MaxSize

结构体成员:

  • 数组
  • 有效长度指示

定义如下:

#define MaxSize 50
typedef int ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int effectiveLength;
} SeqList;

顺序表的变量声明是不使用指针的,而是直接定义一个实实在在的结构体

初始化

可以在定义时初始化,直接指示成员的值,这里利用了数组的初始化知识:

SeqList SL = {{0}, 0};

第二种方法是直接声明数组,然后再指定成员有效长度的值.

SeqList SL;
SL.effectiveLength = 0;

在这里不能使用SL.data={0}或者0的方法赋值,因为那相当于操作数组名.

当然,也可以定义专门负责初始化的方法,那样可以显得优雅一些,不过顺序表较为简单,就不折腾了

创建

创建的时候允许插入一些值,使用循环读取的时候,要设置一个退出条件,这个案例中我们使用0

顺序表的最大下表是MaxSize-1,

bool CreateSeqList(SeqList &SL)
{
    bool flag = true;
    int data;
    scanf("%d", &data);
    while (data != 0)
    {
        if (SL.effectiveLength <= MaxSize)
        {
            flag = true;
            SL.data[SL.effectiveLength++] = data;
            scanf("%d", &data);
        }
        else
        {
            flag = false;
        }
    }
    return flag;
}

增加

可以选择向指定索引增加数据,那么后面的数据就要后移

这里传入的是从1开始的位置值,需要在方法内部处理成从0开始的索引值

需要先排除掉一些非法的索引并判断成员数组是否已满

当上一句的条件没有满足时,我们使用位置+1来实现末尾追加数据

符合条件之后,先将指定位置的数据后移,然后插入并更新数据

bool InsertSeqList(SeqList &SL, int position, ElemType data)
{
    /*排除非法索引以及满状态*/
    if (position <= 0 || SL.effectiveLength == MaxSize)
    {
        return false;
    }
    if (position > SL.effectiveLength + 1) // 巧妙地实现可以追加,不可与以上同时判断
    {
        return false;
    }

    /*数据位移*/
    int index = SL.effectiveLength;
    while (index >= position)
    {
        SL.data[index] = SL.data[index - 1];
        index -= 1;
    }
    /*数据插入*/
    SL.data[position - 1] = data;
    /*有效长度更新*/
    SL.effectiveLength += 1;

    return true;
}

删除

删除指定位置的数据,之后需要将其他数据往前移动,返回值可以设计为flag

判断位置值是否合法,判断成员数组是否为空

这里的删除直接覆盖掉元素的值即可,操作完成之后,需要将有效长度值-1

bool DeleteSeqList(SeqList &SL, int position)
{
    bool flag = true;
    if (position < 0 || position > SL.effectiveLength || SL.effectiveLength == 0)
    {
        flag = false;
    }
    else
    {
        /*数据位移,可以直接覆盖*/
        int i = position;
        while (SL.effectiveLength > i)
        {
            SL.data[i - 1] = SL.data[i]; // 巧妙的理解:位置作为索引的时候,-1即可获得当前数据
            i += 1;
        }
        SL.effectiveLength -= 1;
    }

    return flag;
}

修改

提供位置,修改数据

判断索引是否合法,之后便可进行操作,较为简单

需要注意的是,position与effectiveLength均是从1开始

bool AlterSeqList(SeqList &SL, int position, ElemType data)
{
    if (position <= 0 || position > SL.effectiveLength)
    {
        return false;
    }
    SL.data[position - 1] = data;

    return true;
}

查找

这里设计两个方法,根据值查找:

bool SeqListExistValue(SeqList SL, ElemType data)
{
    bool flag = false;
    for (int i = 0; i < SL.effectiveLength; i++)
    {
        if (SL.data[i] == data)
        {
            flag = true;
        }
    }
    return flag;
}

返回值可以设计为位置值,如果有需要的话

根据位置查找:

bool GetElemByPosition(SeqList SL, int position, ElemType &data)
{
    /*也需要判断索引的合法性*/
    if (position <= 0 || position > SL.effectiveLength)
    {
        return false;
    }
    data = SL.data[position - 1];
    return true;
}

打印

打印逻辑比较简答,遍历输出即可

void PrintSeqList(SeqList SL)
{
    int i = 0;
    while (i < SL.effectiveLength)
    {
        printf("%d\t", SL.data[i]);
        i += 1;
    }
    putwchar('\n');
}

代码

#include <stdio.h>

/*定义字段*/
#define MaxSize 10
typedef int ElemType;
/*定义结构体*/
typedef struct
{
    ElemType data[MaxSize];
    int effectiveLength;
} SeqList;

bool CreateSeqList(SeqList &SL)
{
    bool flag = true;
    int data;
    scanf("%d", &data);
    while (data != 0)
    {
        if (SL.effectiveLength <= MaxSize)
        {
            flag = true;
            SL.data[SL.effectiveLength++] = data;
            scanf("%d", &data);
        }
        else
        {
            flag = false;
        }
    }
    return flag;
}
bool InsertSeqList(SeqList &SL, int position, ElemType data)
{
    /*排除非法索引以及满状态*/
    if (position <= 0 || SL.effectiveLength == MaxSize)
    {
        return false;
    }
    if (position > SL.effectiveLength + 1) // 巧妙地实现可以追加,不可与以上同时判断
    {
        return false;
    }

    /*数据位移*/
    int index = SL.effectiveLength;
    while (index >= position)
    {
        SL.data[index] = SL.data[index - 1];
        index -= 1;
    }
    /*数据插入*/
    SL.data[position - 1] = data;
    /*有效长度更新*/
    SL.effectiveLength += 1;

    return true;
}
bool DeleteSeqList(SeqList &SL, int position)
{
    bool flag = true;
    if (position < 0 || position > SL.effectiveLength || SL.effectiveLength == 0)
    {
        flag = false;
    }
    else
    {
        /*数据位移,可以直接覆盖*/
        int i = position;
        while (SL.effectiveLength > i)
        {
            SL.data[i - 1] = SL.data[i]; // 巧妙的理解:位置作为索引的时候,-1即可获得当前数据
            i += 1;
        }
        SL.effectiveLength -= 1;
    }

    return flag;
}
bool AlterSeqList(SeqList &SL, int position, ElemType data)
{
    if (position <= 0 || position > SL.effectiveLength)
    {
        return false;
    }
    SL.data[position - 1] = data;

    return true;
}
bool SeqListExistValue(SeqList SL, ElemType data)
{
    bool flag = false;
    for (int i = 0; i < SL.effectiveLength; i++)
    {
        if (SL.data[i] == data)
        {
            flag = true;
        }
    }
    return flag;
}
bool GetElemByPosition(SeqList SL, int position, ElemType &data)
{
    /*也需要判断索引的合法性*/
    if (position <= 0 || position > SL.effectiveLength)
    {
        return false;
    }
    data = SL.data[position - 1];
    return true;
}
void PrintSeqList(SeqList SL)
{
    int i = 0;
    while (i < SL.effectiveLength)
    {
        printf("%d\t", SL.data[i]);
        i += 1;
    }
    putwchar('\n');
}
int main()
{
    SeqList SL = {{0}, 0};
    // SeqList SL;
    // SL.effectiveLength = 0;
    bool flag;
    CreateSeqList(SL);
    PrintSeqList(SL);

    int po = 3;
    ElemType da = 999;
    flag = InsertSeqList(SL, po, da);
    if (flag)
    {
        printf("insert into position %d value %d success\n", po, da);
        PrintSeqList(SL);
    }
    else
    {
        printf("insert error\n");
    }

    int pos = 2;
    flag = DeleteSeqList(SL, pos);
    if (flag)
    {
        printf("delete by position %d success\n", pos);
        PrintSeqList(SL);
    }
    else
    {
        printf("delete failed\n");
    }

    int p = 2;
    ElemType l = 2;
    flag = AlterSeqList(SL, p, l);
    if (flag)
    {
        printf("the value of position %d  were alter to %d success\n", p, l);
        PrintSeqList(SL);
    }
    else
    {
        printf("alter failed with position %d value %d\n", p, l);
    }

    ElemType t = 2;
    flag = SeqListExistValue(SL, t);
    if (flag)
    {
        printf("Elem %d is Exist!\n", t);
        PrintSeqList(SL);
    }
    else
    {
        printf("Elem %d not exist\n", t);
    }

    ElemType data;
    int position = 3;
    flag = GetElemByPosition(SL, position, data);
    if (flag)
    {
        printf("GetElemByPosition success and data is %d in position %d\n", data, position);
        PrintSeqList(SL);
    }
    else
    {
        printf("GetElemByPosition failed with position %d\n", position);
    }
}

使用C++创建长度为n的顺序表,可通过数组或`std::vector`来实现。 ### 方法一:使用数组 ```cpp #include <iostream> // 假设元素类型为int const int MAX_SIZE = 100; template <typename ElemType> class SeqList { private: ElemType data[MAX_SIZE]; int length; public: SeqList(int n) { length = n; // 可以根据需要初始化元素 for (int i = 0; i < n; ++i) { data[i] = 0; // 初始化为0 } } // 可以添加其他操作方法,如获取元素、插入元素等 ElemType getElement(int index) { if (index >= 0 && index < length) { return data[index]; } std::cerr << "Index out of range." << std::endl; return ElemType(); } }; int main() { int n = 10; SeqList<int> list(n); std::cout << "The first element is: " << list.getElement(0) << std::endl; return 0; } ``` 此方法使用数组存储顺序表元素,在类的构造函数中指定顺序表长度`n`,并对元素进行初始化。 ### 方法二:使用`std::vector` ```cpp #include <iostream> #include <vector> template <typename ElemType> class SeqList { private: std::vector<ElemType> data; public: SeqList(int n) { data.resize(n); // 可以根据需要初始化元素 for (int i = 0; i < n; ++i) { data[i] = 0; // 初始化为0 } } // 可以添加其他操作方法,如获取元素、插入元素等 ElemType getElement(int index) { if (index >= 0 && index < data.size()) { return data[index]; } std::cerr << "Index out of range." << std::endl; return ElemType(); } }; int main() { int n = 10; SeqList<int> list(n); std::cout << "The first element is: " << list.getElement(0) << std::endl; return 0; } ``` 此方法使用`std::vector`存储顺序表元素,在类的构造函数中使用`resize`方法指定顺序表长度`n`,并对元素进行初始化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值