/* main.cpp
* 测试顺序表类List
*/
#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(4,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();
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<<" 的元素的信息";
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();
l.show();
}
#include < iostream.h >
#include < string .h >
#include < stdlib.h >
#include " element.h "
#include " list.h "
void main( int ac, char * av[]) ... {
List l(4,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();
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<<" 的元素的信息";
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();
l.show();
}
/* list.h
*顺序表的实现部分
*/
#define
oope(msg) {perror(msg);exit(1);}
#define ELEMENT(n) ((char*)element+(n)*size)
class List ... {
private:
void * element;
/**//* size is the size of element, length means how many data in the list, count is the max length of the list */
int size,len,count;
public:
List(int,int); //init list
~List(); //destroy list
void show(); //print list
void clearList(); //clean up list
int isEmpty();
int length(); //return the number of elements in the list
int getElement(void *,int); //get a element
int locateElement(const void *); //return a element's location in the list
int priorElement(const void *,int *); //return a element's location which before another element
int nextElement(const void *,int *); //return a element's location which after another element
int insert(const void *); //insert a element to list
int del(const void *); //delete a element from list
} ;
List::List( int c, int s) ... {
count=c;
size=s;
len=0;
element=malloc(count*size);
if(element==NULL) oope("can not alloc memory");
}
List:: ~ List() ... {
free(element);
}
void List::show() ... {
int n;
cout<<"----------------------------------------------------"<<endl;
cout<<"there are "<<len<<" elements in the list"<<endl;
for(n=0;n<len;n++)
//here void * element must trun to short * element in order to add 1 qeual add 1 byte
showElement(ELEMENT(n));
cout<<"----------------------------------------------------"<<endl;
}
void List::clearList() ... {
len=0;
}
int List::isEmpty() ... {
return len;
}
int List::length() ... {
return len;
}
int List::getElement( void * dt, int n) ... { //if success return n else return -1;
if(n<0 || n>=len) return -1;
if(memcpy(dt,ELEMENT(n),size)==NULL) return -1;
return n;
}
int List::locateElement( const void * dt) ... {//if success return location else return -1
int n;
for(n=0;n<len;n++)
if(compareElement(dt,ELEMENT(n))==0) return n;
return -1;
}
// if success return prior location
// return -1 if error
int List::priorElement( const void * cur, int * pos) ... {
int n;
for(n=0;n<len;n++)
if(compareElement(cur,ELEMENT(n))==0) ...{*pos=n-1;return 0;}
return -1;
}
// if success return prior location else return -1
// return len if cur is the last element
int List::nextElement( const void * cur, int * pos) ... {
int n;
for(n=0;n<len;n++)
if(compareElement(cur,ELEMENT(n))==0) ...{*pos=n+1;return 0;}
return -1;
}
int List::insert( const void * dt) ... {//if success return location where insert else return -1
if(len==count)...{
cout<<"can not insert data beacuse list is full, please delete some element and try again"<<endl;
return -1;
}
int loc;
if((loc=locateElement(dt))!=-1)...{
cout<<"can not insert data beacuse it has been exist"<<endl;
return -1;
}
void *tmp;
tmp=ELEMENT(len);
if(memcpy(tmp,dt,size)==NULL) return -1;
len++;
return len;
}
int List::del( const void * dt) ... {//if success return location where delete else return -1
if(len==0)...{
cout<<"nothing to delete, list is empty"<<endl;
return -1;
}
int loc,n;
if((loc=locateElement(dt))==-1)...{
cout<<"nothing to delete, data is not exist in the list"<<endl;
return -1;
}
for(n=loc;n<len-1;n++)...{
memcpy(ELEMENT(n),ELEMENT(n+1),size);
}
len--;
return -1;
}
#define ELEMENT(n) ((char*)element+(n)*size)
class List ... {
private:
void * element;
/**//* size is the size of element, length means how many data in the list, count is the max length of the list */
int size,len,count;
public:
List(int,int); //init list
~List(); //destroy list
void show(); //print list
void clearList(); //clean up list
int isEmpty();
int length(); //return the number of elements in the list
int getElement(void *,int); //get a element
int locateElement(const void *); //return a element's location in the list
int priorElement(const void *,int *); //return a element's location which before another element
int nextElement(const void *,int *); //return a element's location which after another element
int insert(const void *); //insert a element to list
int del(const void *); //delete a element from list
} ;
List::List( int c, int s) ... {
count=c;
size=s;
len=0;
element=malloc(count*size);
if(element==NULL) oope("can not alloc memory");
}
List:: ~ List() ... {
free(element);
}
void List::show() ... {
int n;
cout<<"----------------------------------------------------"<<endl;
cout<<"there are "<<len<<" elements in the list"<<endl;
for(n=0;n<len;n++)
//here void * element must trun to short * element in order to add 1 qeual add 1 byte
showElement(ELEMENT(n));
cout<<"----------------------------------------------------"<<endl;
}
void List::clearList() ... {
len=0;
}
int List::isEmpty() ... {
return len;
}
int List::length() ... {
return len;
}
int List::getElement( void * dt, int n) ... { //if success return n else return -1;
if(n<0 || n>=len) return -1;
if(memcpy(dt,ELEMENT(n),size)==NULL) return -1;
return n;
}
int List::locateElement( const void * dt) ... {//if success return location else return -1
int n;
for(n=0;n<len;n++)
if(compareElement(dt,ELEMENT(n))==0) return n;
return -1;
}
// if success return prior location
// return -1 if error
int List::priorElement( const void * cur, int * pos) ... {
int n;
for(n=0;n<len;n++)
if(compareElement(cur,ELEMENT(n))==0) ...{*pos=n-1;return 0;}
return -1;
}
// if success return prior location else return -1
// return len if cur is the last element
int List::nextElement( const void * cur, int * pos) ... {
int n;
for(n=0;n<len;n++)
if(compareElement(cur,ELEMENT(n))==0) ...{*pos=n+1;return 0;}
return -1;
}
int List::insert( const void * dt) ... {//if success return location where insert else return -1
if(len==count)...{
cout<<"can not insert data beacuse list is full, please delete some element and try again"<<endl;
return -1;
}
int loc;
if((loc=locateElement(dt))!=-1)...{
cout<<"can not insert data beacuse it has been exist"<<endl;
return -1;
}
void *tmp;
tmp=ELEMENT(len);
if(memcpy(tmp,dt,size)==NULL) return -1;
len++;
return len;
}
int List::del( const void * dt) ... {//if success return location where delete else return -1
if(len==0)...{
cout<<"nothing to delete, list is empty"<<endl;
return -1;
}
int loc,n;
if((loc=locateElement(dt))==-1)...{
cout<<"nothing to delete, data is not exist in the list"<<endl;
return -1;
}
for(n=loc;n<len-1;n++)...{
memcpy(ELEMENT(n),ELEMENT(n+1),size);
}
len--;
return -1;
}
/* element.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;
}