数据结构-线性表-顺序表

81 阅读2分钟

数据结构-线性表-顺序表

/**
 * 顺序表的表示方法 用一个数组 和一个记录数组长度的变量就可以表示
 * 基本操作
 * 1.初始化 创建一个顺序表
 * 2.给空的顺序表添加元素
 * 3.取值
 * 4.查找
 * 5.插入值
 * 6.删除值
 * 顺序表优点:
 * 操作简单 存储密度高 取出第i个元素事件只需要o(1)
 * 顺序表缺点:
 * 需要预估存储空间 过大或者过小会造成空间的浪费或溢出
 * 插入删除需要大量移动元素
 *
 */
#include <iostream>
using namespace std;
int maxSize = 100;
typedef int ElemType;
/**
 * 构造一个自定义数据类型表示顺序表
 */
struct seqList{
    ElemType *arr; //存储数组的首地址
    int length;//顺序表的长度
};
typedef  struct seqList SeqList;

/**
 * 初始化顺序表  动态分配链表
 * @param L
 * @return
 */
bool inItSeqList(SeqList &L)//& 给变量名取个别名
{
    //分配大小为maxSize的连续空间
    L.arr = new ElemType[maxSize];
    if (!L.arr) {
        return false;
    }
    //长度初始化 为0
    L.length = 0;
    return true;
}

/**
 * 创建顺序表  给空的顺序表添加数据
 * @param L
 * @return
 */
bool createSeqList(SeqList &L) {
    int x, i = 0; //x 输入赋值 i最后一个数据的下一个下标
    while (1) {
        if (L.length == maxSize) {
            cout << "顺序表已经存储满了";
            return true;
        }
        cin >> x;
        if (x == -1) {
            cout << "输入结束";
            return true;
        }
        L.arr[i++] = x;
        L.length++;
    }
}
/**
 * 取出一个下标为i元素   h存储取出的值
 * @param L
 * @param i
 * @param h
 * @return
 */
bool getElement(SeqList &L, int i, int &h) {
    //判断 下标i 是否在范围之内
    if (i < 0 || i >= L.length) {
        cout << "i的范围超出" << endl;
        return false;
    }
    h = L.arr[i];
    return true;
}

/**
 * 查询顺序表中是否有该值
 * @param L
 * @param h
 * @return
 */
bool selectValue(SeqList &L, int h) {
    for (int i = 0; i < L.length; i++) {
        if (h == L.arr[i]) {
            return true;
        }
    }
    return false;
}

/**
 * 插入值  i表示在下标i之前插入一个值  h表示插入的值
 * @param L
 * @param i
 * @param h
 * @return
 */
bool insertValue(SeqList &L, int i, int h) {
    //判断下标i是否在范围之内 i==L.length 表示可以在最后一个元素后面插入值
    if (i < 0 || i > L.length) {
        cout << "i超出范围";
        return false;
    }
    //判断顺序表是否满了
    if (L.length == maxSize) {
        return false;
    }
    //下标i处为新值插入的地方  所以下标 L.length-1 ---i 处的元素都要往后移动一位
    for (int j = L.length - 1; j >= i; j--) {
        L.arr[j + 1] = L.arr[j];
    }
    L.arr[i] = h;
    L.length++;
    return true;
}

/**
 * 在顺序表中删除下标为i的元素
 * @return
 */
bool deleteValue(SeqList &L, int i) {
    if (i < 0 || i > L.length - 1) {
        cout << "i超出范围";
        return false;
    }
    for (int j = i + 1; j <= L.length - 1; j++) {
        L.arr[j - 1] = L.arr[j];
    }
    L.length--;
    return true;
}

/**
 * 遍历输出 顺序表
 * @return
 */
void traverse(SeqList &L) {
    for (int i = 0; i < L.length; i++) {
        cout << L.arr[i] << " ";
    }
    cout << endl;
}

int main() {
    SeqList l;
    //创建一个空的顺序表
    if (inItSeqList(l)) {
        cout << "初始化成功" << endl;
    }

    //创建顺序表  给空的顺序表添加数据
    if (createSeqList(l)) {
        cout << "创建顺序表成功" << endl;
    }
    traverse(l);

    //取出下标为2的元素
    int e;
    if (getElement(l, 2, e)) {
        cout << "取出的值为" << e << endl;
    }

    //查找10
    if (selectValue(l, 10)) {
        cout << "找到该值" << endl;
    } else {
        cout << "没找到该值" << endl;
    }

    //插入值
    if (insertValue(l, 1, 10)) {
        cout << "插入成功" << endl;
    } else {
        cout << "插入失败" << endl;
    }
    traverse(l);

    //删除值
    if (deleteValue(l, 1)) {
        cout << "删除成功" << endl;
    } else {
        cout << "删除失败" << endl;
    }
    traverse(l);
    return 0;
}