来源链接:https://blog.csdn.net/sinat_34474705/article/details/54295579
这两天在准备《软件工程》期末考试,顺带着整理一下今天复习线性表基本操作的代码。
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
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(); //显示表中所有元素
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
程序运行结果: