C语言单向链表的实现


采用VS2013编辑器编写的C语言单向链表代码:

#include <stdio.h>
#include <windows.h>

typedef  int TypeData;
#define  NODE_LENGTH sizeof(NODE)

/**定义链表的结构体*/
typedef struct tagNode
{
    TypeData tdData;
    struct tagNode *plNext;
}NODE;

/*******函数声明****************************/
NODE* createList(TypeData tdInData);
int forEachList(NODE* pstInHead);
int insertListTail(NODE* pstInHead, TypeData tdInData);
int insertListHead(NODE* pstInHead, TypeData tdInData);
int getListLength(NODE* pstInHead);
int delListHead(NODE* pstInHead);
int delListTail(NODE* pstInHead);
int sortList(NODE* pstInHead, int nInFlag);
int clearList(NODE* pstInHead);

/*****************************************
函数功能:创建一个新的链表,带头节点
函数入参:tdInData 头结点的值
 返回值: 非NULL 成功
        NULL 失败
******************************************/
NODE* createList(TypeData tdInData)
{
    NODE* pstNewNode = (NODE*)malloc(NODE_LENGTH);
    if (NULL == pstNewNode)
    {
        printf("createList: malloc failed");
        return NULL;
    }
    pstNewNode->plNext = NULL;
    pstNewNode->tdData = tdInData;
    return pstNewNode;
}

