下面的代码是数据结构里的,是什么含义,用法,可以用个MAIN函数解释吗! 100
template<classElem>classList{public:virtualvoidclear()=0;virtualboolinsert(constElem&...
template <class Elem>
class List
{public:
virtual void clear() = 0;
virtual bool insert(const Elem&) = 0;
virtual bool append(const Elem&) = 0;
virtual bool remove(Elem&) = 0;
virtual void setStart() = 0;
virtual void setEnd() = 0;
virtual void prev() = 0;
virtual void next() = 0;
virtual int leftLength()const = 0;
virtual int rightLength()const = 0;
virtual bool setPos(int pos)= 0;
virtual bool getValue(Elem&)const = 0;
virtual void print() const = 0;
};
template <class Elem> // Array-based list
class AList : public List<Elem> {
private:
int maxSize; // Maximum size of list
int listSize; // Actual elem count
int fence; // Position of fence
Elem* listArray; // Array holding list
public:
AList(int size=DefaultListSize) {
maxSize = size;
listSize = fence = 0;
listArray = new Elem[maxSize];
}
~AList() { delete [] listArray; }
void clear() {
delete [] listArray;
listSize = fence = 0;
listArray = new Elem[maxSize];}
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart() { fence = 0; }
void setEnd() { fence = listSize; }
void prev() { if (fence != 0) fence--; }
void next() { if (fence <= listSize)
fence++; }
int leftLength() const { return fence; }
int rightLength() const
{ return listSize - fence; }
bool setPos(int pos)
{ if ((pos >= 0) && (pos <= listSize))
fence = pos;
return (pos >= 0) && (pos <= listSize);
}
bool getValue(Elem& it) const
{ if (rightLength() == 0) return false;
else { it = listArray[fence];
return true; }
}
void print()const
{ int temp=0;
cout<<"<";
while(temp<fence)cout<<listArray
[temp++]<<" ";
cout<<"|";
while(temp>fence)cout<<listArray
[temp++]<<" ";
cout<<">\n";
}}; 展开
class List
{public:
virtual void clear() = 0;
virtual bool insert(const Elem&) = 0;
virtual bool append(const Elem&) = 0;
virtual bool remove(Elem&) = 0;
virtual void setStart() = 0;
virtual void setEnd() = 0;
virtual void prev() = 0;
virtual void next() = 0;
virtual int leftLength()const = 0;
virtual int rightLength()const = 0;
virtual bool setPos(int pos)= 0;
virtual bool getValue(Elem&)const = 0;
virtual void print() const = 0;
};
template <class Elem> // Array-based list
class AList : public List<Elem> {
private:
int maxSize; // Maximum size of list
int listSize; // Actual elem count
int fence; // Position of fence
Elem* listArray; // Array holding list
public:
AList(int size=DefaultListSize) {
maxSize = size;
listSize = fence = 0;
listArray = new Elem[maxSize];
}
~AList() { delete [] listArray; }
void clear() {
delete [] listArray;
listSize = fence = 0;
listArray = new Elem[maxSize];}
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart() { fence = 0; }
void setEnd() { fence = listSize; }
void prev() { if (fence != 0) fence--; }
void next() { if (fence <= listSize)
fence++; }
int leftLength() const { return fence; }
int rightLength() const
{ return listSize - fence; }
bool setPos(int pos)
{ if ((pos >= 0) && (pos <= listSize))
fence = pos;
return (pos >= 0) && (pos <= listSize);
}
bool getValue(Elem& it) const
{ if (rightLength() == 0) return false;
else { it = listArray[fence];
return true; }
}
void print()const
{ int temp=0;
cout<<"<";
while(temp<fence)cout<<listArray
[temp++]<<" ";
cout<<"|";
while(temp>fence)cout<<listArray
[temp++]<<" ";
cout<<">\n";
}}; 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询