单链表的基本算法1---C语言实现

LinkList.h

#ifndef __LINK_LIST_H__
#define __LINK_LIST_H__
#include
#include
#include
typedef int DataType;//定义数据类型
typedef struct Node
{
    DataType data;
    struct Node* pNext;
}Node, *PNode;
//初始化链表
void InitLinkList(PNode* pHead);
//给链表创建新结点
PNode BuyNode(DataType x);
//正向打印链表
void PrintLinkList(PNode pHead);
//逆向打印链表(递归打印)
void PrintLinkListFormpTailNode(PNode pHead);
//给链表尾插
void PushBack(PNode* pHead, DataType x);
//给链表尾删
void PopBack(PNode* pHead);
//给链表头插
void PushFront(PNode* pHead, DataType x);
//给链表头删
void PopFront(PNode* pHead);
// 求链表中节点的个数 
size_t Size(PNode pHead);
// 正向销毁单链表 
void DestroyList(PNode* pHead);
// 逆向销毁链表(递归删除)
void DestroyLinkListpTailNode(PNode*pHead);
#endif//__LINK_LIST_H__

LinkList.c

#include"LinkList.h"
//初始化链表
void InitLinkList(PNode* pHead)
{
    assert(*pHead);
    (*pHead) = NULL;
}
//给链表创建新结点
PNode BuyNode(DataType x)
{
    PNode pCur = (PNode)malloc(sizeof(Node));
    if (NULL == pCur)
        return NULL;
    else
    {
        pCur->data = x;
        pCur->pNext = NULL;
        return pCur;
    }
}
//正向打印链表
void PrintLinkList(PNode pHead)
{
    if (NULL == pHead)
        printf("null\n");
    else
    {
        while (pHead)
        {
            printf("%d--->", pHead->data);
            pHead = pHead->pNext;
        }
        printf("NULL\n");
    }
}
//逆向打印链表(使用递归)
void PrintLinkListFormpTailNode(PNode pHead)
{
        if (pHead!=NULL)
        {
            PrintLinkListFormpTailNode(pHead->pNext);
            printf("%d---", pHead->data);
        }
}
//逆向打印链表时,尾节点后面没有 "NULL",例如:3-->2-->1-->
  //所以封装一个函数,给尾节点加NULL,就变成3-->2-->1-->NULL
void PrintLinkListFormpTailNode2(PNode pHead)
{
    PrintLinkListFormpTailNode(pHead);
    printf("NULL\n");
}
//给链表尾插
void PushBack(PNode* pHead, DataType x)//尾插函数
{
    PNode pTail = *pHead;
    PNode pCur = BuyNode(x);
    if (NULL == (*pHead))
        *pHead = pCur;
    else
    {
        while (pTail->pNext)
        {
            pTail = pTail->pNext;
        }
        pTail->pNext = pCur;
    }
}
//给链表尾删
void PopBack(PNode* pHead)
{
    assert(*pHead);
    PNode pTail = *pHead;
    PNode pCur = NULL;
    if (NULL == (*pHead)->pNext)
    {
        free(*pHead);
        (*pHead) = NULL;
    }
    else
    {
        while (pTail->pNext->pNext)
        {
            pTail = pTail->pNext;
        }
        pTail->pNext = pCur;
        free(pCur);
        pCur = NULL;
    }
}
//给链表头插
void PushFront(PNode* pHead, DataType x)
{
    PNode pCur = BuyNode(x);
    if (NULL == (*pHead))
        *pHead = pCur;
    else
    {
        pCur->pNext = *pHead;
        *pHead = pCur;
    }
}
//给链表头删
void PopFront(PNode* pHead)
{
    PNode pCur = NULL;
    assert(*pHead);
    if (NULL == (*pHead))
        ;
    else
    {
        pCur = *pHead;
        *pHead = pCur->pNext;
        free(pCur);
        pCur = NULL;
    }
}
//求链表中节点的个数 
size_t Size(PNode pHead)
{
    assert(pHead);
    int count = 0;
    PNode pTail = pHead;
    while (pTail)
    {
        count++;
        pTail = pTail->pNext;
    }
    return count;
}
// 正向销毁单链表 
void DestroyList(PNode* pHead)
{
    PNode pCur = NULL;
    PNode pTail = NULL;
    if (NULL == (*pHead))
        return;
    pCur = *pHead;
    while (pCur)
    {
        pTail = pCur->pNext;
        free(pCur);
        pCur = pTail;
    }
    *pHead = NULL;
}
// 逆向销毁链表(递归法销毁)
void DestroyLinkListpTailNode(PNode*pHead)
{
    assert(pHead);
    if (*pHead)
    {
        DestroyLinkListpTailNode(&((*pHead)->pNext));//参数是二级指针,需要传指针
        free(*pHead);
        (*pHead) = NULL;
    }
}

test.c

#include"LinkList.h"
void test()
{
    PNode pHead;
    InitLinkList(&pHead);
    PushBack(&pHead, 1);//尾插
    PushBack(&pHead, 2);//尾插
    PushBack(&pHead, 3);//尾插
    PushBack(&pHead, 4);//尾插
    PushBack(&pHead, 5);//尾插
    PushBack(&pHead, 6);//尾插
    PrintLinkListFormpTailNode(pHead);//逆向打印链表
    PrintLinkListFormpTailNode2(pHead);//封装成的逆向打印函数
    PrintLinkList(pHead);//正向打印链表
    PopBack(&pHead);//尾删
    PopBack(&pHead);//尾删
    PopBack(&pHead);//尾删
    PopBack(&pHead);//尾删
    PopBack(&pHead);//尾删
    PopBack(&pHead);//尾删
    PrintLinkList(pHead);//正向打印链表
    PushFront(&pHead, 2);//头插
    PushFront(&pHead, 4);//头插
    PushFront(&pHead, 6);//头插
    PushFront(&pHead, 8);//头插
    PushFront(&pHead, 10);//头插
    PushFront(&pHead, 9);//头插
    PrintLinkList(pHead);//正向打印链表
    PopFront(&pHead);//头删
    PopFront(&pHead);//头删
    PopFront(&pHead);//头删
    PopFront(&pHead);//头删
    PopFront(&pHead);//头删
    PopFront(&pHead);//头删
    PrintLinkList(pHead);//正向打印链表
    int num=Size(pHead);// 求链表中节点的个数
    printf("%d\n", num);
    DestroyList(&pHead);// 正向销毁单链表 
    PrintLinkList(pHead);//正向打印链表
    DestroyLinkListpTailNode(&pHead);// 逆向销毁链表
    PrintLinkList(pHead);//正向打印链表
}
int main()
{
    test();
    system("pause");
    return 0;
}

你可能感兴趣的