#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(constvoid*); //return a element's location in the list int priorElement(constvoid*,int*); //return a element's location which before another element int nextElement(constvoid*,int*); //return a element's location which after another element int insert(constvoid*); //insert a element to list int del(constvoid*); //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(constvoid*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(constvoid*cur,int*pos)...{ int n; for(n=0;n<len;n++) if(compareElement(cur,ELEMENT(n))==0) ...{*pos=n-1;return0;} return-1; } //if success return prior location else return -1 //return len if cur is the last element int List::nextElement(constvoid*cur,int*pos)...{ int n; for(n=0;n<len;n++) if(compareElement(cur,ELEMENT(n))==0) ...{*pos=n+1;return0;} return-1; } int List::insert(constvoid*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(constvoid*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(constvoid*dt)...{ struct element *e=(struct element *)dt; cout<<"NO. "<<e->no<<" NAME: "<<e->name<<endl; } int compareElement(constvoid*d1,constvoid*d2)...{ struct element *e1,*e2; e1=(struct element *)d1; e2=(struct element *)d2; return e1->no-e2->no; }