[2023 King's Road Data Structure] [Linear Table 01] 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:

>本题目实现起来相对容易,只需要遍历顺序表找到最小元素以及它对应的下标即可
>然后将最后一个位置的元素填补删除位置的元素

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;
}

// 题目功能函数
bool DelMin(SqList &L, int &value)
{
    if (L.length == 0)
    {
        cout << "当前顺序表为空!";
        return false;
    }

    value = L.data[0];
    int pos = 0;

    // 循环遍历寻找最小值及最小值所在下标
    for (int i = 1; i < L.length; i++)
    {
        if (L.data[i] < value)
        {
            value = L.data[i];
            pos = i;
        }
    }

    // 使用最后一个位置填补删除元素
    L.data[pos] = L.data[L.length - 1];
    L.length--;

    return true;
}

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

    int value = 0;
    DelMin(L, value);
    PrintList(L);
    cout << "最小的元素为:" << value;
}

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/124409561