C++实现顺序结构线性表的基本操作

这两天在准备《软件工程》期末考试,顺带着整理一下今天复习线性表基本操作的代码。

ps:本人编程水平一般,有问题还望指出,高手请见谅。

main.cpp

/* 
    内容:建立元素数据类型为CElemType的动态顺序结构线性表,并封装对表的操作函数
    作者:Dreamer_zz
    日期:2017/1/9
*/

#include<iostream>
#include"CList.cpp"
using namespace std;


/* 主函数部分 */
int main()
{
    //定义两个空表(类型不同)
    CList <int> C1;
    CList <float> C2;

    //表的初始信息
    cout << "表的初始信息输出测试:" << endl;
    cout<<"C1和C2的初始容量为:"<<C1.GetLength()<<'\t'<< C2.GetLength()<<endl;
    cout<<"C1和C2的初始长度为:"<<C2.GetSize()<<'\t'<<C2.GetSize()<<endl;
    cout << endl;

    //在表的尾部新增一些元素并显示
    cout << "表元素添加测试:" << endl;
    for (int i = 1; i <= 10; i++)
    {
        C1.AddTail(2 * i);
        C2.AddTail(i*i+0.1);
    }
    cout << "C1所有元素为:\n";
    C1.ShowValues();
    cout << "C2所有元素为:\n";
    C2.ShowValues();
    cout << endl;

    //在表的指定位置插入元素
    cout << "表元素插入测试:"<<endl;
    C1.InsertAt(5, 11);
    C2.InsertAt(7, 22.2);
    cout << "C1所有元素为:\n";
    C1.ShowValues();
    cout << "C2所有元素为:\n";
    C2.ShowValues();
    cout << endl;

    //显示表指定位置的值
    cout << "表元素索引测试:"<<endl;
    cout<<"C1和C2索引5对应的节点的值分别为:"<<C1.GetAt(5)<<'\t'<<C2.GetAt(5)<<endl;
    cout << endl;

    //删除表中指定位置的值
    cout << "表元素删除测试:" << endl;
    C1.RemoveAt(3, 2);
    C2.RemoveAt(4);
    cout << "C1所有元素为:\n";
    C1.ShowValues();
    cout << "C2所有元素为:\n";
    C2.ShowValues();
    cout << endl;

    system("pause");
    return 0;
}

CList.h

/* 建立类模板 */
template<class CElemType>
class CList
{
    //常数定义
public:
    enum
    {
        INIT_SIZE = 20, //线性表初始空间长度(元素个数)
        INCREMENT_SIZE = 10 //新增空间长度(元素个数)
    };

    //成员变量
private:
    CElemType *m_pElem; //线性表顺序结构的基地址
    int m_nSize; //线形表当前长度(小于等于开辟的空间长度)
    int m_nLength; //为线性表分配的存储空间长度 

    //成员函数
public:
    CList(); //构造函数
    ~CList(); //析构函数
    int GetSize(); //获取线性表当前长度
    int GetLength(); //获取线性表分配的存储容量
    CElemType GetAt(int nIndex); //检索元素
    void SetAt(int nIndex, CElemType e); //修改元素
    int InsertAt(int nIndex, CElemType e); //在指定位置插入元素,返回索引号
    int AddTail(CElemType e); //把元素e加到线性表的尾部
    int AddHead(CElemType e); //把元素e加到线性表的头部
    void RemoveAt(int nIndex, int nCounter = 1); //在指定位置删除nCounter个元素
    void ShowValues(); //显示表中所有元素
};

CList.cpp

#include<iostream>
#include"CList.h"
using namespace std;


/* 类模板中成员函数定义 */
//构造函数,用于线性表初始化
template<class CElemType>
CList<CElemType>::CList()
{
    m_nSize = 0;
    m_nLength = INIT_SIZE;
    m_pElem = new CElemType[INIT_SIZE]; //开辟动态存储空间
}

//析构函数,用于删除线性表
template<class CElemType>
CList<CElemType>::~CList()
{
    delete[]m_pElem; //删除动态存储空间
}

//获取线性表当前长度
template<class CElemType>
int CList<CElemType>::GetSize()
{
    return m_nSize;
}

