[2023 King's Road Data Structure] [Linear Table 05] Complete implementation of C and C++ (can be run directly)

~~~How can the writing style end in a dull way, and the story does not recognize ordinary at the beginning ✌✌✌

If you need the complete code, you can follow the public account below, and reply "code" in the background to get it. Aguang is looking forward to your visit~

topic:
insert image description here

Problem solving ideas:

>定义一个k变量,用它来记录不在区间内元素的个数
>在遍历期间如果遇到不在区间内的元素
>将其赋值到k位置

Code:

#include <iostream>
using namespace std;
#define Maxsize 50

// 定义顺序表结构
typedef struct
{
    int data[Maxsize];
    int length = 0;
} SqList;

// 插入测试数据
void ListInsert(SqList &L)
{
    int val = 0;
    while (cin >> val)
    {
        L.data[L.length++] = val;

        if (cin.get() == '\n')
        {
            break;
        }
    }
}

// 打印顺序表
void PrintList(SqList L)
{
    for (int i = 0; i < L.length; i++)
    {
        cout << L.data[i] << '\t';
    }
    cout << endl;
}

// 题目功能函数
void DelValue(SqList &L, int s, int t)
{
    // 判断合理性
    if (s >= t || L.length == 0)
    {
        cout << "Error";
        return;
    }

    // 定义k用于记录不在之间的元素个数
    int k = 0;
    for (int i = 0; i < L.length; i++)
    {
        if (!(L.data[i] >= s && L.data[i] <= t)) // 如果发现不在之间,则将其前移
        {
            L.data[k++] = L.data[i];
        }
    }

    L.length = k;
}

int main()
{
    SqList L;      // 创建一个顺序表
    ListInsert(L); // 插入写测试数据
    PrintList(L);

    DelValue(L, 3, 6);
    PrintList(L);
}

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/124419974
Recommended