![]() |
#2
hellovfp2012-03-27 10:47
#include "stdafx.h"
#include<iostream> using namespace std; 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); void Output(ostream& out)const; private: int length; int MaxSize; T *element; }; template<class T> Linearlist<T>::Linearlist(int MaxListSize){ MaxSize=MaxListSize; element=new T[MaxSize]; length=0; } template<class T> bool Linearlist<T>::Find(int k,T &x)const{ if(k<1||k>length)return false; x=element[k-1]; return true; } template<class T> int Linearlist<T>::Search(const T&x)const{ for(int i=0;i<length;i++) if(element[i]==x)return++i; return o; } template<class T> Linearlist<T>& Linearlist<T>::Delete(int k,T& x){ if(Find(k,x)){ for(int i=k;i<length;i++) element[i-1]=element[i]; length--; return *this; } else throw OutOfBounds(); } template<class T> Linearlist<T>& Linearlist<T>::Insert(int k,const T& x){ if(k<0||k>length)throw OutOfBounds(); if(Length==Maxsize)throw NoMem(); for(int i=length-i;i>=k;i--) element[i+1]=element[i]; element[k]=x; length++; return *this; } template<class T> void Linearlist<T>::Output(ostream& out)const{ for(int i=0;i<length;i++) out<<element[i]<<""; } void main(){ Linearlist<int> a; } 自己不小心,Linearlist大小写不分,没有运行,自己调试去。 |
#include "stdafx.h"
#include<iostream.>
using namespace std;
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);
void Output(ostream& out)const;
private:
int length;
int MaxSize;
T *element;
};
template<class T>
Linearlist<T>::Linearlist(int MaxListSize){
MaxSize=MaxListSize;
element=new T[MaxSize];
length=0;
}
template<class T>
bool LinearList<T>::Find(int k,T&x)const{
if(k<1||k>length)return false;
x=element[k-1];
return true;
}
template<class T>
int LinearList<T>::Search(const T&x)const{
for(int i=0;i<length;i++)
if(element[i]==x)return++i;
return o;
}
template<class T>
LinearList<T>& LinearList<T>::Delete(int k,T& x){
if(Find(k,x)){
for(int i=k;i<length;i++)
element[i-1]=element[i];
length--;
return *this;
}
else throw OutOfBounds();
}
template<class T>
LinearList<T>& LinearList<T>::Insert(int k,const T& x){
if(k<0||k>length)throw OutOfBounds();
if(Length==Maxsize)throw NoMem();
for(int i=length-i;i>=k;i--)
element[i+1]=element[i];
element[k]=x;
length++;
return *this;
}
template<class T>
void LinearList<T>::Output(ostream& out)const{
for(int i=0;i<length;i++)
out<<element[i]<<"";
}
void main(){
Linearlist<int> a;
}
在vs上运行老有错误 求大神解决