链表实现

link.h

 

#ifndef __LINK_H__
#define __LINK_H__

#define ERROR ( -1 )
#define OK      ( 0  )

#define TRUE  ( 1 == 1 )
#define FALSE ( !TRUE  )


typedef int BOOL;
typedef int elem_t;            //定义元素的数据类型

typedef struct node
{
    elem_t data;
    elem_t bata;
    struct node * next;
}tagNode_t,tagList_t;


/**//* 把已经存在的头节点进行初始化为一个空链表 */
void initList ( tagList_t * list );
/**//* 销毁链表 */
void destroyList( tagList_t * list );

/**//* 将链表重置为空 */
void clearList( tagList_t * list );

BOOL listEmpty( tagList_t * list );
/**//* 链表长度 */
int listLength( tagList_t * list );

/**//* 得到指定位置 pos 的元素,如果顺利得到则返回TRUE,否则返回 FALSE*/
BOOL getElem( tagList_t * list, int iPos, elem_t * e);

/**//* 返回第一个满足 compare 关系的元素的位置,如果不存在则返回 -1 */
int locateElem( tagList_t * list, elem_t e,
    BOOL (*compare)(elem_t e1,elem_t e2));

/**//* 将元素e插到链表的指定位置 ,若iPos > listLength() 则插到最后*/
BOOL listInsert( tagList_t * list, int iPos, elem_t e );

BOOL listInsertFront( tagList_t * list, elem_t e, elem_t f);

/**//* 按照费递减序插入元素 e */
BOOL listSortInsert( tagList_t * list, elem_t e );

BOOL listDeleteFront( tagList_t * list, elem_t * e );

/**//* 用visit 函数遍历链表 */
void listTraverse( tagList_t * list, void (*visit)(elem_t e));
/**//* 打印链表 */
void listPrint( tagList_t * list);

#endif

--------------------------------------------------------------------------

 

linkFun.h

 

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


/**//* 把已经存在的头节点进行初始化为一个空链表 */
void initList ( tagList_t * list )
{
    list->next = NULL;
}
/**//* 销毁链表,没有释放头结点 */
void destroyList( tagList_t * list )
{
    tagNode_t * pn = NULL;
    assert( list != NULL );
   
    pn = list->next;
    while( pn != NULL)
    {
        list->next = pn->next;
        free( pn );
        pn = list->next;
    }
}

/**//* 将链表重置为空 */
void clearList( tagList_t * list )
{
    destroyList( list );  //同 销毁
}

BOOL listEmpty( tagList_t * list )
{
    return ( NULL == list->next );
}

int listLength( tagList_t * list )
{
    int iLen = 0;
    tagNode_t * ptn = NULL;
    assert( list != NULL );
   
    ptn = list->next;
    while( ptn != NULL )
    {
        iLen += 1;
        ptn = ptn->next;
    }
    return iLen;
}

/**//* 得到指定位置 pos 的元素,如果顺利得到则返回TRUE,否则返回 FALSE*/
BOOL getElem( tagList_t * list, int iPos, elem_t * e)
{
    BOOL bRet = FALSE;
    tagNode_t * ptn = NULL;
    assert( list );        //list != NULL
   
    if( list || ( iPos >= 0 ) )
    {
        ptn = list->next;
        while( iPos-- && ptn )
        {
            ptn = ptn->next;
        }       
        if( ptn != NULL )
        {
            *e = ptn->data;
            bRet = TRUE;
        }       
    }
    return bRet;   
}

/**//* 返回第一个满足 compare 关系的元素的位置,如果不存在则返回 0 */
int locateElem( tagList_t * list, elem_t e,
    BOOL (*compare)(elem_t e1,elem_t e2))
{
    int iPos = 0;
    tagNode_t * ptn = NULL;
    assert( list );        //list != NULL
   
    ptn = list->next;
    while( ptn )
    {
        iPos += 1;
        if( compare( e, ptn->data ) )
        {
            break;
        }
        ptn = ptn->next;
    }
    if( NULL == ptn )
    {
        iPos = 0;
    }
    return iPos;
   
}

/**//* 将元素e插到链表的指定位置 ,若iPos > listLength() 则插到最后*/
BOOL listInsert( tagList_t * list, int iPos, elem_t e )
{
    BOOL bRet = FALSE;
    tagNode_t * ptn = NULL, * pstNew = NULL;
    assert( list );        //list != NULL
   
    if( list || (iPos >= 0) )
    {
        ptn = list->next;
        while( iPos-- && ptn->next )
        {
            ptn = ptn->next;
        }       

        pstNew = ( tagNode_t * )malloc( sizeof( tagNode_t ) );
        if( pstNew != NULL )
        {
            pstNew->data = e;
            pstNew->next = ptn->next;
            ptn->next = pstNew;
           
            bRet = TRUE;
        }   
    }
    return bRet;
}

