#include<iostream>
#include<iomanip>
using namespace std;
const int Maxsize = 100;
template<typename T>
class Seqlist
{
public:
Seqlist() { length = 0; }
Seqlist(T a[], int n);
~Seqlist();
int Length();
T Get(int i);
int Locate(T x);
void Insert(int i, T x);
T Delete(int i);
int Empty();
void Printlist();
private:
T data[Maxsize];
int length;
};
template<typename T>
Seqlist<T>::Seqlist(T a[], int n)
{
if (n > Maxsize)throw"参数非法";
for (int i = 0; i < n; i++)
{
data[i] = a[i];
}
length = n;
}
template<typename T>
int Seqlist<T>::Length()
{
return length;
}
template<typename T>
T Seqlist<T>::Get(int i)
{
if (i<1 || i>length)throw"查找位置非法";
else
return data[i - 1];
}
template<typename T>
int Seqlist<T>::Locate(T x)
{
for (int i = 0; i < length; i++)
{
if (data[i] == x)return i - 1;
}
return 0;
}
template<typename T>
void Seqlist<T>::Insert(int i, T x)
{
if (length == Maxsize)throw"上溢";
if (i<1 || i>length+1)throw"插入位置错误";
for (int j = length; j >= i; j--)
{
data[j] = data[j - 1];
}
data[i - 1] = x;
length++;
}
template<typename T>
T Seqlist<T>::Delete(int i)
{
if (length == 0)throw"下溢";
if (i<1 || i>length)throw"删除位置非法";
T x = data[i - 1];
for (int j = i ; j < length ; j++)
{
data[j-1] = data[j];
}
length--;
return x;
}
/*template<typename T>
int Seqlist<T>::Empty()
{
if (length == 0)return 0;
else
return 1;
}*/
template<typename T>
void Seqlist<T>::Printlist()
{
for (int i = 0; i < length; i++)
{
cout << data[i] << "\t";
}
cout << endl;
}
int main()
{
int r[5] = { 1,2,3,4,5 };
int i, x;
Seqlist<int> L{ r,5 };
cout << "当前线性表长度为:";
L.Printlist();
try {
L.Insert(2, 8);
cout << "执行插入操作后数据为:";
L.Printlist();
}
catch (char* str) { cout << str << endl; }
cout << "当前线性表长度为:" << L.Length() << endl;
cout << "请输入查找的元素值:";
cin >> x;
i = L.Locate(x);
if (i == 0)cout << "查找失败" << endl;
else cout << "元素位置为:" << i << endl;
try
{
cout << "请输入查找元素位置:";
cin >> i;
cout << "元素值是" << L.Get(i) << endl;
}
catch (char* str) { cout << str << endl; }
try
{
cout << "请输入要删除的元素序号";
cin >> i;
x = L.Delete(i);
cout << "删除的元素是" << x << endl;
cout << "删除后的数据为" << endl;
L.Printlist();
}
catch (char* str) { cout << str << endl; }
return 0;
}
为什么会生成错误?24行有个警告,但是我代码都是照着书上敲的,是哪里的代码出了问题?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- threenewbee 2020-10-11 12:14关注
~Seqlist()
析构函数定义了,但是没有实现。是不是少了代码。本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥100 支付宝网页转账系统不识别账号
- ¥15 基于单片机的靶位控制系统
- ¥15 AT89C51控制8位八段数码管显示时钟。
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 下图接收小电路,谁知道原理
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
- ¥15 ETLCloud 处理json多层级问题