C/C++语言实现顺序表基本操作(C++为主混合部分C语言)

在考研复习过程中,我发现严书上给出的代码非常不易于理解,并且搜索引擎搜到的一些博客所写的代码质量实在是一言难尽。所以在此更新一些根据我自己的理解写的代码,希望可以帮助到初学者以及正在复习数据结构的人。同时也希望大家可以重视代码的规范性,提高代码的可阅读性。一定要做到见名知意。不要写出类似int a,b,c,d,e,f,g,以及不缩进不使用驼峰命名法的垃圾代码。

#include <iostream>

using namespace std;
#define MAXSIZE 100

bool insertElem(int *SqList, int &length, int position, int elem);//插入
bool deleteElem(int *SqList, int &length, int position, int &elem);//删除
bool createList(int *SqList, int &length);//建表
bool merge(const int *SqListA, int lengthA, const int *SqListB, int lengthB, int *SqListC);//合并
bool reverseSimple(int *SqListA, int length);//简单逆置(逆置给定的长度)
bool reverseComplex(int *SqList, int left, int right, int lengthToReverse);//对指定长度,指定位置的元素进行逆置
bool locateByElem(const int *SqList, int number, int &location);//按元素查找所在位置(数组下标)
bool locateByPosition(const int *SqList, int length, int location, int &number);//按位置查找对应位置的元素
bool moveByNumberP(int *SqList, int p, int listLength); //将顺序表循环左移p位

//划分:考研一共考三种划分策略
void partitionA(int *SqList, int length);//以第一个元素为枢轴和比较元素
void partitionB(int *SqList, int length, int compare);//以第一个元素为枢轴,同时以compare为比较元素(可替代第一个划分函数,只需要把第一元素作为compare)
void partitionC(int *SqList, int length, int randomNumber);//以任意元素为枢轴和比较元素
void showElem(int *SqList, int length); //输出


int main() {
    int SqListA[MAXSIZE];//绝大多数情况下,只用数组即可解决所有顺序表的问题,无需结构体
    int length;

    createList(SqListA, length)

    return 0;
}

bool createList(int *SqList, int &length) {
    cout << "please input the SqList length: ";
    cin >> length;

    if (length > MAXSIZE)
        return false;

    cout << "please input the elem in line: " << endl;
    for (int i = 0; i < length; ++i) {
        cin >> SqList[i];
    }

    return true;
}

bool insertElem(int *SqList, int &length, int position, int elem) {
    if (position < 0 || position > length || length == MAXSIZE)
        return false;

    for (int i = length - 1; i >= position; i--) {
        SqList[i + 1] = SqList[i];
    }

    SqList[position] = elem;
    length++;

    return true;
}

bool deleteElem(int *SqList, int &length, int position, int &elem) {
    if (position < 0 || position > length - 1)
        return false;

    elem = SqList[position];

    for (int i = position - 1; i < length - 1; ++i) {
        SqList[i] = SqList[i + 1];
    }

    length--;
    return true;
}

void showElem(int *SqList, int length) {
    for (int j = 0; j < length; ++j) {
        cout << SqList[j] << "\t";
    }
    cout << endl << "Length:" << length;
}


bool merge(const int *SqListA, int lengthA, const int *SqListB, int lengthB, int *SqListC) {
    if (lengthB + lengthA > 2 * MAXSIZE)
        return false;

    int pointerA = 0;
    int pointerB = 0;
    int pointerC = 0;

    while (pointerA < lengthA && pointerB < lengthB) {
        if (SqListA[pointerA] < SqListB[pointerB]) {
            SqListC[pointerC++] = SqListA[pointerA++];
        } else {
            SqListC[pointerC++] = SqListB[pointerB++];
        }
    }

    while (pointerA < lengthA) {
        SqListC[pointerC++] = SqListA[pointerA++];
    }
    while (pointerB < lengthB) {
        SqListC[pointerC++] = SqListB[pointerB++];
    }

    return true;
}

bool reverseSimple(int *SqListA, int length) {
    if (length < 0)
        return false;

    int temp;

    for (int head = 0, back = length - 1; head < back; ++head, --back) {
        temp = SqListA[head];
        SqListA[head] = SqListA[back];
        SqListA[back] = temp;
    }

    return true;
}

bool reverseComplex(int *SqList, int left, int right, int lengthToReverse) {
    if (left < 0 || right < 0 || lengthToReverse < 0 ||
        lengthToReverse > MAXSIZE || right > MAXSIZE || left > MAXSIZE)
        return false;

    int temp;
    for (int head = left, back = right;
         head < left + lengthToReverse && left < right; ++head, --back) {
        temp = SqList[head];
        SqList[head] = SqList[back];
        SqList[back] = temp;
    }

    return true;
}

bool moveByNumberP(int *SqList, int p, int listLength) {
    if (SqList == nullptr || p < 0 || listLength < 0)
        return false;
    reverseComplex(SqList, 0, p - 1, p);
    reverseComplex(SqList, p, listLength - 1, listLength - p);
    reverseComplex(SqList, 0, listLength - 1, listLength);

    return true;
}

bool locateByElem(const int *SqList, int number, int &location) {
    if (number < 0 || SqList == nullptr)
        return false;

    for (int i = 0; i < number; ++i) {
        if (SqList[i] == number) {
            location = i;
            return true;
        }
    }

    return false;
}

bool locateByPosition(const int *SqList, int length, int location, int &number) {
    if (location < 0 || location > length - 1 || SqList == nullptr)
        return false;

    for (int i = 0; i < location; ++i) {
        if (i == location) {
            number = SqList[i];
        }
    }

    return true;
}

void partitionA(int *SqList, int length) {
    int head = 0;
    int back = length - 1;
    int temp = SqList[0];

    while (head < back) {
        while (head < back && SqList[back] >= temp)//找到比temp小的元素所在位置
            --back;
        if (head < back) {
            SqList[head] = SqList[back];
            head++;
        }
        while (head < back && SqList[head] < temp)
            ++head;
        if (head < back) {
            SqList[back] = SqList[head];
            --back;
        }
    }

    SqList[head] = temp;
}

void partitionB(int *SqList, int length, int compare) {
    int head = 0;
    int back = length - 1;
    int temp = SqList[0];

    while (head < back) {
        while (head < back && SqList[back] >= compare)//找到比compare小的元素所在位置
            --back;
        if (head < back) {
            SqList[head] = SqList[back];
            head++;
        }
        while (head < back && SqList[head] < compare)
            ++head;
        if (head < back) {
            SqList[back] = SqList[head];
            --back;
        }
    }

    SqList[head] = temp;
}

void partitionC(int *SqList, int length, int randomNumber) {
    int head = 0;
    int back = length - 1;
    int temp = SqList[0];
    SqList[0] = SqList[randomNumber];
    SqList[randomNumber] = temp;
    temp = SqList[head];

    while (head < back) {
        while (head < back && SqList[back] >= temp)//找到比temp小的元素所在位置
            --back;
        if (head < back) {
            SqList[head] = SqList[back];
            head++;
        }
        while (head < back && SqList[head] < temp)
            ++head;
        if (head < back) {
            SqList[back] = SqList[head];
            --back;
        }
    }

    SqList[head] = temp;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随风舞落叶殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值