线性表的顺序实现(C++)

线性表的顺序实现,采用C++封装了一个类,欢迎各位感兴趣的同学共同学习讨论。

 

/*******定义******/

#ifndef _CSeqList_ 
#define _CSeqList_

 

#ifdef __cplusplus
extern "C" {
#endif

 

#define OK  (2)
#define ERROR (-1)
#define TRUE (1)
#define FALSE (0)

 

typedef   int DataType;

 

typedef struct  _SeqList
{
DataType *elem;  //存储空间基址
int  length;   //当前长度 
int  listsize;  //当前分配的存储容量

}SeqList;

 

class CSeqList
{
public:
CSeqList();
~CSeqList();
public:
int  InitList(void);
void DestroyList(void);
void ClearList(void);
int  ListEmpty(void) const;
int  ListLength(void) const;
void GetElem(int i, DataType *e) const;
void PriorElem(int cur_e, DataType *pre_e) const;
void NextElem(int cur_e, DataType *next_e) const;
int  ListInsert(int i, DataType e);
int  ListDelete(int i, DataType *e);
void ShowList(void) const;
private:
enum
{
  LISTINCERMENT  = 10,
  LIST_INIT_SIZE = 100
};
SeqList *seq;
};

 

#ifdef __cplusplus
}
#endif

 

#endif

/*******实现******/

#include "CSeqList.h"

#include <stdlib.h>
#include <iostream.h>
#include <string.h>

 

CSeqList::CSeqList()
{
    seq = new SeqList;
}

 

CSeqList::~CSeqList()
{
    delete seq;
    seq = NULL;
}

 

int    CSeqList::InitList(void)
{
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

        return ERROR;
    }

 

    seq->elem = (DataType*)malloc(LIST_INIT_SIZE*sizeof(DataType));
    if (seq->elem == NULL)
    {
        cout<<"seq->elem == NULL"<<endl;

        return ERROR;
    }

 

    memset(seq->elem, 0x00, LIST_INIT_SIZE*sizeof(DataType));
    seq->length = 0;
    seq->listsize = LIST_INIT_SIZE;
    return OK;
}

 

void    CSeqList::DestroyList()
{
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

        return; 

    }
   

    if (seq->elem)
    {
        free(seq->elem);
        seq->elem = NULL;
    }


    seq->length = 0;
    seq->listsize = 0;

    cout<<"DestroyList success!"<<endl;

}

 

void    CSeqList::ClearList()
{
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

        return;
    }


    if (seq->elem)
    {
        memset(seq->elem, 0x00, LIST_INIT_SIZE*sizeof(DataType));
    }


    seq->length = 0;
    seq->listsize = LIST_INIT_SIZE*sizeof(DataType);

}

 

int        CSeqList::ListEmpty() const
{
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

        return ERROR;
    }

 

    if (seq->length == 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }

}

 

int        CSeqList::ListLength() const
{   
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

       return ERROR;
    }

    return seq->length;
}

 

void    CSeqList::GetElem(int i, DataType *e) const
{
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

        return;
    }

 

    if (e == NULL)
    {
        cout<<"e == NULL"<<endl;

        return;
    }

 

    if (i < 1 || i > ListLength())
    {
        cout<<"i is illegal"<<endl;

        return;
    }

 

    if (seq->elem == NULL)
    {
        cout<<"seq->elem == NULL"<<endl;

       return;
    }

    *e = seq->elem[i];
}

 

void    CSeqList::PriorElem(int cur_e, DataType *pre_e) const
{
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

       return;
    }


    if (cur_e <= 1 || cur_e > ListLength())
    {
        cout<<"cur_e is illegal"<<endl;

        return;
    }


    if (seq->elem == NULL)
    {
        cout<<"seq->elem == NULL"<<endl;

       return;
    }


    if (pre_e == NULL)
    {
        cout<<"pre_e == NULL"<<endl;

        return;
    }

    *pre_e = seq->elem[cur_e - 2];

}

 

void    CSeqList::NextElem(int cur_e, DataType *next_e) const
{
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

        return;
    }
    if (cur_e < 1 || cur_e >= ListLength())
    {
        cout<<"cur_e is illegal"<<endl;

        return;
    }
    if (seq->elem == NULL)
    {
        cout<<"seq->elem == NULL"<<endl;

        return;
    }
    if (next_e == NULL)
    {
        cout<<"pre_e == NULL"<<endl;

        return;
    }

    *next_e = seq->elem[cur_e ];

}

 

int        CSeqList::ListInsert(int i, DataType e)
{

    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

        return ERROR;
    }

 

    if (i < 1 || i > ListLength() + 1)
    {
        cout<<"i is illagel"<<endl;

        return ERROR;
    }

 

    if (seq->elem == NULL)
    {
        cout<<"seq->elem == NULL"<<endl;

        return ERROR;
    }

 

    if (seq->length >= seq->listsize)
    {
        DataType* newbase = NULL;

        newbase = (DataType*)realloc(seq->elem, (LIST_INIT_SIZE + LISTINCERMENT)*sizeof(DataType));
        if (newbase == NULL)
        {
            cout<<"newbase == NULL"<<endl;

            return ERROR;
        }
        seq->elem = newbase;
        seq->listsize += LISTINCERMENT;
    }

    DataType *p, *q;

    q = &seq->elem[i - 1];
    p = &seq->elem[seq->length - 1];

    for (; p >= q; --p)
    {
        *(p+1) = *p;
    }

    *q = e;
    ++seq->length;

    return OK;

}

 

int        CSeqList::ListDelete(int i, DataType *e)
{
    if (seq == NULL)
    {
        cout<<"seq == NULL"<<endl;

        return ERROR;
    }
    if (i < 1 || i > ListLength())
    {
        cout<<"i is illagel"<<endl;

        return ERROR;
    }
    if (seq->elem == NULL)
    {
        cout<<"seq->elem == NULL"<<endl;

        return ERROR;
    }   
    DataType *p, *q;
    q = &seq->elem[i - 1];
    *e = *q;
    p = &seq->elem[seq->length - 1];
    for (++q; q <= p; ++q)
    {
        *(q-1) = *q;
    }
    --seq->length;
    return OK;

}

 

void    CSeqList::ShowList(void) const
{
 if (seq == NULL)
 {
  cout<<"seq == NULL"<<endl;
  return;
 }
 cout<<"------- List start -------"<<endl;
 for (int i = 0; i < ListLength(); i++)
 {
  cout<<"number "<<i+1<<" is "<<seq->elem[i]<<endl;
 }
 cout<<"------- List end -------"<<endl;

 return;

}

 

/*******测试******/

#include "CSeqList.h"

#include  <iostream>
using namespace std;

CSeqList seqlist;

int main(int argc, char* argv[])
{
    DataType e;

    seqlist.InitList();
    seqlist.ListInsert(1, 4);
    seqlist.ListInsert(1, 3);
    seqlist.ListInsert(1, 2);
    seqlist.ListInsert(1, 1);
    seqlist.ShowList();
    seqlist.ListDelete(4, &e);
    seqlist.ShowList(); 
    cout<<"List length is "<<seqlist.ListLength()<<endl;
    cout<<"Delete element is "<<e<<endl;

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值