#include "head.h"
#include "iostream.h"
#define LIST_INIT_SIZE 100
template <class ElemType>
class SqList
{
private:
ElemType *elem;
int length;
int listsize;
public:
SqList(int max=LIST_INIT_SIZE);
~SqList(){delete[] elem;}
void ClearList();
Status input(int n);
Status output();
Status ListEmpty();
int ListLength();
ElemType GetElem(int i);
Status NextElem(ElemType i,ElemType &next_e);
Status ListInsert(int i,ElemType e);
Status ListDelete(int i,ElemType &e);
Status inver();// 逆置
//Status MergeList(SqList La,SqList Lb);
};
template <class ElemType> SqList<ElemType>::SqList(int max)
{
listsize=max;
length=0;
elem=new ElemType[listsize];
};
template <class ElemType> void SqList<ElemType>::ClearList()
{
delete[] elem;
elem=new ElemType[listsize];
length=0;
};
template <class ElemType> Status SqList<ElemType>::input(int n)
{
if(n<=0) return ERROR;
for(int i=0;i<n;i++)
{
cout<<"输入第"<<i+1<<"个数:";
cin>>elem[i];
}
length=n;
return OK;
};
template <class ElemType> Status SqList<ElemType>::output()
{
if(length==0) return ERROR;
else
{
for(int i=0;i<length;i++)
{
cout<<elem[i]<<"->";
}
cout<<"NULL"<<endl;
}
return OK;
};
template <class ElemType> Status SqList<ElemType>::ListEmpty()
{
if(length==0) return TRUE;
else return FALSE;
};
template <class ElemType> int SqList<ElemType>::ListLength()
{
return length;
};
template <class ElemType> ElemType SqList<ElemType>::GetElem(int n)
{
ElemType e;
if(length==0) return ERROR;
if(n<1||n>ListLength()) return OVERFLOW;
e=elem[n-1];
return e;
};
template <class ElemType> Status SqList<ElemType>::NextElem(ElemType loc_e,ElemType &next_e)
{
if(length==0) return ERROR;
for(int i=1;loc_e!=elem[i-1];i++);
if(i==length) return OVERFLOW;
next_e=elem[i];
return OK;
};
template <class ElemType> Status SqList<ElemType>::ListInsert(int i,ElemType e)
{
if(i<1||i>length) return ERROR;
if(listsize==length) return OVERFLOW;
for(int n=length-1;n>=i-1;n--)
elem[n+1]=elem[n];
elem[i-1]=e;
length++;
return OK;
};
template <class ElemType> Status SqList<ElemType>::ListDelete(int i,ElemType &e)
{
if(i<1||i>length) return ERROR;
e=elem[i-1];
for(int n=i-1;n<length-1;n++)
elem[n]=elem[n+1];
length--;
return OK;
};
template <class ElemType> Status SqList<ElemType>::inver()
{
int i,m,n;
ElemType temp;
n=length;
m=n/2;
for(i=0;i<m;i++)
{
temp=elem[i];elem[i]=elem[n-i-1];elem[n-i-1]=temp;
}
return OK;
};
posted on 2007-06-07 19:44
星梦情缘 阅读(546)
评论(0) 编辑 收藏 引用 所属分类:
数据结构的所有实现程序