C语言 单链表 Singly Linked List

#include <stdio.h>
#include <stdlib.h>
#define bool int
#define true 1
#define false 0
struct List
{
    struct Node* ptrNode;
    int nLen;
};

struct Node
{
    struct Node* next;
    int nNum;
};

struct List* createList()
{
    struct List* ptrList = (struct List*)malloc(sizeof(struct List));
    ptrList->nLen = 0;
    ptrList->ptrNode = NULL;
    return ptrList;
}

void destroyList(struct List* ptrList)
{
    if (ptrList == NULL)
        return;
    struct Node* ptrCurNode = ptrList->ptrNode;
    if (ptrCurNode == NULL)
    {
        free(ptrList);//List was created but no node added yet
        return;
    }
    while (ptrCurNode)
    {
        struct Node* ptrNextNode = ptrCurNode->next;//get next code and ready to free current node
        if (ptrNextNode == NULL)
        {
            free(ptrCurNode);//only one node or free all other nodes yet
            break;
        }
        free(ptrCurNode);//free current node
        ptrCurNode = NULL;
        ptrCurNode = ptrNextNode;//move current node to next,make next code be current node
    }
    free(ptrList);
    ptrList = NULL;
}

void clearList(struct List* ptrList)
{
    if (ptrList == NULL)
        return;
    if (ptrList->nLen == 0)
        return;
    struct Node* ptrCurNode = ptrList->ptrNode;
    struct Node* ptrNextNode = NULL;
    while (ptrCurNode)
    {
        ptrNextNode = ptrCurNode->next;
        free(ptrCurNode);
        ptrCurNode = NULL;
        ptrCurNode = ptrNextNode;
    }
    ptrList->nLen = 0;
    ptrList->ptrNode = NULL; 
}

bool addEleToList(struct List* ptrList, int nNum)
{
    bool bRet = false;
    if (ptrList == NULL)
        return bRet;
    struct Node* ptrCurNode = ptrList->ptrNode;
    if (ptrCurNode == NULL)
    {//no ndoe had been added before,list is empty,so create and add a node
        struct Node* ptrNode = (struct Node*)malloc(sizeof(struct Node));
        ptrNode->next = NULL;
        ptrNode->nNum = nNum;
        ptrList->ptrNode = ptrNode;
        ptrList->nLen += 1;
        bRet = true;
        return bRet;
    }
    while (ptrCurNode)
    {//exsit nodes in list, move to tail first
        struct Node* ptrNextNode = ptrCurNode->next;//get next code and ready to free current node
        if (ptrNextNode == NULL)
            break;
        ptrCurNode = ptrNextNode;
    }
    //create and add new node to tail and increase the lentgh of list
    struct Node *ptrNode = (struct Node *)malloc(sizeof(struct Node));
    ptrNode->next = NULL;
    ptrNode->nNum = nNum;
    ptrCurNode->next = ptrNode;
    ptrList->nLen += 1;
    bRet = true;
    return bRet;
}

bool removeEleInListByIndex(struct List* ptrList, int nIndex)
{
    bool bRet = false;
    if (ptrList == NULL)
        return bRet;
    if(nIndex + 1 > ptrList->nLen)
        return bRet;
    struct Node* ptrCurNode = ptrList->ptrNode;
    struct Node* ptrPreNode = NULL;
    int nCount = 0;
    while (ptrCurNode)
    {
        if (nIndex == nCount)
        {
            struct Node* ptrNextNode = ptrCurNode->next;
            free(ptrCurNode);
            ptrCurNode = NULL;
            if (ptrPreNode == NULL)
            {
                ptrList->ptrNode = ptrNextNode;
            }
            else
            {
                ptrPreNode->next = ptrNextNode;
            }
            ptrList->nLen -= 1;
            bRet = true;
            break;
        }
        ptrPreNode = ptrCurNode;
        ptrCurNode = ptrCurNode->next;
        nCount++;
    }
    
    return bRet;
}

struct Node* findEleInList(struct List* ptrList, int nKey)
{
    if (ptrList == NULL)
        return NULL;
    if (ptrList->nLen == 0)
        return NULL;
    struct Node* ptrCurNode = ptrList->ptrNode;
    while (ptrCurNode)
    {
        if (ptrCurNode->nNum == nKey)
        {
            return ptrCurNode;
        }
        ptrCurNode = ptrCurNode->next;
    }
    
    return NULL;
}

bool removeEqulaEleInList(struct List* ptrList, int nKey)
{
    bool bRet = false;
    if (ptrList == NULL)
        return bRet;
    if (ptrList->nLen == 0)
        return bRet;
    struct Node* ptrCurNode = ptrList->ptrNode;
    struct Node* ptrPreNode = NULL;
    while (ptrCurNode)
    {
        if (ptrCurNode->nNum == nKey)
        {
            bRet = true;
            struct Node* ptrNextNode = ptrCurNode->next;//maybe NULL
            free(ptrCurNode);
            ptrCurNode = NULL;
            if (ptrPreNode == NULL)
            {
                ptrList->ptrNode = ptrNextNode;
            }
            else
            {
                ptrPreNode->next = ptrNextNode;//let pre-node point to next node
            }
            ptrCurNode = ptrNextNode;//update current node postion
            ptrList->nLen -= 1;
            continue;
        }
        ptrPreNode = ptrCurNode;
        ptrCurNode = ptrCurNode->next;
    }  
    
   return bRet;
}

void printList(struct List* ptrList)
{
    if (ptrList == NULL)
        return;
    struct Node* ptrCurNode = ptrList->ptrNode;
    while (ptrCurNode)
    {
        printf("%d\n", ptrCurNode->nNum);
        ptrCurNode = ptrCurNode->next;
    }
    printf("list's length=%d\n", ptrList->nLen);    
}

void main()
{
    struct List* ptrList = createList();
    addEleToList(ptrList, 10);
    addEleToList(ptrList, 11);
    addEleToList(ptrList, 12);
    printList(ptrList);
    printf("remove 12 in List\n");
    removeEqulaEleInList(ptrList, 12);
    printList(ptrList);
    printf("remove ele by index 1\n");
    removeEleInListByIndex(ptrList, 1);
    printList(ptrList);
    return;
}
 

转载于:https://my.oschina.net/u/2303126/blog/3083396

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值