C语言有头结点链表的经典实现
经常用到链表,但每次写都不那么顺利,终于有点时间整理一下,把感觉写的不错的代码拿出来分享,希望大家能指出问题,那我算没白写。 该链表以存放整型数据为例。
头文件:
 #ifndef __LINK_H__
#ifndef __LINK_H__ #define __LINK_H__
#define __LINK_H__
 #define ERROR ( -1 )
#define ERROR ( -1 ) #define OK      ( 0  )
#define OK      ( 0  )
 #define TRUE  ( 1 == 1 )
#define TRUE  ( 1 == 1 ) #define FALSE ( !TRUE  )
#define FALSE ( !TRUE  )

 typedef int BOOL;
typedef int BOOL; typedef int elem_t;            //定义元素的数据类型
typedef int elem_t;            //定义元素的数据类型 
 typedef struct node
typedef struct node {
{ elem_t data;
    elem_t data; struct node * next;
    struct node * next; }tagNode_t,tagList_t;
}tagNode_t,tagList_t;

 /* 把已经存在的头节点进行初始化为一个空链表 */
/* 把已经存在的头节点进行初始化为一个空链表 */ void initList ( tagList_t * list );
void initList ( tagList_t * list ); /* 销毁链表 */
/* 销毁链表 */ void destroyList( tagList_t * list );
void destroyList( tagList_t * list );
 /* 将链表重置为空 */
/* 将链表重置为空 */ void clearList( tagList_t * list );
void clearList( tagList_t * list );
 BOOL listEmpty( tagList_t * list );
BOOL listEmpty( tagList_t * list );
 int listLength( tagList_t * list );
int listLength( tagList_t * list );
 /* 得到指定位置 pos 的元素,如果顺利得到则返回TRUE,否则返回 FALSE*/
/* 得到指定位置 pos 的元素,如果顺利得到则返回TRUE,否则返回 FALSE*/ BOOL getElem( tagList_t * list, int iPos, elem_t * e);
BOOL getElem( tagList_t * list, int iPos, elem_t * e);
 /* 返回第一个满足 compare 关系的元素的位置,如果不存在则返回 -1 */
/* 返回第一个满足 compare 关系的元素的位置,如果不存在则返回 -1 */ int locateElem( tagList_t * list, elem_t e,
int locateElem( tagList_t * list, elem_t e,  BOOL (*compare)(elem_t e1,elem_t e2));
    BOOL (*compare)(elem_t e1,elem_t e2));
 /* 将元素e插到链表的指定位置 ,若iPos > listLength() 则插到最后*/
/* 将元素e插到链表的指定位置 ,若iPos > listLength() 则插到最后*/ BOOL listInsert( tagList_t * list, int iPos, elem_t e );
BOOL listInsert( tagList_t * list, int iPos, elem_t e );
 BOOL listInsertFront( tagList_t * list, elem_t e );
BOOL listInsertFront( tagList_t * list, elem_t e );
 /* 按照费递减序插入元素 e */
/* 按照费递减序插入元素 e */ BOOL listSortInsert( tagList_t * list, elem_t e );
BOOL listSortInsert( tagList_t * list, elem_t e );
 BOOL listDeleteFront( tagList_t * list, elem_t * e );
BOOL listDeleteFront( tagList_t * list, elem_t * e );
 /* 用visit 函数遍历链表 */
/* 用visit 函数遍历链表 */ void listTraverse( tagList_t * list, void (*visit)(elem_t e));
void listTraverse( tagList_t * list, void (*visit)(elem_t e));

 #endif
#endif
源文件:

 #include <stdio.h>
#include <stdio.h> #include <stdlib.h>
#include <stdlib.h> #include <assert.h>
#include <assert.h> #include "link.h"
#include "link.h"

 /* 把已经存在的头节点进行初始化为一个空链表 */
/* 把已经存在的头节点进行初始化为一个空链表 */ void initList ( tagList_t * list )
void initList ( tagList_t * list ) {
{ list->next = NULL;
    list->next = NULL; }
} /* 销毁链表,没有释放头结点 */
/* 销毁链表,没有释放头结点 */ void destroyList( tagList_t * list )
void destroyList( tagList_t * list ) {
{ tagNode_t * pn = NULL;
    tagNode_t * pn = NULL; assert( list != NULL );
    assert( list != NULL ); 
     pn = list->next;
    pn = list->next; while( pn != NULL)
    while( pn != NULL) {
    { list->next = pn->next;
        list->next = pn->next; free( pn );
        free( pn ); pn = list->next;
        pn = list->next; }
    } }
}
 /* 将链表重置为空 */
