线性表的ADT
List.h
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;
};
顺序表的事先声明 & 没有定义成内嵌的顺序表成员函数的实现
AList.h
#include "stdafx.h"
#include "List.h"
#define defaultListSize 255
template <class Elem>
class AList : public List<Elem>
{
private:
int maxSize;
int listSize;
int fence;
Elem* listArray;
public:
AList(int size = defaultListSize)
{
maxSize = size;
listSize = fence = 0;
listArray = new Elem[maxSize];
}
~AList()
{
delete[] listArray;
}
void clear();
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart();
void setEnd();
void prev();
void next();
int leftLength() const;
int rightLength() const;
bool setPos(int pos);
bool getValue(Elem& it) const;
void print() const;
};
template <class Elem>
void AList<Elem>::clear()
{
delete[] listArray;
listSize = fence = 0;
listArray = new Elem[maxSize];
}
template <class Elem> // insert at front of right partition
bool AList<Elem>::insert(const Elem& item)
{
if(listSize == maxSize) return false;
for(int i = listSize; i > fence; i--)
{
listArray[i] = listArray[i - 1];
}
listArray[fence] = item;
listSize++;
return true;
}
template <class Elem>
bool AList<Elem>::append(const Elem& item) //在表尾插入元素
{
if(listSize == maxSize)
return false;
listArray[listSize++]=item;
return true;
}
template < class Elem>
bool AList<Elem>::remove(Elem& it)
{
if(rightLength() == 0) return false;
it = listArray[fence];
for(int i = fence; i < listSize - 1; i++)
{
listArray[i] = listArray[i + 1];
}
listSize--;
return true;
}
template < class Elem>
void AList<Elem>::setStart()
{
fence = 0;
}
template < class Elem>
void AList<Elem>::setEnd()
{
fence = listSize;
}
template < class Elem>
void AList<Elem>::prev()
{
if(fence != 0) fence--;
}
template < class Elem>
void AList<Elem>::next()
{
if(fence <= listSize) fence++;
}
template < class Elem>
int AList<Elem>::leftLength() const
{
return fence;
}
template < class Elem>
int AList<Elem>::rightLength() const
{
return listSize - fence;
}
template < class Elem>
bool AList<Elem>::setPos(int pos)
{
if((pos >= 0) && (pos <= listSize)) fence = pos;
return (pos >= 0) && (pos <= listSize);
}
template < class Elem>
bool AList<Elem>::getValue(Elem& it) const
{
if(rightLength() == 0) return false;
else
{
it = listArray[fence];
return true;
}
}
///
// 定义模板类,实现部分和声明部分都要放在头文件(*.h)里,不能放在cpp文件里。
template < class Elem>
void AList<Elem>::print() const
{
int temp = 0;
cout << " < ";
while(temp < fence)
{
cout << listArray[temp++] << " ";
}
cout << " | ";
while(temp < listSize)
{
cout << listArray[temp++] << " ";
}
cout << " > \n";
}
注意:一开始,我把 没有定义成内嵌的顺序表成员函数的实现 写在了AList.cpp文件里,一直编译通过不了。事实上,这样是不对的,定义模板类,实现部分和声明部分都要放在头文件(*.h)里,不能放在cpp文件里。
http://blog.csdn.net/ljfxmf/article/details/2510786
主函数 Test.cpp
#include "stdafx.h"
#include <iostream>
#include "AList.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
AList<int> a;
a.append(1);
a.append(10);
a.append(20);
a.append(30);
a.append(60);
cout << "content:" << endl;
a.print();
cout << endl;
return 0;
}