数据结构的应用

实验 3 的参考源代码
(1)SeqList.h
#ifndef SEQLIST_H /*避免重复引用头文件*/
#define SEQLIST_H
const int MaxSize = 100; /*顺序表的最大容量为 100*/ template <typename DataType> /*定义模板类 SeqList*/ class SeqList{
public:
SeqList(); /*建立空的顺序表*/
SeqList(DataType a[ ], int n); /*建立一个长度为 n 的顺序表*/ ~SeqList(); /*析构函数*/
int Length(); /*返回顺序表的表长*/
DataType Get(int i); /*按位查找*/
int Locate(DataType x); /*按值查找*/
void Insert(int i, DataType x); /*插入操作*/
DataType Delete(int i); /*删除操作*/
void PrintList(); /*遍历操作*/
private:
DataType data[MaxSize]; /*存放数据元素的数组*/ int length; /*顺序表的长度*/
};
#endif
(2) SeqList.cpp
#include "SeqList.h"
/*SeqList.h 中定义的 SeqList 类的详细实现*/ template <typename DataType> SeqList<DataType>::SeqList() {
length = 0; }
template <typename DataType> SeqList<DataType>::SeqList(DataType a[], int n) {
if(n > MaxSize)
throw "参数非法,数组参数的长度超过了顺序表的最大容量";
for(int i = 0; i < n; i++) data[i] = a[i];
/*顺序表的长度为数组参数的长度*/
length = n; }
template <typename DataType> SeqList<DataType>::~SeqList() { }

template <typename DataType> int SeqList<DataType>::Length() {
return length; }
template <typename DataType> DataType SeqList<DataType>::Get(int i) {
if(i < 1 || i > length)
throw "查找位置非法,合法位置应在 1~length 之间";
else
return data[i-1];
}
template <typename DataType>
int SeqList<DataType>::Locate(DataType x) {
for (int i = 0; i < length; i++) if(data[i] == x)
return i+1; /*若 data[i]=x,返回其逻辑位置 i+1*/ return 0; /*查找失败,返回 0*/
}
template <typename DataType>
void SeqList<DataType>::Insert(int i, DataType x) {
if (length >= MaxSize)
throw "顺序表已满,不能再插入元素,上溢";
if (i < 1|| i > length+1)
throw "插入位置不合法,合法的插入位置应为 1~length+1";
for(int j = length; j >= i; j--)
data[j] = data[j-1]; /*元素 data[i-1]...data[length-1]后移,方向从后向前*/
data[i-1] = x; /*新元素位置*/
length++; }
template <typename DataType>
DataType SeqList<DataType>::Delete(int i) {
if(length == 0)
throw "顺序表为空,不能删除元素,下溢";
if(i < 1 || i > length)
throw "删除元素的逻辑位置非法,合法位置应为 1~length";
DataType x = data[i-1]; /*取出位置 i 的元素*/ for(int j = i; j < length; j++)
data[j-1] = data[j]; /*元素 data[i]...data[length-1]前移,方向从前向后*/ length--;
return x;

}
template <typename DataType> void SeqList<DataType>::PrintList() {
for (int i = 0; i < length; i++)
cout<<data[i]<<" "; /*依次输出线性表的元素值*/
cout<<endl; }
(3) SeqListMain.cpp
#include <iostream>
using namespace std; /*使用 std 命名空间,避免命名冲突*/ #include "SeqList.cpp"
int main() {
int r[10] = {4, 2, 6, 8, 12, 10, 14, 16, 19, 18}; SeqList<int> L(r, 10); cout<<"执行插入操作前数据为:"<<endl; L.PrintList(); /*输出所有元素*/
try{
L.Insert(6, 11);
}
catch(char *str) {
cout<<str<<endl; }
cout<<"执行插入操作后数据为:"<<endl;
L.PrintList(); /*输出所有元素*/
cout<<"值为 16 的元素位置为:";
cout<<L.Locate(16)<<endl; /*查找元素 16,并返回在顺序表中的位置*/ cout<<"执行删除第 7 个元素操作,删除前数据为:"<<endl; L.PrintList(); /*输出所有元素*/
try{
L.Delete(7); /*删除第 7 个元素*/
}
catch(char *str) {
cout<<str<<endl; }
cout<<"删除后数据为:"<<endl; L.PrintList(); /*输出所有元素*/ return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值