C++数据结构——线性表

//MyCTplate.h
namespace MyCla_Struct
{
    template <class Type>
    class CLN_List
    {
    public:
        CLN_List();
        CLN_List(int max);       //构造函数,初始最大长度为max
        CLN_List(const CLN_List & L);//复制构造函数
        //void DestroyList();   //销毁线性表
        void ClearList();  //清空线性表
        bool ListEmpty();  //判断线性表是否为空 空true,否false
        int Listlength();  //返回数据表中的元素个数
        bool GetElem(int i,Type &e);  //用e返回第i个元素
        bool PriorElem(Type cur_e,Type &pre_e);   //若cur_e是对象的元素且不是第一个元素,则用pre_e返回它的前驱失败返回false
        bool NextElem(Type cur_e,Type &next_e);     //若cur_e是对象的元素且不是最后一个,则用next_e返回它的后继失败返回flase
        bool ListInsert(int i,Type e);   //在i位置插入e,失败返回false
        bool ListDelete(int i,Type &e);//删除i处的元素并且用e返回失败返回false
        CLN_List operator =( CLN_List L);  //重载=
        ~CLN_List();
    protected:
        int Max;   //申请内存空间大小
        int length;//   元素个数
        Type *PL;  //头指针
    };

}
//CSK_Cla_Declare.h
#ifndef LIST
#define LIST
#include<new>
#include"MyCTplate.h"
using namespace MyCla_Struct;
template<class Type>
CLN_List<Type>::CLN_List()
{
    Max = 0;
    length = 0;
    PL = new Type();
}
template<class Type>
CLN_List<Type>::CLN_List(int max)
{
    PL = new Type[max];
    length = 0;
    Max = max;

}
template<class Type>
CLN_List<Type>::CLN_List(const CLN_List &L)
{
    length = L.length;
    Max = L.Max;
    PL = new Type[Max];
    for (int i = 0; i < length; i++)
        L.PL[i] = L.PL[i];
}
template<class Type>
bool CLN_List<Type>::ListEmpty()
{
    if (length == 0)
        return true;
    else
        return false;
}
template<class Type>
void CLN_List<Type>::ClearList()
{

    length = 0;
}
template<class Type>
int CLN_List<Type>::Listlength()
{
    return length;
}
template <class Type>
bool CLN_List<Type>::GetElem(int i, Type &e)
{
    if (i >= 0 && i < length)
    {
        e = PL[i];
        return true;
    }

    else
    {
        return false;
    }

}
template<class Type>
bool CLN_List<Type>::PriorElem(Type cur_e,Type &pri_e)
{
    for (int i = 0; i < length;i++)
    if (i>0&&cur_e == PL[i])
    {
        pri_e = PL[i - 1];
        return true;
    }
    return false;

}
template<class Type>
bool CLN_List<Type>::NextElem(Type cur_e, Type &next_e)
{
    for (int i = 0; i < length; i++)
    if (i < length - 1&& cur_e == PL[i])
    {
        next_e = PL[i+1];
        return true;
    }
    return false;
}
template<class Type>
bool CLN_List<Type>::ListInsert(int i, Type e)
{
    if (i >= 0 && i <=length)
    {

        if (length >= Max)                       //lengh代表数据元素的个数,并且是下一个插入的指针
        {
            /*
            new布局很坑,new布局不能代替realloc,具体为什么 我到现在也没高清,反正如果要用就要,重新申请,释放旧的,再重新指向指针。
            */
            Max += 10;
            Type *t = new Type[Max];  
            for(int i=0;i<length;i++)
                t[i]=PL[i];    
            delete[]PL;
            PL = t;
/*
            Max += 10;
            PL = new (PL)Type[Max];    */         
        }
        int j = length;
        for (; j > i; j--)
            PL[j] = PL[j-1];
        PL[j] = e;
        length++;
        return true;
    }
    return false;
}
template<class Type>
bool CLN_List<Type>::ListDelete(int i, Type &e)
{
    if (i >= 0 && i < length)
    {
        e = PL[i];
        int j;
        for ( j = i; j < length-1; j++)
            PL[j] = PL[j + 1];
        length = j;
        return true;
    }
    return false;


}
template<class Type>
CLN_List<Type> CLN_List<Type>::operator=(CLN_List p)
{
    length = p.length;
    Max = p.Max;
    for (int i = 0; i < length; i++)
        PL[i] = p.PL[i];
}

template<class Type>
CLN_List<Type>::~CLN_List()
{
    delete []PL;
}
#endif

以后会不定期更新,自己没事儿写着玩,免不了有错误!请大家指教!##

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值