//——————-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;
}
-
近期文章
近期评论
- coolker 发表在《打造最快的Hash表》
- struggle 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- diys 发表在《C语言编程宝典(王大刚) 1.1 C 语言的产生与发展》
文章归档
- 2022 年十月
- 2014 年一月
- 2013 年十二月
- 2012 年十一月
- 2012 年七月
- 2012 年六月
- 2012 年五月
- 2012 年四月
- 2012 年三月
- 2012 年二月
- 2011 年十二月
- 2011 年十月
- 2011 年九月
- 2011 年八月
- 2011 年七月
- 2011 年六月
- 2011 年五月
- 2011 年四月
- 2011 年三月
- 2011 年二月
- 2011 年一月
- 2010 年十二月
- 2010 年十一月
- 2010 年十月
- 2010 年九月
- 2010 年八月
- 2010 年七月
- 2010 年六月
- 2010 年五月
- 2010 年四月
- 2010 年三月
- 2010 年二月
- 2010 年一月
- 2009 年十二月
- 2009 年十一月
- 2009 年十月
- 2009 年九月
- 2009 年八月
- 2009 年七月
- 2009 年六月
- 2009 年五月
- 2009 年四月
- 2009 年三月
- 2009 年二月
- 2009 年一月
- 2008 年十二月
- 2008 年十一月
- 2008 年十月
- 2008 年九月
- 2008 年八月
- 2008 年七月
- 2008 年六月
- 2008 年五月
- 2008 年四月
- 2008 年三月
- 2008 年二月
- 2008 年一月
- 2007 年十二月
- 2007 年十一月
- 2007 年十月
- 2007 年九月
- 2007 年八月
- 2007 年七月
- 2007 年六月
- 2007 年三月
- 2007 年二月
- 2007 年一月
- 2006 年十二月
- 2006 年十一月
- 2006 年十月
- 2006 年九月
- 2006 年八月
- 2006 年七月
- 2006 年六月
- 2006 年五月
- 2006 年四月
- 2006 年三月
- 2006 年二月
- 2006 年一月
- 2005 年十二月
- 2005 年十一月
分类目录
功能