//《C++编程——数据结构与程序设计方法》15.8.3
#include
#include
#include "arrayListType.h"
using namespace std;
template
arrayListType::arrayListType(int size)//(int size=100)是错的??
{
if(size<0)
{
cout<
maxSize=100;
}
else
maxSize=size;
length=0;
list=new elemType[maxSize];
}
template
arrayListType::~arrayListType()
{
delete [] list;
}
template
void arrayListType::print() const
{
int i;
for(i=0;i
cout<
cout<
}
template
void arrayListType::insertAt(int location,const elemType& insertItem)
{
int i;
if(location<0||location>=maxSize)
cout<
else
if(length>=maxSize)
cout<
else
{
for(i=length;i>location;i--)
list[i]=list[i-1];
list[location]=insertItem;
length++;
}
}
template
void arrayListType::removeAt(int location)
{
int i;
if(location<0||location>=maxSize)
cout<
else
{
for(i=location;i
list[i]=list[i+1];
length--;
}
}
template
int arrayListType::seqSearch(const elemType& item)
{
int loc;
bool found=false;
for(loc=0;loc
if(list[loc]==item)
{
found=true;
break;
}
if(found)
return loc;
else
return -1;
}
template
void arrayListType::remove(const elemType& removeItem)
{
int loc;
if(length==0)
cout<
else
{
loc=seqSearch(removeItem);
if(loc!=-1)
removeAt(loc);
else
cout<
}
}
template
const arrayListType& arrayListType::operator =(const arrayListType& otherList)
{
if(this!=&otherList)
{
if(maxSize!=otherList.maxSize)
cout<
else
{
int j;
maxSize=otherList.maxSize;
length=otherList.length;
list=new elemType[maxSize];
if(length!=0)
for(j=0;j
list[j]=otherList.list[j];
}
return *this;
}
}
//头文件arrayListType.h,据说类模板的实现部分只能做到.h 文件中
#ifndef _ARRAYLISTTYPE_H
#define _ARRAYLISTTYPE_H
template
class arrayListType
{
public:
const arrayListType& operator=(const arrayListType&);
// bool isEmpty();
// bool isFull();
// int ListSize();
// int ListSize();
// int maxListSize();
void print() const;
// bool isItemAtEqual(int location,const elemType& item);
void insertAt(int location,const elemType& insertItem);
// void insertEnd(const elemType& insertItem);
void removeAt(int location);
// void retrieveAt(int location,elemType& retItem);
// void replaceAt(int location,const elemType& repItem):
// void clearList();
int seqSearch(const elemType& item);
// void insert(const elemType& insertItem);
void remove(const elemType& removeItem);
arrayListType(int size=100);
// arrayListType(const arrayListType& otherList);
~arrayListType();
protected:
elemType *list;
int length;
int maxSize;
};
template
arrayListType::arrayListType(int size)//(int size=100)是错的??
{
if(size<0)
{
cout<
maxSize=100;
}
else
maxSize=size;
length=0;
list=new elemType[maxSize];
}
template
arrayListType::~arrayListType()
{
delete [] list;
}
template
void arrayListType::print() const
{
int i;
for(i=0;i
cout<
cout<
}
template
void arrayListType::insertAt(int location,const elemType& insertItem)
{
int i;
if(location<0||location>=maxSize)
cout<
else
if(length>=maxSize)
cout<
else
{
for(i=length;i>location;i--)
list[i]=list[i-1];
list[location]=insertItem;
length++;
}
}
template
void arrayListType::removeAt(int location)
{
int i;
if(location<0||location>=maxSize)
cout<
else
{
for(i=location;i
list[i]=list[i+1];
length--;
}
}
template
int arrayListType::seqSearch(const elemType& item)
{
int loc;
bool found=false;
for(loc=0;loc
if(list[loc]==item)
{
found=true;
break;
}
if(found)
return loc;
else
return -1;
}
template
void arrayListType::remove(const elemType& removeItem)
{
int loc;
if(length==0)
cout<
else
{
loc=seqSearch(removeItem);
if(loc!=-1)
removeAt(loc);
else
cout<
}
}
template
const arrayListType& arrayListType::operator =(const arrayListType& otherList)
{
if(this!=&otherList)
{
if(maxSize!=otherList.maxSize)
cout<
else
{
int j;
maxSize=otherList.maxSize;
length=otherList.length;
list=new elemType[maxSize];
if(length!=0)
for(j=0;j
list[j]=otherList.list[j];
}
return *this;
}
}
#endif