#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_
#include<algorithm>
#include<iostream>
template<class T>
class arraylist
{
private:
T* element; //存储一元数组的容器
int arrayLength; //一维数组容量
int listSize; //线性表的元素个数
bool CheckIndex(int theIndex); //检查下标是否越界
public:
arraylist(int initcapacity=10);
arraylist(const arraylist<T>& thelist);
~arraylist();
//ADT实现
void add(T theElement); //向尾部添加元素
bool empty() const ; //判断链表是否为空
int size() const; //判断商数组中元素的个数
T& get(int theIndex)const; //根据索引获取元素
int indexof(const T& theElement); //返回数组中第一出现的下标
void erase(int theIndex); //删除操作
void insert(int theIndex,const T& theElement); //插入操作
void output() const; //输出操作
bool equal(const arraylist<T>& theList) const; //判断两个线性表是否相等
int Capacity(); //返回链表中可以放置的元素的个数
void changeLength1D(T* &a,int oldlength,int newlength); //将数组放大一倍
};
template<class T>
arraylist<T>::arraylist(int initcapacity)
{
if(initcapacity<1)
{
initcapacity=10;
}
arrayLength = initcapacity;
element = new T[initcapacity];
listSize=0;
}
/*线性表拷贝构造函数*/
template<class T>
arraylist<T>::arraylist(const arraylist<T>& thelist)
{
arrayLength=thelist.arrayLength;
listSize=thelist.listSize;
element=new T[arrayLength];
for(int i=0;i<arrayLength;i++)
{
element[i] =thelist[i];
}
}
/*检查下标是否越界*/
template<class T>
bool arraylist<T>::CheckIndex(int theIndex)
{
if(theIndex<0 || theIndex >arrayLength)
{
throw "error"
return false;
}
else
{
return true;
}
}
/*线性表中加入一个元素*/
template<class T>
void arraylist<T>::add(T theElement)
{
/*如果线性表容量未满则添加,如果容量满了,将线性表扩大一倍*/
if(listSize < arrayLength)
{
element[listSize]=theElement;
listSize++;
}
else
{
changeLength1D(element,arrayLength,2*arrayLength);
element[listSize]=theElement;
listSize++;
}
}
/*判断线性表是否为空*/
template<class T>
bool arraylist<T>::empty() const
{
if(0==listSize)
{
return true;
}
else
{
return false;
}
}
/*根据索引获取元素值*/
template<class T>
T& arraylist<T>::get(int theIndex) const
{
if(theIndex <0 || theIndex >llistSize)
{
return element[-1];
}
else
{
return element[theIndex];
}
}
/*获取线性表中第元素的下标*/
template<class T>
int arraylist<T>::indexof(const T& theElement)
{
for(int i=0;i<listSize;i++)
{
if(theElemen == element[i])
{
return i;
}
}
return -1;
}
/*删除索引为theIndex元素*/
template<class T>
void arraylist<T>::erase(int theIndex)
{
CheckIndex(theIndex);
copy(element+theIndex+1,element+listSize,element+theIndex);
element[--listSize].~T();
}
/*在线性表中插入一个元素*/
template<class T>
void arraylist<T>::insert(int theIndex,const T& theElemen)
{
if(theIndex<0 || theIndex > listSize)
{
return ;
}
if(listSize == arrayLength)
{
int newLength = 2*arrayLength;
changeLength1D(element,arrayLength,newLength);
}
copy(element+theIndex,element+listSize,element+theIndex+1);
element[theIndex]=theElemen;
listSize++;
}
/*线性表输出操作*/
template<class T>
void arraylist<T>::output() const
{
for(int i=0;i<listSize;i++)
{
std::cout<<element[i]<<" ";
}
std::cout<<std::endl;
}
/*判断两个线性表是否相等*/
template<class T>
bool arraylist<T>::equal(const arraylist<T>& theList) const
{
if(listSize != theList.listSize)
{
return false;
}
if(arrayLength != theList.arrayLength)
{
return false;
}
bool equal = true;
for(int i=0;i<listSize;i++)
{
if(element[i] != theList.element[i])
{
equal = false;
breal;
}
}
return equal;
}
/*将线性表放大一倍*/
template<class T>
void arraylist<T>::changeLength1D(T* &a,int oldlength,int newlength)
{
if(newlength < 0)
{
throw "newlength must be >0";
}
T* temp=new T[newlength];
int number =min(oldlength,newlength);
copy(a,a+number,temp);
delete []a;
a=temp;
}
/*析构函数*/
template<class T>
arraylist<T>::~arraylist()
{
delete []element;
element=0;
}
#endif
线性表---数组描述
最新推荐文章于 2021-01-13 21:11:52 发布