/**/
/* main.cpp
* 测试链表
*/
#include < stdio.h >
#include < iostream.h >
#include < string .h >
#include < stdlib.h >
#include " element.h "
#include " list.h "
void main( int ac, char * av[]) ... {
List l(sizeof(struct element));
char name[4][MAX_NAME]=...{"bluce","jone","kbb","huffman"};
struct element e[4],tmp;
int n,loc;
cout<<"+++++++++++++++++ 测试函数 isEmpty, length, insert, show +++++++++++++++++"<<endl;
cout<<"任意键开始"<<endl;
getchar();
cout<<"isEmpty 的返回值: "<<l.isEmpty()<<endl;
cout<<"length 的返回值: "<<l.length()<<endl;
for(n=0;n<4;n++)...{
e[n].no=n+1;
strcpy(e[n].name,name[n]);
l.insert(&e[n]);
l.show();
}
l.insert(&e[0]);
l.show();
cout<<"isEmpty 的返回值: "<<l.isEmpty()<<endl;
cout<<"length 的返回值: "<<l.length()<<endl;
cout<<"+++++++ 测试函数 locateElement, getElement, priorElement,nextElement ++++++"<<endl;
cout<<"任意键开始"<<endl;
getchar();
for(n=0;n<4;n++)...{
cout<<"locateElement 的返回值:号码是 "<<n<<" 的元素的位置在 "<<l.locateElement(&e[n])<<endl;
}
for(n=0;n<4;n++)...{
l.getElement(&tmp,n);
cout<<"getElement 的返回值:号码是 "<<n<<" 的元素的信息"<<endl;
showElement(&tmp);
}
for(n=0;n<4;n++)...{
l.priorElement(&e[n],&loc);
cout<<"priorElement 的返回值:号码是"<<n<<"的前一个元素的位置: "<<loc<<endl;
}
for(n=0;n<4;n++)...{
l.nextElement(&e[n],&loc);
cout<<"nextElement 的返回值:号码是"<<n<<"的下一个元素的位置: "<<loc<<endl;
}
cout<<"+++++++++++++++++++ 测试函数 del, clearList +++++++++++++++++++++++"<<endl;
cout<<"任意键开始"<<endl;
getchar();
l.show();
l.del(&e[0]);
l.show();
l.del(&e[3]);
l.show();
l.del(&e[2]);
l.show();
l.clearList();
cout<<"调用clearList 以后:"<<endl;
l.show();
}
* 测试链表
*/
#include < stdio.h >
#include < iostream.h >
#include < string .h >
#include < stdlib.h >
#include " element.h "
#include " list.h "
void main( int ac, char * av[]) ... {
List l(sizeof(struct element));
char name[4][MAX_NAME]=...{"bluce","jone","kbb","huffman"};
struct element e[4],tmp;
int n,loc;
cout<<"+++++++++++++++++ 测试函数 isEmpty, length, insert, show +++++++++++++++++"<<endl;
cout<<"任意键开始"<<endl;
getchar();
cout<<"isEmpty 的返回值: "<<l.isEmpty()<<endl;
cout<<"length 的返回值: "<<l.length()<<endl;
for(n=0;n<4;n++)...{
e[n].no=n+1;
strcpy(e[n].name,name[n]);
l.insert(&e[n]);
l.show();
}
l.insert(&e[0]);
l.show();
cout<<"isEmpty 的返回值: "<<l.isEmpty()<<endl;
cout<<"length 的返回值: "<<l.length()<<endl;
cout<<"+++++++ 测试函数 locateElement, getElement, priorElement,nextElement ++++++"<<endl;
cout<<"任意键开始"<<endl;
getchar();
for(n=0;n<4;n++)...{
cout<<"locateElement 的返回值:号码是 "<<n<<" 的元素的位置在 "<<l.locateElement(&e[n])<<endl;
}
for(n=0;n<4;n++)...{
l.getElement(&tmp,n);
cout<<"getElement 的返回值:号码是 "<<n<<" 的元素的信息"<<endl;
showElement(&tmp);
}
for(n=0;n<4;n++)...{
l.priorElement(&e[n],&loc);
cout<<"priorElement 的返回值:号码是"<<n<<"的前一个元素的位置: "<<loc<<endl;
}
for(n=0;n<4;n++)...{
l.nextElement(&e[n],&loc);
cout<<"nextElement 的返回值:号码是"<<n<<"的下一个元素的位置: "<<loc<<endl;
}
cout<<"+++++++++++++++++++ 测试函数 del, clearList +++++++++++++++++++++++"<<endl;
cout<<"任意键开始"<<endl;
getchar();
l.show();
l.del(&e[0]);
l.show();
l.del(&e[3]);
l.show();
l.del(&e[2]);
l.show();
l.clearList();
cout<<"调用clearList 以后:"<<endl;
l.show();
}
/**/
/* elememt.h
* 定义一个用来测试数据
*/
#define MAX_NAME 20
struct element ... {
int no;
char name[MAX_NAME];
} ;
// 定义如何显示该结构体
void showElement( const void * dt) ... {
struct element *e=(struct element *)dt;
cout<<"NO. "<<e->no<<" NAME: "<<e->name<<endl;
}
// 定义如何判断两个结构体是否相等
int compareElement( const void * d1, const void * d2) ... {
struct element *e1,*e2;
e1=(struct element *)d1;
e2=(struct element *)d2;
return e1->no-e2->no;
}
* 定义一个用来测试数据
*/
#define MAX_NAME 20
struct element ... {
int no;
char name[MAX_NAME];
} ;
// 定义如何显示该结构体
void showElement( const void * dt) ... {
struct element *e=(struct element *)dt;
cout<<"NO. "<<e->no<<" NAME: "<<e->name<<endl;
}
// 定义如何判断两个结构体是否相等
int compareElement( const void * d1, const void * d2) ... {
struct element *e1,*e2;
e1=(struct element *)d1;
e2=(struct element *)d2;
return e1->no-e2->no;
}
/**/
/* list.h
* 定义并实现通用链表类
*/
#define oope(msg) {perror(msg);exit(1);}
// 链表类
class List ... {
private:
/**//* 定义数据结构,head指向表头节点,bottom指向链表最后一个节点所在的位置,以便向链表插入数据 */
struct member...{
void *element;
struct member *next;
}head,*bottom;
/**//* size 是data的大小,len 表示当前链表中元素的个数 */
int size,len;
public:
List(int); //初始化链表
~List(); //释放链表
void show(void); //打印链表
void clearList(void); //清空链表
int isEmpty(void); //判断链表是否为空
int length(void); //返回当前链表中数据元素的个数
int getElement(void *dt,int n); //得到链表中第n个数并复制给dt
int locateElement(const void *dt); //返回链表链表中值等于dt的元素的位置
int priorElement(const void *cur,int *pos); //将链表中值等于dt的元素的前驱位置复制给pos
int nextElement(const void *cur,int *pos); //将链表中值等于dt的元素的后继位置复制给pos
int insert(const void *dt); //向链表中插入值等于dt的元素
int del(const void *dt); //从链表中删除值等于dt的元素
} ;
List::List( int s) ... {
size=s;
len=0;
head.next=NULL;
bottom=&head;
}
void List::clearList() ... {
if(len==0) return;
int n;
struct member *pre,*aft;
pre=head.next;
aft=pre->next;
for(n=0;n<len;n++)...{
free(pre);
pre=aft;
if(aft) aft=aft->next;
}
len=0;
head.next=NULL;
bottom=&head;
}
List:: ~ List() ... {
clearList();
}
int List::locateElement( const void * dt) ... {//如果成功返回dt所在的位置,否则返回-1
int n;
struct member *loc;
loc=head.next;
for(n=0;n<len;n++)...{
if(compareElement(dt,loc->element)==0) return n;
loc=loc->next;
}
return -1;
}
int List::insert( const void * dt) ... {//if success return location where insert else return -1
int pos;
if((pos=locateElement(dt))!=-1)...{
cout<<"can not insert data beacuse it has been exist"<<endl;
return -1;
}
struct member *tmp;
tmp=(struct member *)malloc(sizeof(struct member));
if(tmp==NULL)...{
perror("insert error: ");
return -1;
}
tmp->element=malloc(size);
memcpy(tmp->element,dt,size);
tmp->next=NULL;
if(len==0)...{
bottom=head.next=tmp;
}
else...{
bottom->next=tmp;
bottom=tmp;
}
return ++len;
}
void List::show() ... {
int n;
struct member * tmp;
cout<<"----------------------------------------------------"<<endl;
cout<<"there are "<<len<<" heads in the list"<<endl;
tmp=head.next;
for(n=0;n<len;n++)...{
showElement(tmp->element);
tmp=tmp->next;
}
cout<<"----------------------------------------------------"<<endl;
}
int List::isEmpty() ... {
return len;
}
int List::length() ... {
return len;
}
// 调用成功返回0,否则返回-1
int List::getElement( void * dt, int n) ... {
int loc;
struct member *tmp;
if(n<0 || n>=len) return -1;
tmp=head.next;
for(loc=0;loc<n;loc++)
tmp=tmp->next;
if(memcpy(dt,tmp->element,size)==NULL) return -1;
return 0;
}
// 调用成功返回0,否则返回-1
int List::priorElement( const void * cur, int * pos) ... {
int n;
struct member *tmp=head.next;
for(n=0;n<len;n++)...{
if(compareElement(cur,tmp->element)==0) ...{*pos=n-1;return 0;}
tmp=tmp->next;
}
return -1;
}
// 如果成功返回元素的后继位置,否则返回-1
int List::nextElement( const void * cur, int * pos) ... {
int n;
struct member *tmp=head.next;
for(n=0;n<len;n++,tmp=tmp->next)
if(compareElement(cur,tmp->element)==0) ...{*pos=n+1;return 0;}
return -1;
}
// 删除元素,成功放回0,否则返回-1
int List::del( const void * dt) ... {
if(len==0)...{
cout<<"nothing to delete, list is empty"<<endl;
return -1;
}
struct member *tmp=&head,*t;
int flag=-1;
while(tmp)...{
if(memcmp(tmp->next->element,dt,size)==0)...{
if(bottom==tmp->next) ...{
bottom=tmp;
free(tmp->next);
tmp->next=NULL;
}
else...{
t=tmp->next;
tmp->next=tmp->next->next;
free(t);
}
flag=0;
len--;
break;
}
else...{
tmp=tmp->next;
}
}
return flag;
}
* 定义并实现通用链表类
*/
#define oope(msg) {perror(msg);exit(1);}
// 链表类
class List ... {
private:
/**//* 定义数据结构,head指向表头节点,bottom指向链表最后一个节点所在的位置,以便向链表插入数据 */
struct member...{
void *element;
struct member *next;
}head,*bottom;
/**//* size 是data的大小,len 表示当前链表中元素的个数 */
int size,len;
public:
List(int); //初始化链表
~List(); //释放链表
void show(void); //打印链表
void clearList(void); //清空链表
int isEmpty(void); //判断链表是否为空
int length(void); //返回当前链表中数据元素的个数
int getElement(void *dt,int n); //得到链表中第n个数并复制给dt
int locateElement(const void *dt); //返回链表链表中值等于dt的元素的位置
int priorElement(const void *cur,int *pos); //将链表中值等于dt的元素的前驱位置复制给pos
int nextElement(const void *cur,int *pos); //将链表中值等于dt的元素的后继位置复制给pos
int insert(const void *dt); //向链表中插入值等于dt的元素
int del(const void *dt); //从链表中删除值等于dt的元素
} ;
List::List( int s) ... {
size=s;
len=0;
head.next=NULL;
bottom=&head;
}
void List::clearList() ... {
if(len==0) return;
int n;
struct member *pre,*aft;
pre=head.next;
aft=pre->next;
for(n=0;n<len;n++)...{
free(pre);
pre=aft;
if(aft) aft=aft->next;
}
len=0;
head.next=NULL;
bottom=&head;
}
List:: ~ List() ... {
clearList();
}
int List::locateElement( const void * dt) ... {//如果成功返回dt所在的位置,否则返回-1
int n;
struct member *loc;
loc=head.next;
for(n=0;n<len;n++)...{
if(compareElement(dt,loc->element)==0) return n;
loc=loc->next;
}
return -1;
}
int List::insert( const void * dt) ... {//if success return location where insert else return -1
int pos;
if((pos=locateElement(dt))!=-1)...{
cout<<"can not insert data beacuse it has been exist"<<endl;
return -1;
}
struct member *tmp;
tmp=(struct member *)malloc(sizeof(struct member));
if(tmp==NULL)...{
perror("insert error: ");
return -1;
}
tmp->element=malloc(size);
memcpy(tmp->element,dt,size);
tmp->next=NULL;
if(len==0)...{
bottom=head.next=tmp;
}
else...{
bottom->next=tmp;
bottom=tmp;
}
return ++len;
}
void List::show() ... {
int n;
struct member * tmp;
cout<<"----------------------------------------------------"<<endl;
cout<<"there are "<<len<<" heads in the list"<<endl;
tmp=head.next;
for(n=0;n<len;n++)...{
showElement(tmp->element);
tmp=tmp->next;
}
cout<<"----------------------------------------------------"<<endl;
}
int List::isEmpty() ... {
return len;
}
int List::length() ... {
return len;
}
// 调用成功返回0,否则返回-1
int List::getElement( void * dt, int n) ... {
int loc;
struct member *tmp;
if(n<0 || n>=len) return -1;
tmp=head.next;
for(loc=0;loc<n;loc++)
tmp=tmp->next;
if(memcpy(dt,tmp->element,size)==NULL) return -1;
return 0;
}
// 调用成功返回0,否则返回-1
int List::priorElement( const void * cur, int * pos) ... {
int n;
struct member *tmp=head.next;
for(n=0;n<len;n++)...{
if(compareElement(cur,tmp->element)==0) ...{*pos=n-1;return 0;}
tmp=tmp->next;
}
return -1;
}
// 如果成功返回元素的后继位置,否则返回-1
int List::nextElement( const void * cur, int * pos) ... {
int n;
struct member *tmp=head.next;
for(n=0;n<len;n++,tmp=tmp->next)
if(compareElement(cur,tmp->element)==0) ...{*pos=n+1;return 0;}
return -1;
}
// 删除元素,成功放回0,否则返回-1
int List::del( const void * dt) ... {
if(len==0)...{
cout<<"nothing to delete, list is empty"<<endl;
return -1;
}
struct member *tmp=&head,*t;
int flag=-1;
while(tmp)...{
if(memcmp(tmp->next->element,dt,size)==0)...{
if(bottom==tmp->next) ...{
bottom=tmp;
free(tmp->next);
tmp->next=NULL;
}
else...{
t=tmp->next;
tmp->next=tmp->next->next;
free(t);
}
flag=0;
len--;
break;
}
else...{
tmp=tmp->next;
}
}
return flag;
}