單向連結串列————遍歷、查詢、插入結點 (基於C語言實現)

Dazz_24發表於2024-06-14
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdbool.h>

// 指的是單向連結串列中的結點有效資料型別,使用者可以根據需要進行修改
typedef int DataType_t;

// 構造連結串列的結點,連結串列中所有結點的資料型別應該是相同的
typedef struct LinkedList
{
    DataType_t data;         // 結點的資料域
    struct LinkedList *next; // 結點的指標域

} LList_t;

// 建立一個空連結串列,空連結串列應該有一個頭結點,對連結串列進行初始化
LList_t *LList_Create(void)
{
    // 1.建立一個頭結點並對頭結點申請記憶體
    LList_t *Head = (LList_t *)calloc(1, sizeof(LList_t));
    if (NULL == Head)
    {
        perror("Calloc memory for Head is Failed");
        exit(-1);
    }

    // 2.對頭結點進行初始化,頭結點是不儲存有效內容的!!!
    Head->next = NULL;

    // 3.把頭結點的地址返回即可
    return Head;
}

// 建立新的結點,並對新結點進行初始化(資料域 + 指標域)
LList_t *LList_NewNode(DataType_t data)
{
    // 1.建立一個新結點並對新結點申請記憶體
    LList_t *New = (LList_t *)calloc(1, sizeof(LList_t));
    if (NULL == New)
    {
        perror("Calloc memory for NewNode is Failed");
        return NULL;
    }

    // 2.對新結點的資料域和指標域進行初始化
    New->data = data;
    New->next = NULL;

    return New;
}

// 指定插入
bool LList_Insert(LList_t *Head, DataType_t dest, DataType_t data)
{
    LList_t *Phead = Head;
    // 1.建立新的結點,並對新結點進行初始化
    LList_t *New = LList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    } // 定義p0為需增加的新節點

    if (NULL == Head->next)
    {
        printf("連結串列為空\n");
        return false;
    }

    while (Phead->data != dest)
    {
        if (Phead->next == NULL)
        {
            printf("未找到指定元素\n");
            return false;
        }
        Phead = Phead->next;
    }

    New->next = Phead->next;
    Phead->next = New;

    return true;
}

// 頭插
bool LList_HeadInsert(LList_t *Head, DataType_t data)
{
    // 1.建立新的結點,並對新結點進行初始化
    LList_t *New = LList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    }

    // 2.判斷連結串列是否為空,如果為空,則直接插入即可
    if (NULL == Head->next)
    {
        Head->next = New;
        return true;
    }

    // 3.如果連結串列為非空,則把新結點插入到連結串列的頭部
    New->next = Head->next;
    Head->next = New;

    return true;
}

// 尾插
bool LList_TailInsert(LList_t *Head, DataType_t data)
{
    LList_t *Phead = Head;

    // 1.建立新的結點,並對新結點進行初始化
    LList_t *New = LList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    }

    // 2.判斷連結串列是否為空,如果為空,則直接插入即可
    if (NULL == Head->next)
    {
        Head->next = New;
        return true;
    }

    // 3.迴圈找到連結串列的末尾結點
    while (Phead->next != NULL)
    {
        Phead = Phead->next;
    }

    // 4.把新結點插入到連結串列的尾部
    Phead->next = New;

    return true;
}

// 指定元素刪除
bool LList_DestDel(LList_t *Head, DataType_t dest)
{
    LList_t *Phead = Head;
    LList_t *Point;
    if (NULL == Head->next)
    {
        return false;
    }
    while (Phead && Phead->next->data != dest)
    { // 遍歷連結串列找到要刪除的數 或 Head為NULL
        Phead = Phead->next;
    }
    if (Phead != NULL)
    {
        Point = Phead->next;
        Phead->next = Phead->next->next;
        Point->next = NULL;
        free(Point);
        return true;
    }
    else
    {
        printf("未找到該數\n");
        return false;
    }
}
// 頭刪
bool LList_Headdelete(LList_t *Head)
{
    LList_t *Phead = Head->next;
    // 2.判斷連結串列是否為空,如果為空,則直接退出即可
    if (NULL == Head->next)
    {
        printf("連結串列為空\n");
        return false;
    }
    Head->next = Phead->next;
    Phead->next = NULL;
    free(Phead);
}

