C++实现数据结构——线性表

线性表共有两种实现方式:顺序实现与链接实现。

线性表的顺序实现是将线性表的数据元素存放在一块连续的空间(即数组)中,用存放位置反映数据元素之间的关系,将第个元素放在数组的下标为i的位置上(从0开始计数),这样在物理位置上相邻的元素在逻辑结构中也是相邻的。

复制代码
#include<iostream>

using namespace std;

template <class elemType>
class list {//线性表抽象类
public:
    virtual void clear() = 0;//清空线性表
    virtual int length() const = 0;// 获取线性表的长度,即元素个数
    virtual void insert(int i,const elemType &x)=0;//在第i个位置插入一个元素x
    virtual void remove(int i) = 0;// 删除第i个元素
    virtual int search(const elemType &x) const = 0;//搜索元素x是否在线性表中出现
    virtual elemType visit(int i) const = 0;// 访问线性表第i个元素
    virtual void traverse() const = 0;// 遍历线性表
    virtual ~list(){};
};

//线性表的顺序实现,即顺序表
template <class elemType>
class seqList :public list<elemType>{
private:
    elemType *data;
    int currentLength;
    int maxSize;
    void doubleSpace();
public:
    seqList(int initSize=10);
    ~seqList(){delete[] data;} // 释放动态数组的空间
    void clear(){currentLength = 0;}
    int length()const { return currentLength;}
    void insert(int i, const elemType &x);
    void remove(int i);
    int search(const elemType &x)const;
    elemType visit(int i)const {return data[i];}
    void traverse()const;
};

//顺序表构造函数的实现
template <class elemType>
seqList<elemType>::seqList(int initSize){
    data = new elemType[initSize];
    maxSize = initSize;
    currentLength = 0;
};

//顺序表查找函数的实现
template<class elemType>
int seqList<elemType>::search(const elemType &x) const {
    int i;
    for(i=0;i<currentLength && data[i] != x;i++) {
        if(i == currentLength) {
            return -1;
        }else {
            return i;
        }
    }
}
//顺序表遍历函数的实现
template <class elemType>
void seqList<elemType>::traverse() const {
    for (int i = 0; i < currentLength; i++) {
        cout << data[i] << " " << endl;
    }
}

//顺序表实现插入元素
//在第i个位置插入元素x
template<class elemType>
void seqList<elemType>::insert(int i,const elemType &x) {
    //如果当前表长已经达到了申请空间的上限,则必须执行扩大数组空间的操作
    if(currentLength ==maxSize) doubleSpace();
    //将第i个元素到最后一个元素全部后移一个位置
    for(int j=currentLength;j>i;j--) {
        data[j]= data[j- 1];
    }
    data[i]= x;
    ++currentLength;
}

//顺序表实现自动扩容函数doubleSpace()
template <class elemType>
void seqList<elemType>::doubleSpace(){
    elemType *tmp = data;
    maxSize*=2;
    data=new elemType[maxSize];// 申请容量翻倍的新空间
    for(int i=0;i<currentLength; ++i) {
        data[i]= tmp[i]; //将数据从旧空间复制到新空间
    }
    delete[]tmp;//回收旧空间
}

//顺序表实现删除元素
//删除第i个位置的元素
template <class elemType>
void seqList<elemType>::remove(int i){
    // 将第i+1个元素到最后一个元素全部前移一个位置
    for(int j=i;j<currentLength-1;j++)data[j]= data[j + 1];
    --currentLength;
}

int main() {
    //实例化类型为int的seqList
    seqList<int> intSeqList(10);
    cout << std::addressof(intSeqList) << endl;
    intSeqList.insert(0,12);

    //实例化类型为string的seqList
    seqList<string> stringSeqList(20);
    cout << std::addressof(stringSeqList) << endl;
    stringSeqList.insert(0,"a");

    cout << intSeqList.length() << endl;
    cout << stringSeqList.length() << endl;
    return 0;

}
相关推荐
hh随便起个名1 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
橘子真甜~1 小时前
C/C++ Linux网络编程15 - 网络层IP协议
linux·网络·c++·网络协议·tcp/ip·计算机网络·网络层
asiwxy3 小时前
OpenGL 材质
c++
xie_pin_an3 小时前
深入浅出 C 语言数据结构:从线性表到二叉树的实战指南
c语言·数据结构·图论
阿华hhh3 小时前
Linux系统编程(标准io)
linux·开发语言·c++
tang&3 小时前
滑动窗口:双指针的优雅舞步,征服连续区间问题的利器
数据结构·算法·哈希算法·滑动窗口
程序喵大人4 小时前
推荐个 C++ 练习平台
开发语言·c++·工具推荐
Nandeska5 小时前
2、数据库的索引与底层数据结构
数据结构·数据库
fpcc5 小时前
跟我学C++中级篇——std::is_invocable的分析应
c++