//获取线性表分配的存储容量
template<class CElemType>
int CList<CElemType>::GetLength()
{
    return m_nLength;
}

//检索元素
template<class CElemType>
CElemType CList<CElemType>::GetAt(int nIndex)
{
    return m_pElem[nIndex];
}

//修改元素
template<class CElemType>
void CList<CElemType>::SetAt(int nIndex, CElemType e)
{
    m_pElem[nIndex] = e;
}

//把元素e加到线性表的尾部
template<class CElemType>
int CList<CElemType>::AddTail(CElemType e)
{
    return InsertAt(GetSize(), e);
}

//把元素e加到线性表的头部
template<class CElemType>
int CList<CElemType>::AddHead(CElemType e)
{
    return InsertAt(0, e);
}

//在指定位置(nIndex)插入元素e,返回索引号
template <class CElemType>
int CList<CElemType>::InsertAt(int nIndex, CElemType e)
{
    //索引超出范围
    if (nIndex > GetSize())
    {
        cout << "索引超出范围,未插入任何元素!" << endl;
        return -1;
    }
    //空间已满,此时需要增加空间
    if (GetSize() == GetLength())
    {
        //开辟新空间(增加INCREMENT_SIZE个空间)
        CElemType *p = new CElemType[GetSize() + INCREMENT_SIZE];
        //将原地址索引nIndex前的空间复制给新开辟的空间
        for (int i = 0; i < nIndex; i++)
        {
            p[i] = m_pElem[i];
        }
        //将原地址索引nIndex后的空间复制给新开辟的空间(位置后移)
        for (int i = nIndex; i < GetSize(); i++)
        {
            p[i + 1] = m_pElem[i];
        }
        delete[]m_pElem; //删除原空间
        m_nLength += INCREMENT_SIZE; //更新空间长度
        m_pElem = p; //复制空间(位置经过调整的空间复制给原空间)
        delete[]p; //释放p的空间
    }
    //空间还有剩余,直接插入到相应位置
    else
    {
        //插入点后的元素后移一个位置,插入点前位置不变
        for (int i = GetSize() - 1; i >= nIndex; i--)
        {
            m_pElem[i + 1] = m_pElem[i];
        }
    }
    m_pElem[nIndex] = e; //将待插入的元素放到指定位置(nIndex)
    m_nSize++; //空间长度+1
    return nIndex;
}

//从指定位置删除nCounter个元素
template<class CElemType>
void CList<CElemType>::RemoveAt(int nIndex, int nCounter)
{
    //索引超出范围或者最后一个元素超出范围
    if (nIndex > GetSize() - 1 || nIndex + nCounter >= GetSize())
    {
        cout << "索引超出范围,未删除任何元素!" << endl;
        return;
    }
    //删除所选中的空间(后面的元素覆盖前面的元素位置)
    for (int i = nIndex; i <= nIndex + nCounter; i++)
    {
        m_pElem[i] = m_pElem[i + nCounter];
    }
    m_nSize -= nCounter; //线性表空间改变
    //空间冗余时,删除多元的空间
    if (GetLength() > INIT_SIZE && GetLength() - GetSize() >= INCREMENT_SIZE)
    {
        //计算合适的线性表空间大小
        int N = GetSize() + (GetLength() - GetSize()) % INCREMENT_SIZE;
        CElemType *p = new CElemType[N];
        //将原线性表中元素复制给p并删除原空间,再将复制给线性表
        for (int i = 0; i < GetSize(); i++)
        {
            p[i] = m_pElem[i];
            delete[]m_pElem;
            m_pElem = p;
            m_nLength = N;
        }
        delete[]p; //释放p的空间
    }

}

//显示表中所有元素
template<class CElemType>
void CList<CElemType>::ShowValues()
{
    //表中无元素
    if (GetSize() == 0)
    {
        cout << "表中无元素!" << endl;
    }
    //表中有元素
    else
    {
        for (int i = 0; i < GetSize(); i++)
        {
            cout << m_pElem[i] << ' ';
            if ((i + 1) % 15 == 0 && i != GetSize() - 1) //每10个元素为一行
            {
                cout << endl;
            }
        }
        cout << endl;
    }
}

程序运行结果:


程序运行结果

  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值