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;
};node

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

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

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;
}rem

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; 
}get

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;
}it

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;
}io

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;
}date

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;
}List

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);    
}next

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; }  

相關文章
相關標籤/搜索