// 尾刪
bool LList_Taildelete(LList_t *Head)
{
    LList_t *Phead = Head->next;
    LList_t *Phead_prev = Head;
    // 2.判斷連結串列是否為空,如果為空,則直接退出即可
    if (NULL == Head->next)
    {
        printf("連結串列為空\n");
        return false;
    }
    while (Phead->next != NULL)
    {
        Phead_prev = Phead_prev->next;
        Phead = Phead->next;
    }
    Phead_prev->next = NULL;
    free(Phead);
}

// 遍歷連結串列
bool LList_Print(LList_t *Head)
{
    // 對單向迴圈連結串列的頭結點的地址進行備份
    LList_t *Phead = Head;

    // 判斷當前連結串列是否為空,為空則直接退出
    if (Head->next == Head)
    {
        printf("current linkeflist is empty!\n");
        return false;
    }

    // 從首結點開始遍歷
    while (Phead->next)
    {
        // 把頭結點的直接後繼作為新的頭結點
        Phead = Phead->next;

        // 輸出頭結點的直接後繼的資料域
        printf("data = %d\n", Phead->data);

        // 判斷是否到達尾結點,尾結點的next指標是指向首結點的地址
        if (Phead->next == NULL)
        {
            break;
        }
    }

    return true;
}

int main(int argc, char const *argv[])
{

    // 1.建立順序表
    LList_t *Manager = LList_Create();

    // 2.向順序表中的尾部插入新元素
    // printf("*********************************尾插********************************\n");
    LList_TailInsert(Manager, 5);
    LList_TailInsert(Manager, 2);
    LList_TailInsert(Manager, 1);
    LList_TailInsert(Manager, 4);
    LList_TailInsert(Manager, 6);

    // //3.遍歷順序表
    // LList_Print(Manager); // -- 5 2 1 4 6
    // printf("\n");
    //  4.向順序表中的頭部插入新元素
    //  printf("*********************************頭插********************************\n");
    LList_HeadInsert(Manager, 9);
    LList_HeadInsert(Manager, 7);
    LList_HeadInsert(Manager, 8);
    LList_HeadInsert(Manager, 0);
    LList_HeadInsert(Manager, 10);

    // //5.遍歷順序表
    // LList_Print(Manager); // --10 0 8 7 9 5 2 1 4 6
    // printf("\n");
    // 6.刪除順序表的元素
    // printf("*********************************頭刪********************************\n");
    LList_Headdelete(Manager);
    LList_Headdelete(Manager);
    LList_Headdelete(Manager);
    LList_Headdelete(Manager);
    LList_Headdelete(Manager);

    // LList_Print(Manager); // --5  2  1  4  6

    // 7.指定插入
    // printf("*********************************指定插入********************************\n");
    LList_Insert(Manager, 5, 1);
    LList_Insert(Manager, 2, 3);
    LList_Insert(Manager, 6, 7);

    // 8.遍歷順序表
    // LList_Print(Manager); // --5 1 2 3 1 4 6 7
    // printf("\n");

    // 9.指定刪除
    // printf("*********************************指定刪除********************************\n");
    LList_DestDel(Manager, 5);
    LList_DestDel(Manager, 7);
    LList_DestDel(Manager, 3);

    // 10.遍歷順序表
    // LList_Print(Manager); // -- 1 2 1 4 6
    // printf("\n");

    // 11.尾部刪除
    // printf("*********************************尾刪********************************\n");
    LList_Taildelete(Manager);
    LList_Taildelete(Manager);
    LList_Taildelete(Manager);
    LList_Taildelete(Manager);

    // 12.遍歷順序表
    LList_Print(Manager); // --1
    // printf("\n");
}

相關文章