チェーンテーブルのC/C++言語実装方式

33786 ワード

リストファイル:
// list.h

#ifndef  __MY_H_
#define  __MY_H_

typedef char EleType;   //            

//      
typedef struct node
{
    EleType data;
    struct node * next;
}ChainNode;

//   
typedef struct {
    ChainNode* head;
} List;


List* CreateList();     //    
void DestoryLIst(List* );       //    
void ClearList(List* );
int ListAppend(List*, EleType);     //    
int ListInsert(List*, int, EleType);    //    
int ListDelete(List*, int);     //    
int GetElememt(List*, int, EleType *);  //   
ChainNode* GetAddr(List*,int );     //     
ChainNode* NewChainNode(EleType);   //        
int TraverseList(List*, int(*) (EleType*) );    //    
void ShowList(List*);
int PrintElement(EleType *);
#endif

ファイルlist.cpp
// list.cpp

#include "list.h"
#include 
using namespace std;

//    ,     
List* CreateList()
{
    List* p = new List;
    EleType data = (EleType)0;
    p->head = NewChainNode(data);
    if(NULL != p && NULL !=p->head) 
    {
        p->head->data = data;
        p->head->next = NULL;
        return p;
    }
    cout<<"      "<<endl;
    return NULL;
}
    

//    
void DestoryLIst(List* lp)
{
    if(NULL != lp)
    {
        ClearList(lp);
        delete lp;
    }
}   

//    
void ClearList(List* lp)
{
    if(NULL != lp)
    {
        while(ListDelete(lp, 1) );
    }
    
}

//    
int ListAppend(List* lp, EleType ele)
{
    ChainNode* p=NULL;
    ChainNode* newp= NewChainNode(ele);
    if(NULL != lp && NULL != newp)
    {
        for(p=lp->head; p->next; p=p->next);
        p->next = newp; 
        return 1;
    }
    return 0;
}   

//    
int ListInsert(List* lp, int n, EleType ele)
{
    ChainNode* p=NULL;
    ChainNode* newp= NewChainNode(ele);

    if(NULL != lp && NULL != newp)
    {
        p = GetAddr(lp, n-1);
        newp->next = p->next;
        p->next = newp;
        return 1;
    }
    return 0;
}   

//    
int ListDelete(List* lp, int n)
{
    ChainNode* temp = NULL;
    if(NULL != lp && NULL != lp->head->next)
    {
        temp = GetAddr(lp, n-1);
        if(NULL != temp && NULL != temp->next)
        {
            temp->next = temp->next->next;
            return 1;
        }
    }
    return 0;
}

//   
int GetElememt(List* lp, int n, EleType * ele)
{
    ChainNode* p = NULL;
    if(NULL != lp && NULL != lp->head->next)
    {
        p =GetAddr(lp, n-1);
        if(NULL != p)
        {
            *ele = p->data;
            return 1;
        }
    }
    return 0;
}

//     
ChainNode* GetAddr(List* lp,int n )
{
    if(n >= 0)
    {
        ChainNode* p = lp->head;
        if(NULL != lp && NULL != p->next)
        {
            int i = 0;
            while(NULL !=p && i<n)
            {
                p = p->next;
                i++;
            }
            return p;
        }
    }
    cout<<"n      "<<endl;
    return NULL;
}

//        
ChainNode* NewChainNode(EleType ele)
{
    ChainNode* p =new ChainNode;
    if(p != NULL)
    {
        p->data = ele;
        p->next = NULL;
        
    }
    return p;
}

//    
int TraverseList(List* lp, int (*f) (EleType *) )   
{   
    if(NULL != lp)
    {
        ChainNode* p = lp->head->next;
        int i =0;
        for( ; NULL != p; p= p->next)
        {
            if( !f(&(p->data)) )
            {
                return i+1;
            }
            i++;
        }
        return 0;

    }

}

void ShowList(List* lp)
{
    TraverseList(lp, PrintElement);
    cout<<endl;
}

int PrintElement(EleType *data)
{
    cout<<" "<<*data;
    return 1;
}

ファイルmain.cpp
// main.cpp

#include "list.h"
#include 
using namespace std;

int main()
{
    char arr[20] ="hello world";

    List* lp = CreateList();
    if(NULL ==lp ) return 0;
    for(int i = 0; arr[i]; i++)
    {
        ListAppend(lp,arr[i]);
    }
    ShowList(lp);

    ListAppend(lp,'o');
    ListAppend(lp,'v');
    ListAppend(lp,'r');
    ListAppend(lp,'y');
    ShowList(lp);

    ListInsert(lp,1,'a');
    ListInsert(lp,2,'b');
    ShowList(lp);

    ListDelete(lp,1);
    ShowList(lp);
    ListDelete(lp,1);
    ShowList(lp);

    return 0;
}