顺序表的基本操作(C++实现)

#pragma once
#include<iostream>
using namespace std;

typedef int SQDataType;
class SeqList
{
private:
    SQDataType* data;//指向开辟的数组空间的指针
    int length;//元素个数
    int capacity;//开辟的空间大小
public:
    void SeqListInit();//初始化
    void SeqListPrint();//打印循序表中数据
    void SeqListPushBack(SQDataType x);//尾插
    void SeqListPushFront(SQDataType x);//头插
    void SeqListPopBack();//尾删
    void SeqListPopFront();//头删
    int GetLength() { return length; }//获取顺序表中元素个数
    int GetCapacity() { return capacity; }//获取顺序表开辟的空间大小
    void SeqListCheckCapacity();//检查顺序表是否满了
    void SeqListCheckEmpty();//检查顺序表是否为空
    void SeqListInsert(int pos, SQDataType x);//任意位置插入
    void SeqListErase(int pos);//任意位置删除
    void SeqListDestroy();//整表删除
    int SeqListFind(SQDataType x);//查找元素并返回所在位置
    void SeqListModity(int pos, SQDataType x);//修改指定位置元素的值
};

#include"SeqList.h"

void SeqList::SeqListInit()//初始化
{
    data = new SQDataType[4];
    length = 0;
    capacity = 4;
}

void SeqList::SeqListPushBack(SQDataType x)//尾插
{
    SeqListCheckCapacity();
    data[length] = x;
    length++;
}

void SeqList::SeqListPushFront(SQDataType x)//头插
{
    SeqListCheckCapacity();
    int end = length - 1;
    while (end >= 0)
    {
        data[end + 1] = data[end];
        --end;
    }
    data[0] = x;
    length++;
}

void SeqList::SeqListPopBack()//尾删
{
    SeqListCheckEmpty();
    length--;
}

void SeqList::SeqListPopFront()//头删
{
    SeqListCheckEmpty();
    int start = 0;
    while (start < length-1)
    {
        data[start] = data[start + 1];
        start++;
    }
    length--;
}

void SeqList::SeqListPrint()//打印
{
    for (int i = 0; i < length; i++)
    {
        cout << data[i] << " ";
    }
    cout << endl;
}

void SeqList::SeqListCheckCapacity()//判断容量
{

    if (length == capacity)
    {
        int* p = data;
        data = new SQDataType[capacity * 2];
        for (int i = 0; i < length; i++)
        {
            data[i] = p[i];
        }
        capacity *= 2;
        delete p;
    }
}

void SeqList::SeqListCheckEmpty()//判断是否为空
{
    if (length == 0)
    {
        cout << "顺序表为空" << endl;
        return;
    }
}

void SeqList::SeqListInsert(int pos, SQDataType x)//任意插入
{
    if (pos < 0 || pos > length)
    {
        cout << "pos输入不合法!" << endl;
        return;
    }
    SeqListCheckCapacity();
    
    int end = length - 1;
    while (end >= pos)
    {
        data[end + 1] = data[end];
        end--;
    }
    data[pos] = x;
    length++;
}

void SeqList::SeqListErase(int pos)//任意删除
{
    if (pos < 0 || pos >= length)
    {
        cout << "pos输入不合法!" << endl;
        return;
    }
    SeqListCheckEmpty();
    int start = pos;
    while (pos < length-1)
    {
        data[pos] = data[pos + 1];
        pos++;
    }
    length--;
}

void SeqList::SeqListDestroy()//整表删除
{
    delete[] data;
    data = NULL;
    length = capacity = 0;
}

int SeqList::SeqListFind(SQDataType x)//查找
{
    for (int i = 0; i < length; i++)
    {
        if (data[i] == x)
        {
            return i;
        }
    }
    return -1;
}

void SeqList::SeqListModity(int pos, SQDataType x)//修改
{
    if (pos < 0 || pos >= length)
    {
        cout << "pos输入不合法!" << endl;
    }
    data[pos] = x;
}

#include"SeqList.h"

void menu()
{
    cout << "*************************************************" << endl;
    cout << "1.尾插数据      2.头插数据" << endl;
    cout << "3.尾删数据      4.头删数据" << endl;
    cout << "5.任意插入      6.任意删除" << endl;
    cout << "7.查找数据      8.修改数据" << endl;
    cout << "9.打印数据      -1.退出" << endl;
    cout << "请输入你操作选项:" << endl;
    cout << "*************************************************" << endl;

}

int main()
{
    SeqList st;
    st.SeqListInit();
    int x = 0;
    int option = 0;
    while (option != -1)
    {
        menu();
        cin >> option;
        switch (option)
        {
        case 1:
            cout << "请输入你要插入的数据,以-1结束" << endl;
            do
            {
                cin >> x;
                if (x != -1)
                {
                    st.SeqListPushBack(x);
                }
            } while (x != -1);
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;
        case 8:
            break;
        case 9:
            st.SeqListPrint();
            break;
        case -1:
            break;
        default:
            break;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值