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;

}
相关推荐
m0_748873551 小时前
C++与Rust交互编程
开发语言·c++·算法
2401_891482178 小时前
多平台UI框架C++开发
开发语言·c++·算法
无敌昊哥战神9 小时前
【LeetCode 257】二叉树的所有路径(回溯法/深度优先遍历)- Python/C/C++详细题解
c语言·c++·python·leetcode·深度优先
㓗冽9 小时前
8皇后·改-进阶题16
数据结构
Darkwanderor9 小时前
三分算法的简单应用
c++·算法·三分法·三分算法
2401_8319207410 小时前
分布式系统安全通信
开发语言·c++·算法
2401_8772742410 小时前
从匿名管道到 Master-Slave 进程池:Linux 进程间通信深度实践
linux·服务器·c++
汉克老师10 小时前
GESP5级C++考试语法知识(八、链表(三)循环链表)
c++·约瑟夫问题·循环链表·gesp5级·gesp五级
月落归舟10 小时前
帮你从算法的角度来认识数组------( 二 )
数据结构·算法·数组
阿贵---11 小时前
C++中的RAII技术深入
开发语言·c++·算法