线性表---数组描述

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值