數據結構之 鏈表

/* 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();
}

 

 

/* 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;
}

 

/* 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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章