/*****************************************
函数功能:遍历整个链表,把链表的值都显示出来,
函数入参:pstInHead 链表的头结点
  返回值:0 成功
        1 失败
******************************************/
int forEachList(NODE* pstInHead)
{
    if (NULL == pstInHead)
    {
        printf("forEachList: the parameter pstInHead is NULL");
        return 1;
    }
    while (NULL != pstInHead->plNext)
    {
        printf("%d-->", pstInHead->tdData);
        pstInHead = pstInHead->plNext;
    }
    printf("%d\n", pstInHead->tdData);
    return 0;
}
/*****************************************
函数功能:在链表的尾部插入数据
函数入参:pstInHead 链表的头结点
        tdInData 在尾部插入数据的值
  返回值: 0 成功
        1 失败
******************************************/
int insertListTail(NODE* pstInHead, TypeData tdInData)
{
    if (NULL == pstInHead)
    {
        printf("insertListTail: the parameter pstInHead is NULL");
        return 1;
    }
    while (NULL != pstInHead->plNext)
    {
        pstInHead = pstInHead->plNext;
    }
    NODE* pstNewNode = (NODE*)malloc(NODE_LENGTH);
    if (NULL == pstNewNode)
    {
        printf("insertListTail: malloc pstNewNode failed");
        return 1;
    }
    pstNewNode->plNext = NULL;
    pstNewNode->tdData = tdInData;
    pstInHead->plNext = pstNewNode;
    return 0;
}
/*****************************************
函数功能:在链表的首部插入数据,就是在头结点的后面插入数据
函数入参:pstInHead 链表的头结点
        tdInData 在尾部插入数据的值
返回值: 0 成功
      1 失败
******************************************/
int insertListHead(NODE* pstInHead, TypeData tdInData)
{
    if (NULL == pstInHead)
    {
        printf("insertListHead: the parameter pstInHead is NULL");
        return 1;
    }
    NODE* pstNewNode = (NODE*)malloc(NODE_LENGTH);
    if (NULL == pstNewNode)
    {
        printf("insertListTail: malloc pstNewNode failed");
        return 1;
    }
    pstNewNode->tdData = tdInData;

    /**将该节点插入头部*/
    pstNewNode->plNext = pstInHead->plNext;
    pstInHead->plNext = pstNewNode;
    return 0;
}
/*****************************************
函数功能:删除头结点
函数入参:pstInHead 链表的头结点
返回值: 0 成功
      1 失败
******************************************/
int delListHead(NODE* pstInHead)
{
    if (NULL == pstInHead)
    {
        printf("delListHead: the parameter pstInHead is NULL");
        return 1;
    }
    int nListLen = getListLength(pstInHead);
    NODE* pstTempNode = NULL;

    /**链表的长度至少为2的时候,才能删除头部*/
    if (nListLen >= 2)
    {
        pstTempNode = pstInHead->plNext;
        pstInHead->plNext = pstInHead->plNext->plNext;
        free(pstTempNode);
    }
    return 0;
}
/*****************************************
函数功能:获得链表的长度,包括头结点
函数入参:pstInHead 链表的头结点
返回值: 0 成功
      1 失败
******************************************/
int getListLength(NODE* pstInHead)
{
    int nLen = 0;
    if (NULL == pstInHead)
    {
        printf("getListLength: the parameter pstInHead is NULL");
        return 1;
    }
    while (NULL != pstInHead)
    {
        nLen++;
        pstInHead = pstInHead->plNext;
    }
    return nLen;
}
/*****************************************
函数功能:删除链表的尾节点
函数入参:pstInHead 链表的头结点
返回值: 0 成功
      1 失败
******************************************/
int delListTail(NODE* pstInHead)
{
    if (NULL == pstInHead)
    {
        printf("delListTail: the parameter pstInHead is NULL");
        return 1;
    }
    int nListLen = getListLength(pstInHead);
    /**如果只有头结点直接返回*/
    if (1 == nListLen)
    {
        return 0;
    }
    while (NULL != pstInHead->plNext->plNext)
    {
        pstInHead = pstInHead->plNext;
    }
    NODE* pstTempNode = pstInHead->plNext;
    pstInHead->plNext = NULL;
    free(pstTempNode);
    return 0;
}
/*****************************************
函数功能:对整个链表进行排序,头结点不参与排序
函数入参:pstInHead 链表的头结点
        nInFlag 0 降序,1升序
返回值: 0 成功
      1 失败
******************************************/
int sortList(NODE* pstInHead, int nInFlag)
{
    if (NULL == pstInHead)
    {
        printf("sortList: the parameter pstInHead is NULL");
        return 1;
    }
    int nListLen = getListLength(pstInHead);
    /**如果只有头结点,不用比较,直接返回*/
    if (1 == nListLen)
    {
        return 0;
    }
    NODE* pstTempNode = NULL;
    pstInHead = pstInHead->plNext;
    TypeData tdTempData = 0;
    /**如果是正序排列*/
    if (0 == nInFlag)
    {
        while (NULL != pstInHead->plNext)
        {
            pstTempNode = pstInHead->plNext;
            while (NULL != pstTempNode)
            {
                if (pstInHead->tdData > pstTempNode->tdData)
                {
                    tdTempData = pstInHead->tdData;
                    pstInHead->tdData = pstTempNode->tdData;
                    pstTempNode->tdData = tdTempData;
                }
                pstTempNode = pstTempNode->plNext;
            }
            pstInHead = pstInHead->plNext;
        }
    }
    /**如果是反序排列*/
    else if (1 == nInFlag)
    {
        while (NULL != pstInHead->plNext)
        {
            pstTempNode = pstInHead->plNext;
            while (NULL != pstTempNode)
            {
                if (pstInHead->tdData < pstTempNode->tdData)
                {
                    tdTempData = pstInHead->tdData;
                    pstInHead->tdData = pstTempNode->tdData;
                    pstTempNode->tdData = tdTempData;
                }
                pstTempNode = pstTempNode->plNext;
            }
            pstInHead = pstInHead->plNext;
        }
    }
    else
    {
        printf("You choice is wrong.Please input (0 | 1)\n");
    }

    return 0;
}
/*****************************************
函数功能:清空整个链表
函数入参:pstInHead 链表的头结点
返回值: 0 成功
      1 失败
******************************************/
int clearList(NODE* pstInHead)
{
    if (NULL == pstInHead)
    {
        printf("clearList: the parameter pstInHead is NULL");
        return 1;
    }
    int nListLen = getListLength(pstInHead);
    /**暂存链表的头结点*/
    NODE* pstTempHead = pstInHead;

    /**如果只有头结点直接返回*/
    if (1 == nListLen)
    {
        return 0;
    }
    NODE* pstTempNode = NULL;
    pstInHead = pstInHead->plNext;
    while (NULL != pstInHead)
    {
        pstTempNode = pstInHead;
        pstInHead = pstInHead->plNext;
        free(pstTempNode);
    }
    pstTempHead->plNext = NULL;
    return 0;
}
int main()
{
    NODE* pstListHead = createList(-1);

    /**测试插入头部、尾部函数*/
    insertListTail(pstListHead, 11);
    insertListTail(pstListHead, 12);
    insertListTail(pstListHead, 13);
    insertListTail(pstListHead, 5);
    insertListHead(pstListHead, 22);
    /**遍历整个链表*/
    forEachList(pstListHead);

    /**测试删除头部节点*/
    delListHead(pstListHead);
    forEachList(pstListHead);

    /**测试删除尾部节点*/
    delListTail(pstListHead);
    forEachList(pstListHead);

    /**测试清空链表*/
    clearList(pstListHead);
    forEachList(pstListHead);

    /**测试插入头部、尾部函数*/
    insertListTail(pstListHead, 101);
    insertListTail(pstListHead, 2);
    insertListTail(pstListHead, 76);
    insertListTail(pstListHead, 43);
    insertListHead(pstListHead, 22);
    insertListHead(pstListHead, 333);
    forEachList(pstListHead);

    /*测试正排序函数**/
    sortList(pstListHead, 0);
    forEachList(pstListHead);

    /*测试反排序函数**/
    sortList(pstListHead, 3);
    forEachList(pstListHead);

    system("PAUSE");
    return 0;

}

 

相关内容