//——————-Sqlist.h———————– #ifndef SQ_H template <class Elemtype> #endif //———————–Common.h———————– #ifndef COMMON_H #i nclude <stdio.h> #define LIST_INIT_SIZE 100 #define OK 1 typedef struct { #endif //————————-Sqlist.cpp—————————- #i nclude “Sqlist.h” template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> template <class Elemtype> //————main.cpp——————- #i nclude “Common.h” int main()
#define SQ_H
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个元素
};
#define COMMON_H
#i nclude <malloc.h>
#i nclude <string.h>
#i nclude <stdlib.h>
#i nclude <iostream>
using namespace std;
#define LIST_ADD 10
#define EOR 0
char *name;
char *number;
char *sex;
}Elemtype;
Sqlist<Elemtype>::Sqlist()
{
InitSlist();
}
Sqlist<Elemtype>::~Sqlist()
{
free(elem);
}
Elemtype *Sqlist<Elemtype>::GetE()
{
return elem;
}
int Sqlist<Elemtype>::GetLength()
{
return length;
}
int Sqlist<Elemtype>::GetListSize()
{
return listsize;
}
int Sqlist<Elemtype>::DestorySlist()
{
if(elem)
{
free(elem);//free空间后,还要把那些length和size都给初始化了。
elem=NULL;
length=0;
listsize=0;
return OK;
}
return EOR;
}
int Sqlist<Elemtype>::ClearSlist()
{
if(elem)
{
length=0;//长度设置为0。就是所谓的空了。
return OK;
}
return EOR;
}
bool Sqlist<Elemtype>::IsEmpty()
{
if(length!=0)
return false;//不是空的
return true;//是空的
}
int Sqlist<Elemtype>::SlistLength()
{
return length;
}
int Sqlist<Elemtype>::GetElem(int i,Elemtype &e)
{
if( (i>=1) && (i<=length) )
{
e=elem[i-1];
return OK;
}
return EOR;
}
//下面三个主要程序,不深入Elemtype里面,所以可以看做一个单元处理,让整个程序变成通用的静态
int Sqlist<Elemtype>::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;
}
int Sqlist<Elemtype>::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;
}
int Sqlist<Elemtype>::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;
}
#i nclude “Sqlist.h”
#i nclude “Sqlist.cpp”
{
//Sqlist<Elemtype> *stu=new Sqlist<Elemtype>;
Sqlist<Elemtype> 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<<”Please input the Student Name:”<<endl;
cin>>e.name;
cout<<”Input the number”<<endl;
cin>>e.number;
cout<<”Input your sex”<<endl;
cin>>e.sex;
stu.SlistInsert(i,e);
}
for(i=0;i<5;i++)
{
cout<<”—————————”<<endl;
cout<<stu.GetE()[i].name<<ends<<stu.GetE()[i].number<<ends<<stu.GetE()[i].sex<<endl;
cout<<”—————————”<<endl;
}
//delete stu;
return OK;
}
>> 本文固定链接: http://www.vcgood.com/archives/1379