C++实现顺序表.为什么数据结构书上的代码基本上都不行啊?

JustIvanLong 2008-04-03 09:49:04
//
//
//线线表的抽象基类定义
//
//


template <class T>
class LinearList{
public:
LinearList(); //构造函数
~LinearList(); //析构函数
virtual int size() const=0; //求表最大的容量
virtual int Length() const=0; //求表长度
virtual int Search(T &x) const=0; //在表中搜索给定值x
virtual int Locate(int i) const=0; //在表中定位第i个元素
virtual T* getData(int i) const=0; //取第i个元素
virtual void setData(int i,T& x)=0; //修改第i个元素的值为x
virtual bool Insert(int i,T& x)=0; //在第i个表项后插入x
virtual bool Remove(int i,T& x)=0; //删除第i个表项,通过x返回
virtual bool IsEmpty() const=0; //判断表空
virtual bool IsFull() const=0; //判断表满
virtual void Sort() =0; //排序
virtual void input()=0; //输入表项
virtual void output()=0 ; //输出表项
virtual LinearList<T> operator=(LinearList<T>& L)=0; //直接复制

};


#include<iostream.h>
#include<stdlib.h>
#include"LinearList.H"

const int defaultSize = 100;

template <class T>
class SeqList: public LinearList<T>{
protected:
T *data;
int maxSize;
int last;
void reSize(int newSize);
public:
SeqList(int sz=defaultSize);
SeqList(SeqList <T>&L );
~SeqList(){delete[] data;}
int Size()const{return maxSize;}
int Length()const{return last+1}
int Search(T& x)const;
int Locate(int i)const;
T *getData(int i)const
{return( i > 0 && i <= last+1 )?&data[i-1]:NULL;}
void setData(int i, T& x)
{ if( i > 0 && i <= last+1 ) data[i-1]=x;}
bool Insert(int i,T& x);
bool Remove(int i,T& x);
bool IsEmpty(){return (last==-1)?true:false;}

bool IsFull(){return (last==maxSize-1)?true:false;}

void input();
void output();
SeqList<T>operator=(SeqList<T>& L);
};


#include"SeqList.H"

template<class T>
SeqList<T>::SeqList(int sz)
{
if(sz>0)
{
maxSize = sz; last = -1;
data= new T[maxSize];
if(data==NULL)
{
cerr<<"内存分配出错!"<<endl;exit(1);
}
}
}

template<class T>
SeqList<T>::SeqList(SeqList<T>& L)
{
maxSize = L.Size();
last=L.length-1;
data=new T[maxSize];
if(data == NULL)
{cerr<<"内存分配出错!"<<endl; exit(1);}

for(int i = 1; i<=last+1; i++)
data[i-1] = L.getData(i);
}

template<class T>
void SeqList<T>::reSize(int newSize)
{
if(newSize <= 0)
{cerr<<"无效的数组大小!"<<endl;return;}

if(newSize != maxSize)
{
T* newarray =new T[newSize];
if(newarray == NULL)
{cerr<<"内存分配出错!"<<endl; exit(1);}
int n=last+1;
T *srcptr =data;
T *desptr=newarray;
while(n--) *desptr++=*srcptr++;
delete [] data;
data = newarray; maxSize = newSize;
}
}


template <class T>
int SeqList<T>::search(T& x)const{
for(int i=0 ;i<=last; i++)
if(data[i]==x) return i+1;
return 0;
}

template <class T>
int SeqList<T>::Locate(int i) const
{
if(i>=1&& i<=last+1) return i;
else return 0;
}

template <class T>
int SeqList<T>::Insert(int i, T& x)
{
if(last == maxSize-1) return false;
if(i<0 || i>last+1 ) return false;
for(int j=last ; j>=i ; j--)
data[j+1]=data[j];
data[i]=x;
last++;
return true;
}

template <class T>
int SeqList<T>::Remove(int i, T& x)
{
if(last == -1) return false;
if( i<1 || i>last+1) return false;
x=data[i-1];
for(int j=i ;j<=last ;j++)
data[j-1]=data[j];
last--;
return true;
}

template <class T>
int SeqList<T>::input()
{
cout<<"开始建立顺序表,请输入表中元素个数:";
while(1)
{
cin>>last;
if( last <= maxSize-1 ) break;
cout<<"表元素个数输入有误,范围不能超过"<<maxSize-1<<"!";
}
for(int i = 0; i<=last ;i++)
{
cin>>data[i];
cout<<i+1<<endl;
}
}

