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;

}
相关推荐
blasit2 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_3 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星3 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛5 天前
delete又未完全delete
c++
端平入洛5 天前
auto有时不auto
c++
琢磨先生David6 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
哇哈哈20216 天前
信号量和信号
linux·c++
多恩Stone6 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马6 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
qq_454245036 天前
基于组件与行为的树状节点系统
数据结构·c#