这个程序弄得我快不行了....c++

来源:百度知道 编辑:UC知道 时间:2024/09/26 16:34:57
// 92901linearlist.cpp : Defines the entry point for the console application.
//
#include <iostream.h>

//#include "xcept.h"
template<class T>
class LinearList
{
public:
LinearList(int MaxListSize = 10);
~LinearList() {delete [] element;}
bool IsEmpty() const {return length == 0;}
int Length() const {return length;}
bool Find(int k, T& x) const;
int Search(const T& x) const;
LinearList<T>& Delete(int k, T& x);
LinearList<T>& insert(int k, const T& x);
private:
int length;
int MaxSize;
T *element;//dongtai

};
//k
/*class NoMem
{
public:
NoMem(){}
};
void my_new_hander()
{
throw NoMem();
}
new_handler Old_Handler_=set_new_handler(my_new_handler);
*/
//k
template <class T>
LinearList<T>::LinearList(int MaxListSize)
{
Ma

你的程序有两个问题
1 你添加和查找所用不统一
你在添加时使用的是 element[k]=x; 也就是默认从0开始计算
而你在查找和删除时都是默认从1开始,也就是element[1]是第一个元素
这样的习惯很不好,建议你就认为element[0]是第一个元素
2 你在插入的函数中的for循环出现了笔误
你写成了for (int i = length-i; i >= k; i--)
而应该是for (int i = length-1; i >= k; i--)
~~~