C++数据结构——线性表的顺序表笔记

线性表是N个相同数据元素的有限序列,数据元素的个数就是线性表的长度。线性表分成两种存储方式,顺序存储——顺序表,链式存储——链表。

顺序表是用一段地址连续的存储单元依次存储线性表的数据元素,假设顺序表每个元素只占c个存储单元,那么第i个元素的位置=【第一个元素的地址+(i-1)*c】。只讲顺序表其实我感觉就像是数组的感觉。

既然类似于数组,那么也会有一个最大容量(Maxsize)。

先写一个顺序表的类的声明。(SeqList.h)

#ifndef SeqList_H
#define SeqList_H   //防止重复
const int Maxsize = 100; //最大容量的设置
template<class DataType> //模板类DataType的定义
class SeqList{
    public:
    SeqList();
    ~SeqList();
    SeqList(DataType a[],int n); //长度为n的顺序表
    int Length(); //求表长度
    DataType Get(int i); //按位查找
    int Locate(DataType x); //按值查找
    void Insert(int i,DataType x); //在i位置插入值x
    DataType Delete(int i); //删除
    int Empty(); //判空
    void PrintList(); //遍历输出
    private:
    DataType data[Maxsize];
    int length;
};
#endif

再写各个函数的定义,重新写一个SeqList.cpp文件

#include<iostream>
#include"SeqList.h"
using namespace std;
template<typename DataType>
SeqList<DataType>::SeqList(){
    length = 0;   //只需要建立一个空的顺序表即可
}

template<typename DataType>
SeqList<DataType>::SeqList(DataType a[],int n){
    if(n > Maxsize){
        throw "参数非法";
    }
    for(int i=0;i<n;i++){
        data[i] = a[i];
    }
    length = n; //确认顺序表长度
}

template<typename DataType>
SeqList<DataType>::~SeqList(){
       //顺序表是静态存储分配,自动释放所占内存,无需写
}

template<typename DataType>
int SeqList<DataType>::Length(){
    return length;  //求顺序表长度
}

template<typename DataType>
DataType SeqList<DataType>::Get(int i){  //按位查找
    if(i < 0 || i > length){
        throw "按位查找错误";
        return DataType();  
    }
    else{
        return data[i-1]; //顺序表与数组下标不同,数组从0开始,而顺序表从1开始
    }

}

template<typename DataType>
int SeqList<DataType>::Locate(DataType x){
    for(int i=0;i<length;i++){
        if(data[i] == x){
            return i+1; //也是因为顺序表与数组下标的原因
        }
    }
    return 0;
}

template<typename DataType>
void SeqList<DataType>::Insert(int i,DataType x){
    if(length == Maxsize){
        throw "上溢";
    }
    if(i<0 || i>length+1){
        throw "插入位置错误";
    }
    for(int j=length;j>=i;j--){
        data[j] = data[j-1]; //第j个元素存在数组下标为j-1,因为j是顺序表下标的递减
    
    }
    data[i-1] = x;
    length++;
}

template<typename DataType>
void SeqList<DataType>::PrintList(){
    for(int i=0;i<length;i++){
        cout<<data[i]<<" ";
    }
    cout<<endl;
}

template<typename DataType>
void SeqList<DataType>::Delete(int i) {
    if (length == 0) {
        throw "下溢";
    }
    if (i<1 || i>length) {
        throw "删除位置错误";
    }
    for (int j=i; j<length; j++) {
        data[j-1] = data[j];
    }
    length--;
}

再写一个main()函数实现

#include<iostream>
using namespace std;
#include"SeqList.h"
#include"SeqList.cpp"
int main(){
    int a[]={1,2,3,4,5,6};
    SeqList<int> L{a,6};
    cout<<"当前顺序表的数据为:"<<endl;
    L.PrintList();
    L.Insert(2,8);
    cout<<"执行插入操作后数据为:"<<endl;
    L.PrintList();
    cout<<"值为5的元素位置为: ";
    cout<<L.Locate(5)<<endl;
    cout<<"执行删除操作前数据为:"<<endl;
    L.PrintList();
    L.Delete(4);
    cout<<"执行删除操作后数据为:"<<endl;
    L.PrintList();
    return 0;
}

一个小tips:要注意顺序表的下标次序和数组的次序不同,所以在函数的增删改查过程总是会出现有+1或者-1的小操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值