template <class T>
int SeqList<T>::output()
{
cout<<"顺序表当前最后位置为:"<<last<<endl;
for(int i=0 ;i<=last ; i++)
cout<<"#"<<i+1<<":"<<data[i]<<endl;
}

template <class T>
SeqList<T> SeqList<T>::operator=(SeqList<T>& L)
{
SeqList<T> *temp=new SeqList<T>(L);//这个函数是我自己实现的
return *temp;
}






















哎,发现数据结构的各种教材上的代码基本上很难调试成功,我看来看去也看不出这代码有何问题了,请各位指点一下....
总是报错,无法实例化类:
顺序表\linearlist.h(26) : error C2259: 'LinearList<T>' : cannot instantiate abstract class due to following members:
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(28) : see reference to class template instantiation 'LinearList<T>' being compiled
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : error C2065: 'T' : undeclared identifier
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(26) : error C2259: 'LinearList<int>' : cannot instantiate abstract class due to following members:
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\seqlist.h(8) : see reference to class template instantiation 'LinearList<int>' being compiled
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : see reference to class template instantiation 'SeqList<int>' being compiled
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : error C2259: 'SeqList<int>' : cannot instantiate abstract class due to following members:
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : warning C4259: 'int __thiscall LinearList<int>::size(void) const' : pure virtual function was not defined
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(13) : see declaration of 'size'
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : warning C4259: 'bool __thiscall LinearList<int>::IsEmpty(void) const' : pure virtual function was not defined
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(21) : see declaration of 'IsEmpty'
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : warning C4259: 'bool __thiscall LinearList<int>::IsFull(void) const' : pure virtual function was not defined
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(22) : see declaration of 'IsFull'
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : warning C4259: 'void __thiscall LinearList<int>::Sort(void)' : pure virtual function was not defined
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(23) : see declaration of 'Sort'
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : warning C4259: 'class LinearList<int> __thiscall LinearList<int>::operator =(class LinearList<int> &)' : pure virtual function was not defined
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(26) : see declaration of '='
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : error C2259: 'SeqList<int>' : cannot instantiate abstract class due to following members:
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : warning C4259: 'int __thiscall LinearList<int>::size(void) const' : pure virtual function was not defined
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(13) : see declaration of 'size'
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : warning C4259: 'bool __thiscall LinearList<int>::IsEmpty(void) const' : pure virtual function was not defined
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\linearlist.h(21) : see declaration of 'IsEmpty'
d:\课程学习资料\专业课程\个人源码\数据结构\线性表\顺序表\testseqlist.cpp(5) : warning C4259: 'bool __thiscall LinearList<int>::IsFull(void) const' : pure virtual function was not defined
...全文
392 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2008-04-04
  • 打赏
  • 举报
回复
楼主啊,你还是先把模板去掉,变成普通类,调通了再说吧。
模板,貌似你现在实力还不足。所以就不要同时承担2个任务吧。
不想低调 2008-04-03
  • 打赏
  • 举报
回复
没看到入口函数
effective_person 2008-04-03
  • 打赏
  • 举报
回复

//基类 是const
virtual bool IsEmpty() const=0; //判断表空
virtual bool IsFull() const=0; //判断表满
//而继承确实 非 const
//随便修改啊!!
bool IsEmpty(){return (last==-1)?true:false;}
bool IsFull(){return (last==maxSize-1)?true:false;}


当然还有些问题。
其实 =可以不用重载的。
JustIvanLong 2008-04-03
  • 打赏
  • 举报
回复
我觉得以上代码并没有什么地方不对,但是就是出错。。。
Inhibitory 2008-04-03
  • 打赏
  • 举报
回复
数据结构书上的内容,主要是看一下他的算法与结构怎么处理,然后再按照自己的思路来写程序,直接用里面的程序会遇到很多问题.
吸收的是书里的精华:算法与思路
而实现形式最好是用自己的习惯来写.
effective_person 2008-04-03
  • 打赏
  • 举报
回复
数据结构只是让你知道数据是怎么样组织的。
用什么结构实现算法会更简洁!
而不是要你照书上打印程序。
我看了很多书,数据结构程序多是不完整的程序。
还是先理解好,再自己一步一步的写程序,这样才能更深刻的理解。
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-04-03 09:49
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下