c语言实现线性表,线性表的顺序实现(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
#include
#include
CSeqList::CSeqList()
{
seq = new SeqList;
}
CSeqList::~CSeqList()
{
delete seq;
seq = NULL;
}
int CSeqList::InitList(void)
{
if (seq == NULL)
{
cout<<"seq == NULL"< return ERROR;
}
seq->elem = (DataType*)malloc(LIST_INIT_SIZE*sizeof(DataType));
if (seq->elem == NULL)
{
cout<<"seq->elem == NULL"< 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"< return;
}
if (seq->elem)
{
free(seq->elem);
seq->elem = NULL;
}
seq->length = 0;
seq->listsize = 0;
cout<<"DestroyList success!"< }
void CSeqList::ClearList()
{
if (seq == NULL)
{
cout<<"seq == NULL"< 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"< return ERROR;
}
if (seq->length == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
int CSeqList::ListLength() const
{
if (seq == NULL)
{
cout<<"seq == NULL"< return ERROR;
}
return seq->length;
}
void CSeqList::GetElem(int i, DataType *e) const
{
if (seq == NULL)
{
cout<<"seq == NULL"< return;
}
if (e == NULL)
{
cout<<"e == NULL"< return;
}
if (i < 1 || i > ListLength())
{
cout<<"i is illegal"< return;
}
if (seq->elem == NULL)
{
cout<<"seq->elem == NULL"< return;
}
*e = seq->elem[i];
}
void CSeqList::PriorElem(int cur_e, DataType *pre_e) const
{
if (seq == NULL)
{
cout<<"seq == NULL"< return;
}
if (cur_e <= 1 || cur_e > ListLength())
{
cout<<"cur_e is illegal"< return;
}
if (seq->elem == NULL)
{
cout<<"seq->elem == NULL"< return;
}
if (pre_e == NULL)
{
cout<<"pre_e == NULL"< return;
}
*pre_e = seq->elem[cur_e - 2];
}
void CSeqList::NextElem(int cur_e, DataType *next_e) const
{
if (seq == NULL)
{
cout<<"seq == NULL"< return;
}
if (cur_e < 1 || cur_e >= ListLength())
{
cout<<"cur_e is illegal"< return;
}
if (seq->elem == NULL)
{
cout<<"seq->elem == NULL"< return;
}
if (next_e == NULL)
{
cout<<"pre_e == NULL"< return;
}
*next_e = seq->elem[cur_e ];
}
int CSeqList::ListInsert(int i, DataType e)
{
if (seq == NULL)
{
cout<<"seq == NULL"< return ERROR;
}
if (i < 1 || i > ListLength() + 1)
{
cout<<"i is illagel"< return ERROR;
}
if (seq->elem == NULL)
{
cout<<"seq->elem == NULL"< 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"< 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"< return ERROR;
}
if (i < 1 || i > ListLength())
{
cout<<"i is illagel"< return ERROR;
}
if (seq->elem == NULL)
{
cout<<"seq->elem == NULL"< 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"< return;
}
cout<<"------- List start -------"< for (int i = 0; i < ListLength(); i++)
{
cout<<"number "<elem[i]< }
cout<<"------- List end -------"< return;
}
/*******测试******/
#include "CSeqList.h"
#include
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 "< cout<<"Delete element is "< return 0;
}
Tags:  数据结构线性表 线性表 线性表的顺序存储 c语言实现线性表

延伸阅读

最新评论

发表评论