/* 将链表重置为空 */ void clearList( tagList_t * list )
void clearList( tagList_t * list ) {
{ destroyList( list );  //同 销毁
    destroyList( list );  //同 销毁  }
}
 BOOL listEmpty( tagList_t * list )
BOOL listEmpty( tagList_t * list ) {
{ return ( NULL == list->next );
    return ( NULL == list->next ); }
}
 int listLength( tagList_t * list )
int listLength( tagList_t * list ) {
{ int iLen = 0;
    int iLen = 0; tagNode_t * ptn = NULL;
    tagNode_t * ptn = NULL; assert( list != NULL );
    assert( list != NULL ); 
     ptn = list->next;
    ptn = list->next; while( ptn != NULL )
    while( ptn != NULL ) {
    { iLen += 1;
        iLen += 1; ptn = ptn->next;
        ptn = ptn->next; }
    } return iLen;
    return iLen; }
}
 /* 得到指定位置 pos 的元素,如果顺利得到则返回TRUE,否则返回 FALSE*/
/* 得到指定位置 pos 的元素,如果顺利得到则返回TRUE,否则返回 FALSE*/ BOOL getElem( tagList_t * list, int iPos, elem_t * e)
BOOL getElem( tagList_t * list, int iPos, elem_t * e) {
{ BOOL bRet = FALSE;
    BOOL bRet = FALSE; tagNode_t * ptn = NULL;
    tagNode_t * ptn = NULL; assert( list );        //list != NULL
    assert( list );        //list != NULL 
     if( list || ( iPos >= 0 ) )
    if( list || ( iPos >= 0 ) ) {
    { ptn = list->next;
        ptn = list->next; while( iPos-- && ptn )
        while( iPos-- && ptn ) {
        { ptn = ptn->next;
            ptn = ptn->next; }
        }         if( ptn != NULL )
        if( ptn != NULL ) {
        { *e = ptn->data;
            *e = ptn->data; bRet = TRUE;
            bRet = TRUE; }
        }         }
    } return bRet;
    return bRet;     }
}
 /* 返回第一个满足 compare 关系的元素的位置,如果不存在则返回 0 */
/* 返回第一个满足 compare 关系的元素的位置,如果不存在则返回 0 */ int locateElem( tagList_t * list, elem_t e,
int locateElem( tagList_t * list, elem_t e,  BOOL (*compare)(elem_t e1,elem_t e2))
    BOOL (*compare)(elem_t e1,elem_t e2)) {
{ int iPos = 0;
    int iPos = 0; tagNode_t * ptn = NULL;
    tagNode_t * ptn = NULL; assert( list );        //list != NULL
    assert( list );        //list != NULL 
     ptn = list->next;
    ptn = list->next; while( ptn )
    while( ptn ) {
    { iPos += 1;
        iPos += 1; if( compare( e, ptn->data ) )
        if( compare( e, ptn->data ) ) {
        { break;
            break; }
        } ptn = ptn->next;
        ptn = ptn->next; }
    } if( NULL == ptn )
    if( NULL == ptn ) {
    { iPos = 0;
        iPos = 0; }
    } return iPos;
    return iPos; 
     }
}
 /* 将元素e插到链表的指定位置 ,若iPos > listLength() 则插到最后*/
/* 将元素e插到链表的指定位置 ,若iPos > listLength() 则插到最后*/ BOOL listInsert( tagList_t * list, int iPos, elem_t e )
BOOL listInsert( tagList_t * list, int iPos, elem_t e ) {
{ BOOL bRet = FALSE;
    BOOL bRet = FALSE; tagNode_t * ptn = NULL, * pstNew = NULL;
    tagNode_t * ptn = NULL, * pstNew = NULL; assert( list );        //list != NULL
    assert( list );        //list != NULL 
     if( list || (iPos >= 0) )
    if( list || (iPos >= 0) ) {
    { ptn = list->next;
        ptn = list->next; while( iPos-- && ptn->next )
        while( iPos-- && ptn->next ) {
        { ptn = ptn->next;
            ptn = ptn->next; }
        }        
 pstNew = ( tagNode_t * )malloc( sizeof( tagNode_t ) );
        pstNew = ( tagNode_t * )malloc( sizeof( tagNode_t ) ); if( pstNew != NULL )
        if( pstNew != NULL ) {
        { pstNew->data = e;
            pstNew->data = e; pstNew->next = ptn->next;
            pstNew->next = ptn->next; ptn->next = pstNew;
            ptn->next = pstNew; 
             bRet = TRUE;
            bRet = TRUE; }
        }     }
    } return bRet;
    return bRet; }
}
 BOOL listInsertFront( tagList_t * list, elem_t e )
BOOL listInsertFront( tagList_t * list, elem_t e ) {
{ BOOL bRet = FALSE;
    BOOL bRet = FALSE; tagNode_t * pstNew =
    tagNode_t * pstNew =  ( tagNode_t * )malloc( sizeof( tagNode_t ) );
        ( tagNode_t * )malloc( sizeof( tagNode_t ) ); assert( list );
    assert( list ); 
     if( pstNew != NULL )
    if( pstNew != NULL ) {
    { pstNew->data = e;
        pstNew->data = e; pstNew->next = list->next;
        pstNew->next = list->next; list->next = pstNew;
        list->next = pstNew; 
         bRet = TRUE;
        bRet = TRUE; }
    } return bRet;
    return bRet; }
}
 /* 按照非递减序插入元素 e */
/* 按照非递减序插入元素 e */ BOOL listSortInsert( tagList_t * list, elem_t e )
BOOL listSortInsert( tagList_t * list, elem_t e ) {
{ BOOL bRet = FALSE;
    BOOL bRet = FALSE; tagNode_t * ptn = NULL, * pstNew = NULL;
    tagNode_t * ptn = NULL, * pstNew = NULL; assert( list );
    assert( list ); 
     ptn = list;
    ptn = list; while( ptn->next )
    while( ptn->next ) {
    { if( ptn->next->data > e )
        if( ptn->next->data > e ) {
        {         bRet = TRUE;
            bRet = TRUE; break;
            break; }
        } ptn = ptn->next;
        ptn = ptn->next; }
    } 
     pstNew = ( tagNode_t * )malloc( sizeof( tagNode_t ) );
    pstNew = ( tagNode_t * )malloc( sizeof( tagNode_t ) ); if( pstNew != NULL)
    if( pstNew != NULL) {
    { pstNew->data = e;
        pstNew->data = e; pstNew->next = ptn->next;
        pstNew->next = ptn->next; ptn->next = pstNew;
        ptn->next = pstNew; 
         bRet = TRUE;
        bRet = TRUE; }
    }
 return bRet;
    return bRet; }
}
 BOOL listDeleteFront( tagList_t * list, elem_t * e )
BOOL listDeleteFront( tagList_t * list, elem_t * e ) {
{ BOOL bRet = FALSE;
    BOOL bRet = FALSE; tagNode_t * ptn = NULL;
    tagNode_t * ptn = NULL; 
     if( list != NULL )
    if( list != NULL ) {
    { ptn = list->next;
        ptn = list->next; *e = ptn->data;
        *e = ptn->data; list->next = ptn->next;
        list->next = ptn->next; free( ptn );
        free( ptn ); bRet = TRUE;
        bRet = TRUE; }
    } return bRet;
    return bRet; }
}
 /* 用visit 函数遍历链表 */
/* 用visit 函数遍历链表 */ void listTraverse( tagList_t * list, void (*visit)(elem_t e))
void listTraverse( tagList_t * list, void (*visit)(elem_t e)) {
{ tagNode_t * ptn = NULL, * pstNew = NULL;
    tagNode_t * ptn = NULL, * pstNew = NULL; assert( list );
    assert( list ); 
     ptn = list->next;
    ptn = list->next; while( ptn )
    while( ptn ) {
    { visit( ptn->data );
        visit( ptn->data ); ptn = ptn->next;
        ptn = ptn->next; }
    } }
}

测试文件:
 include <stdio.h>
include <stdio.h> #include "link.h"
#include "link.h"
 void tst_link()
void tst_link() {
{ elem_t e;
    elem_t e; tagList_t list;
    tagList_t list; 
     printf("********Test Stack******** ");
    printf("********Test Stack******** "); 
     initList( &list );
    initList( &list ); 
     printf("IsEmpty: %s ",(listEmpty( &list ) ? "true":"false"));
    printf("IsEmpty: %s ",(listEmpty( &list ) ? "true":"false")); 
     listSortInsert( &list, 4 );
    listSortInsert( &list, 4 ); listSortInsert( &list, 3 );
    listSortInsert( &list, 3 ); 
     printf("5 in pos: %d ",locateElem( &list,5,elemEqual ));
    printf("5 in pos: %d ",locateElem( &list,5,elemEqual )); 
     listSortInsert( &list, 5 );
    listSortInsert( &list, 5 ); 
     printf("5 in pos: %d ",locateElem( &list,5,elemEqual ));
    printf("5 in pos: %d ",locateElem( &list,5,elemEqual )); 
     listSortInsert( &list, 7 );
    listSortInsert( &list, 7 ); listSortInsert( &list, 2 );
    listSortInsert( &list, 2 ); 
     printf("getElem (3) %s : %d ",(getElem( &list, 3, &e )?"right":"error"),e);
    printf("getElem (3) %s : %d ",(getElem( &list, 3, &e )?"right":"error"),e); printf("getElem (6) %s : %d ",(getElem( &list, 6, &e )?"right":"error"),e);
    printf("getElem (6) %s : %d ",(getElem( &list, 6, &e )?"right":"error"),e); 
     listPrint( &list );
    listPrint( &list ); printf(" ");
    printf(" "); 
     printf("len: %d ",listLength( &list ));
    printf("len: %d ",listLength( &list )); listDeleteFront( &list, &e );
    listDeleteFront( &list, &e ); printf("After delete front len: %d ",listLength( &list ));
    printf("After delete front len: %d ",listLength( &list )); printf("IsEmpty: %s ",(listEmpty( &list ) ? "true":"false"));
    printf("IsEmpty: %s ",(listEmpty( &list ) ? "true":"false")); printf("5 in pos: %d ",locateElem( &list,5,elemEqual ));
    printf("5 in pos: %d ",locateElem( &list,5,elemEqual )); 
     destroyList( &list );
    destroyList( &list ); 
     printf("IsEmpty: %s ",(listEmpty( &list ) ? "true":"false"));
    printf("IsEmpty: %s ",(listEmpty( &list ) ? "true":"false")); }
}
 
                     
                    
                 
                    
                
 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号