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

#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;
}

转载于:https://my.oschina.net/u/196018/blog/383800

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值