单向非循环链表:链表创建、节点插入、链表打印、节点长度计算、链表清空、链表销毁

/* 单向非循环链表:
    初始化
    前插入

    后插入
    打印
    链表长度
    清空
    销毁
*/
#include <stdio.h>
#include <stdlib.h>

#define itemType int

typedef struct node
{
    itemType data;
    struct node *pNext;
}Node;

/* 创建Head节点: 节点的pNext为NULL */
int initList(Node **ppN)
{
    Node *pNewNode = (Node *)malloc(sizeof(Node));
    if (NULL == pNewNode)
    {
        printf("initList fail\n");
        return -1;
    }

    printf("pNewNode address: %p\n", pNewNode);
    pNewNode->pNext = NULL;
    pNewNode->data = 0;

    *ppN = pNewNode;
    
    return 0;
}

/* 前向插入一个节点 */
int insertListHead(Node *pN, itemType data)
{
    Node *pNewNode = (Node *)malloc(sizeof(Node));
    if (NULL == pNewNode)
    {
        printf("insertListHead fail\n");
        return -1;
    }
    
    printf("pNewNode address: %p\n", pNewNode);
    pNewNode->pNext = NULL;
    pNewNode->data = data;
    
    pNewNode->pNext = pN->pNext;
    pN->pNext = pNewNode;

    return 0;
}

/* 后向插入一个节点 */
int insertListTail(Node *pN, itemType data)
{
    Node *pNewNode = (Node *)malloc(sizeof(Node));
    if (NULL == pNewNode)
    {
        printf("insertListTail fail\n");
        return -1;
    }
    
    printf("pNewNode address: %p\n", pNewNode);
    pNewNode->pNext = NULL;
    pNewNode->data = data;
    
    /* 查找最后一个节点 */
    while(NULL != pN->pNext)
    {
        pN = pN->pNext;
    }
    pN->pNext = pNewNode;

    return 0;
}

/* 链表打印 */
int printList(Node *pN)
{
    if (NULL == pN)
    {
        printf("printList is not exist\n");
        return -1;
    }
    
    if (NULL == pN->pNext)
    {
        printf("printList is NULL\n");
        return -1;
    }
    
    while(NULL != pN)
    {
        printf("Node address: %p, Node value: %3d, Node Next: %p\n", pN, pN->data, pN->pNext);
        pN = pN->pNext;
    }
    
    return 0;
}

/* 链表长度 */
int listLength(Node *pN)
{
    int len = 0;

    if (NULL == pN)
    {
        printf("listLength NULL\n");
        return 0;
    }
    
    while(NULL != pN->pNext)
    {
        len++;
        pN = pN->pNext;
    }
    
    return len;
}

/* 清空链表:保留Head节点,其它全部资源 释放 */
int emptyList(Node *pN)
{
    Node *pTempNode = NULL;

    if (NULL == pN)
    {
        printf("emptyList is NULL\n");
        return -1;
    }
    
    while (NULL != pN->pNext)
    {
        pTempNode = pN->pNext;
        pN->pNext = pTempNode->pNext;
        
        free(pTempNode);
    }
    pTempNode = NULL;

    return 0;
}

/* 销毁链表:释放所有节点,包括Head节点 */
int destoryList(Node **pN)
{
    emptyList(*pN);
    free(*pN);
    *pN = NULL;

    return 0;    
}

/* 测试入口 */
int main(void)
{
    Node *pHeadNode = NULL;
    initList(&pHeadNode);

    for (int i=0; i<20; i++)
    {
        insertListHead(pHeadNode, i+1);
        insertListTail(pHeadNode, i+101);
    }

    printf("listLength: %d\n", listLength(pHeadNode));
    printList(pHeadNode);

    emptyList(pHeadNode);
    destoryList(&pHeadNode);

    printList(pHeadNode);

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tedani/p/9984427.html