C链表重构

类别:编程语言 点击:0 评论:0 推荐:

/*这次重写主要是为了增加结构的通用性、为了照顾初学的朋友我保
*留原来的代码,那个看起来更容易被初学者理解.代码中的mian函数
*只是简单的测试,大家可以根据需要自行修改.*/

————————————————lis.h——————————————

#ifndef LIST_H

#define LIST_H

 

/*定义函数状态*/

#ifndef OK

#define ERR -1

#define OK 1

#endif

typedef int status;            /*函数状态*/

typedef struct listitem {

    void            *date;    /*节点数据*/

    int              size;

    struct listitem *next;    /*指向下个节点*/  

} list_node;//链表节点

void* copy_node( int, void* );

status destroy_node( void* );

typedef struct {

    struct listitem  *ptr;    /*链表头指针*/

    int              size;    /*链表长度*/

} list;//链表

 

list* list_init ( void );     /*初始化*/

status list_destroy( list* ); /*销毁*/

status add_node( list*, void*, int );  /*加入一个节点*/

status delete_all( list* );//清空

status delete_node( list*, list_node* ); /*删除一个节点*/

status insert_node( list*, const list_node*, void*, int );

                                                                       /*插入一个节点*/

list_node* find_node( const list*,

                      bool (*)( const void*, const void* ),

                      void* ); /*查找*/

status list_print( const list*,

                               void (*)( void*, const char*),

                          const char* );/*打印*/

 

#endif

 

_________________________________list_node.c_____________________________________

#include <malloc.h>

#include "list.h"

void* copy_node( int size, void *date )

{

    void *p = malloc( size );

    if( size == sizeof(int) )

         *(int*)p = *(int*)date;

     else if ( size == sizeof(char) )

        *(char*)p = *(char*)date;   

     else if( size == sizeof(float) )

        *(float*)p = *(float*)date;

    else if( size == sizeof(double) )

        *(double*)p = *(double*)date;

       

    return p;

}   

status destroy_node( void *p )

{

    if(p != 0)

       free(p);

    return OK;

}   

____________________________list.c________________________________________

#include <stdio.h>

#include <stdlib.h>

#include "list.h"

list* list_init ( void )

{

    list *p = ( list* )malloc( sizeof( list ) );

    if( p == 0 )

       return 0;

    p->ptr = 0;  

    p->size = 0;

    return p;

}

 

status list_destroy( list *pev )

{

    if( pev == 0 )

       return ERR;

    delete_all( pev );

    free( pev );

    return OK;

}

 

status add_node( list *p, void *date, int size )

{

    list_node *pev =

       ( list_node* )malloc( sizeof( list_node ) );

    if( pev == 0 )

       return ERR;

    pev->date = copy_node( size, date );

    pev->next = p->ptr;

    p->ptr = pev;

    p->size++;

    return OK;

}

 

status delete_node( list *p, list_node *pev )

{

    list_node *temp = pev;

    if( pev == 0 )

       return ERR;

    pev = temp->next;

    destroy_node( temp->date );

    free( temp );

    p->size--;

    return OK;

}

 

status delete_all( list *pev )

{

    int ix;

    if( pev == 0 )

       return ERR;

    if( pev->size == 0 )

       return ERR;

    for( ix = 0; ix < pev->size; ++ix, ++pev->ptr )

       delete_node( pev, pev->ptr );

    return OK;

}

status insert_node( list *p, list_node* pev,

                                   void *date, int size )

{

    if( p == 0 )

       return ERR;

    if( pev == 0 )      

       add_node( p, date, size );

    else

    {

       list_node *pv =

         ( list_node* )malloc( sizeof( list_node ) );

       if( pev == 0 )

           return ERR;

       pv->date = copy_node( size, date );

       pv->next = pev->next;

       pev->next = pv;

       p->size++;

    }

    return OK;

}

 

list_node* find_node( const list *pev,
         bool (*f)( const void*, const void* ), 

                   void *date )

{

    int ix;

    list_node *p = pev->ptr;

    for( ix = 0; ix < pev->size; ++ix )

       if( (*f)(p->date, date) )

           return p;

       else

           p = p->next;

    return 0;

}

 

status list_print( const list *pev,
            void (*pprint)( void*, const char* ), 
            const char *ch )

{

    int ix;

    list_node *p = pev->ptr;

    if( pev == 0 )

       return ERR;

    if( pev->size == 0 )

       return OK;   

    for( ix = 0; ix < pev->size; ++ix )

    {

       (*pprint)( p->date, ch );

       printf( "\t" );

       p = p->next;

    }

    printf( "\n" );

    return OK;

}

____________________________ios.h_____________________________

#ifndef IOS_H

#define IOS_H

void output( void*, const char* );

void input( void*, const char* );

#endif

__________________________ios.c___________________________________

#include <stdio.h>

#include <string.h>

#include "ios.h"

 

void output( void* date, const char *cnt )

{

    if( strcmp(cnt, "%d") )

        printf( cnt, *(int*)date );

    else if( strcmp(cnt, "%c") )

        printf( cnt, *(char*)date );   

    else if( strcmp(cnt, "%s") )

        printf( cnt, *(char**)date );

    else if( strcmp(cnt, "%f") )

        printf( cnt, *(double*)date );

}

void input( void* date, const char *cnt )

{

    if( strcmp(cnt, "%d") )

        scanf( cnt, (int*)date );

    else if( strcmp(cnt, "%c") )

        printf( cnt, (char*)date );       

    else if( strcmp(cnt, "%s") )   

        scanf( cnt, (char**)date );

    else if( strcmp(cnt, "%f") )

        scanf( cnt, (double*)date );

}   

_____________________________maim.c______________________________________

#include <stdio.h>

#include <stdlib.h> 

#include "list.h"

#include "ios.h"

 

 

int main()

{

    list *mylistp = list_init();

    for( int i = 0; i < 10; ++i )

    {

        char a = 'a' + i;

        add_node( mylistp,&a, sizeof(a) );

    }

    list_print( mylistp, output, "%c" );

    delete_all( mylistp );  

       system( "pause" );

       return 0;           

}

 

本文地址:http://com.8s8s.com/it/it27383.htm