c语言静态线性表,用c++模板实现数据结构的静态线性表

//——————-Sqlist.h———————–

#ifndef SQ_H

#define SQ_H

template

class Sqlist{

public:

Sqlist();

~Sqlist();

public:

Elemtype *GetE();

int GetLength();

int GetListSize();

private:

Elemtype *elem;

int length;

int listsize;

public:

int InitSlist();//构造一个空的线性表

int DestorySlist();//销毁线性表

int ClearSlist();

bool IsEmpty();

int SlistLength();

int GetElem(int i,Elemtype &e);//找的第i个元素

int SlistInsert(int i,Elemtype e);//在i之前插入元素

int SlistDelete(int i,Elemtype &e);//删除第i个元素

};

#endif

//———————–Common.h———————–

#ifndef COMMON_H

#define COMMON_H

#i nclude

#i nclude

#i nclude

#i nclude

#i nclude

using namespace std;

#define LIST_INIT_SIZE 100

#define LIST_ADD 10

#define OK 1

#define EOR 0

typedef struct {

char *name;

char *number;

char *sex;

}Elemtype;

#endif

//————————-Sqlist.cpp—————————-

#i nclude “Sqlist.h”

template

Sqlist::Sqlist()

{

InitSlist();

}

template

Sqlist::~Sqlist()

{

free(elem);

}

template

Elemtype *Sqlist::GetE()

{

return elem;

}

template

int Sqlist::GetLength()

{

return length;

}

template

int Sqlist::GetListSize()

{

return listsize;

}

template

int Sqlist::DestorySlist()

{

if(elem)

{

free(elem);//free空间后,还要把那些length和size都给初始化了。

elem=NULL;

length=0;

listsize=0;

return OK;

}

return EOR;

}

template

int Sqlist::ClearSlist()

{

if(elem)

{

length=0;//长度设置为0。就是所谓的空了。

return OK;

}

return EOR;

}

template

bool Sqlist::IsEmpty()

{

if(length!=0)

return false;//不是空的

return true;//是空的

}

template

int Sqlist::SlistLength()

{

return length;

}

template

int Sqlist::GetElem(int i,Elemtype &e)

{

if( (i>=1) && (i<=length) )

{

e=elem[i-1];

return OK;

}

return EOR;

}

//下面三个主要程序,不深入Elemtype里面,所以可以看做一个单元处理,让整个程序变成通用的静态

template

int Sqlist::InitSlist()

{

elem=(Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));

//Elemtype *elem=new Elemtype(LIST_INIT_SIZE*sizeof(Elemtype));

if(!elem)

exit(EOR);

length=0;

listsize=LIST_INIT_SIZE;

return OK;

}

template

int Sqlist::SlistInsert(int i,Elemtype e)

{

Elemtype *q,*p;

if( (i<1) || (i>length+1) )

return EOR;

if(length>=listsize)

{

elem=(Elemtype *)realloc(elem,(listsize+LIST_ADD)*sizeof(Elemtype));

if(!elem) exit(EOR);

listsize=listsize+LIST_ADD;

}

q=&elem[i-1];// 得到第i位置的指针,接着就是后移动元素了

for(p=&(elem[length-1]);p>=q;p–) *(p+1)=*p;

*q=e;

++length;

return OK;

}

template

int Sqlist::SlistDelete(int i,Elemtype &e)

{

Elemtype *q,*p;

if( (i<1) || (i>length) )

return EOR;

q=&elem[i-1];

e=*q;

for(p=q+1;p<=&elem[length-1];p++)*(p-1)=*p;

length–;

return OK;

}

//————main.cpp——————-

#i nclude “Common.h”

#i nclude “Sqlist.h”

#i nclude “Sqlist.cpp”

int main()

{

//Sqlist *stu=new Sqlist;

Sqlist stu;

Elemtype e;

for(int i=1;i<=5;i++)

{

e.name=(char *)malloc(sizeof(char));

e.number=(char *)malloc(sizeof(char));

e.sex=(char *)malloc(sizeof(char));

if((!e.name) || (!e.number) || (!e.sex))

return EOR;

cout<

cin>>e.name;

cout<

cin>>e.number;

cout<

cin>>e.sex;

stu.SlistInsert(i,e);

}

for(i=0;i<5;i++)

{

cout<

cout<

cout<

}

//delete stu;

return OK;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值