BOOL listInsertFront( tagList_t * list, elem_t e, elem_t f)
{
    BOOL bRet = FALSE;
    tagNode_t * pstNew =
        ( tagNode_t * )malloc( sizeof( tagNode_t ) );
    assert( list );
   
    if( pstNew != NULL )
    {
        pstNew->data = e;
        pstNew->bata = f;
        pstNew->next = list->next;
        list->next = pstNew;
       
        bRet = TRUE;
    }
    return bRet;
}

/**//* 按照非递减序插入元素 e */
BOOL listSortInsert( tagList_t * list, elem_t e )
{
    BOOL bRet = FALSE;
    tagNode_t * ptn = NULL, * pstNew = NULL;
    assert( list );
   
    ptn = list;
    while( ptn->next )
    {
        if( ptn->next->data > e )
        {       
            bRet = TRUE;
            break;
        }
        ptn = ptn->next;
    }
   
    pstNew = ( tagNode_t * )malloc( sizeof( tagNode_t ) );
    if( pstNew != NULL)
    {
        pstNew->data = e;
        pstNew->next = ptn->next;
        ptn->next = pstNew;
       
        bRet = TRUE;
    }

    return bRet;
}

BOOL listDeleteFront( tagList_t * list, elem_t * e )
{
    BOOL bRet = FALSE;
    tagNode_t * ptn = NULL;
   
    if( list != NULL )
    {
        ptn = list->next;
        *e = ptn->data;
        list->next = ptn->next;
        free( ptn );
        bRet = TRUE;
    }
    return bRet;
}

/**//* 用visit 函数遍历链表 */
void listTraverse( tagList_t * list, void (*visit)(elem_t e))
{
    tagNode_t * ptn = NULL, * pstNew = NULL;
    assert( list );
   
    ptn = list->next;
    while( ptn )
    {
        visit( ptn->data );
        ptn = ptn->next;
    }
}

/**//* 打印链表 */
void listPrint( tagList_t * list)
{
    tagNode_t * ptn = NULL;
    assert( list );
   
    ptn = list->next;
    while( ptn )
    {
        printf("list.data = %d  list.bata = %d\n",ptn->data,ptn->bata);
        ptn = ptn->next;
    }    
}

-------------------------------------------------------------------------------------

 

main.c

 

#include <stdio.h>
#include "link.h"
#include "linkFun.h"

int main()
{
    elem_t e;
    tagList_t list;
   

   
    initList( &list );
   
    printf("IsEmpty: %s \n",(listEmpty( &list ) ? "true":"false"));
   
     
    listInsertFront( &list, 1 , 8);
    listInsertFront( &list, 2 , 7);   
    listInsertFront( &list, 3 , 6);
    listInsertFront( &list, 4 , 5);       
   
    listInsertFront( &list, 5 , 4);
    listInsertFront( &list, 6 , 3);   
    listInsertFront( &list, 7 , 2);
    listInsertFront( &list, 8 , 1);       
       
   
   
    /*
    listSortInsert( &list, 4 );
    listSortInsert( &list, 3 );
   

   
    listSortInsert( &list, 5 );
   

   
    listSortInsert( &list, 7 );
    listSortInsert( &list, 2 );
    */
    printf("getElem (3) %s : %d \n",(getElem( &list, 3, &e )?"right":"error"),e);
    printf("getElem (6) %s : %d \n",(getElem( &list, 6, &e )?"right":"error"),e);
   
    listPrint( &list );
    printf(" ");
   
    printf("len: %d ",listLength( &list ));
    listDeleteFront( &list, &e );
    printf("After delete front len: %d \n",listLength( &list ));
    printf("IsEmpty: %s \n",(listEmpty( &list ) ? "true":"false"));

   
    destroyList( &list );
   
    printf("IsEmpty: %s \n",(listEmpty( &list ) ? "true":"false"));
   
    system("pause");
}


 

-----------------------------------------------------------

DEVc++ 调试结果:

IsEmpty: true
getElem (3) right : 5
getElem (6) right : 2
list.data = 8  list.bata = 1
list.data = 7  list.bata = 2
list.data = 6  list.bata = 3
list.data = 5  list.bata = 4
list.data = 4  list.bata = 5
list.data = 3  list.bata = 6
list.data = 2  list.bata = 7
list.data = 1  list.bata = 8
 len: 8 After delete front len: 7
IsEmpty: false
IsEmpty: true
请按任意键继续. . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值