您的位置:首页 > 编程语言 > C语言/C++

数据接口线性表C++实现

2015-03-07 00:00 447 查看
#include <iostream>
#include <malloc.h>
using namespace std;
const int list_init_size = 100;
const int listincrement = 10;
template<class ElemType>
class list_sq
{
public:
bool InitList();           //构造一个空的线性表
void DestroyList();        //销毁线性表
void ClearList();          //将lst重置为空表
bool ListEmpty();          //若lst为空表,则返回true,否则返回false
int ListLength();          //返回lst中元素的个数
bool GetElem(int pos, ElemType &e); //返回第pos个元素的值
bool PriorElem(ElemType cur_e, ElemType &pre_e);//若cur_e是lst的数据元素,且不是第一个,
//则用pre_e返回它的前驱,否则操作失败
bool NextElem(ElemType cur_e, ElemType &next_e);//若cur_e是lst的数据元素,且不是最后一个,则用next_e返回它的后继
//否则操作失败,next_e无定义
bool ListInsert(int pos, ElemType e);    //在lst中第pos个位置之前插入新的数据元素e,lst长度加一
bool ListDelete(int pos, ElemType &e);   //删除lst的第pos个元素,并用e返回其值,lst的长度减一
private:
class sq
{
public:
sq() : elem(NULL), length(0), listsize(0){}
ElemType *elem;
int length;
int listsize;
};
sq lst;
};
template<class ElemType>
bool list_sq<ElemType>::InitList()
{
lst.elem = new ElemType[list_init_size];
if (!lst.elem)
return false;
lst.length = 0;
lst.listsize = list_init_size;
return true;
}
template<class ElemType>
void list_sq<ElemType>::DestroyList()
{
delete[] lst.elem;
lst.length = 0;
lst.listsize = 0;
lst.elem = NULL;
}
template<class ElemType>
void list_sq<ElemType>::ClearList()
{
lst.length = 0;
}
template<class ElemType>
bool list_sq<ElemType>::ListEmpty()
{
return lst.length == 0;
}
template<class ElemType>
int list_sq<ElemType>::ListLength()
{
return lst.length;
}
template<class ElemType>
bool list_sq<ElemType>::GetElem(int pos, ElemType &e)
{
if(pos < 1 || pos > lst.length)
return false;
e = *(lst.elem + pos - 1);
return true;
}
template<class ElemType>
bool list_sq<ElemType>::PriorElem(ElemType cur_e, ElemType &pre_e)
{
int i = 2;
ElemType *p = lst.elem + 1;
while(i <= lst.length && *p != cur_e)
{
++p;
++i;
}
if(i > lst.length)
return false;
else
pre_e = *--p;
return true;
}
template<class ElemType>
bool list_sq<ElemType>::NextElem(ElemType cur_e, ElemType &next_e)
{
int i = 1;
ElemType *p = lst.elem;
while(i <= lst.length && *p != cur_e)
{
++p;
++i;
}
if(i == lst.length)
return false;
else
next_e = *++p;
return true;
}
template<class ElemType>
bool list_sq<ElemType>::ListInsert(int pos, ElemType e)
{
ElemType *newbase, *p, *q;
if(pos < 1 || pos > lst.length + 1)
return false;
if(lst.length >= lst.listsize)
{
if(!(newbase = (ElemType *)realloc(lst.elem,(lst.listsize+listincrement)*sizeof(ElemType))))
return false;
lst.elem = newbase;
lst.listsize += listincrement;
}
q = lst.elem +pos - 1;
for(p = lst.elem + lst.length - 1; p >= q; --p)
*(p + 1) = *p;
*q = e;
++lst.length;
return true;
}
template<class ElemType>
bool list_sq<ElemType>::ListDelete(int pos, ElemType &e)
{
ElemType *p, *q;
if(pos < 1 || pos > lst.length)
return false;
p = lst.elem + pos - 1;
e = *p;
q = lst.elem + lst.length - 1;
for(++p; p <= q; ++p)
*(p - 1) = *p;
--lst.length;
return true;
}
int main()
{
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: