雙向迴圈連結串列基本操作的實現(C語言)

li27z發表於2017-04-10

“Talk is cheap, show you the code”.

#include <stdio.h>
#include <stdlib.h>

// 雙向迴圈連結串列的節點結構
typedef struct Node {
    int val;
    struct Node *prev;
    struct Node *next;
} ListNode;

// 顯示選單
void DisplayMenu()
{
    printf("*************************\n");
    printf("*[1]建立一個雙向迴圈連結串列*\n");
    printf("*[2]增加節點            *\n");
    printf("*[3]刪除節點            *\n");
    printf("*[4]查詢資料            *\n");
    printf("*[5]修改資料            *\n");
    printf("*[6]顯示所有資料        *\n");
    printf("*[7]退出                *\n");
    printf("*************************\n");
    printf("                         \n");
    printf("請輸入您的選擇(數字1-7):");
}

// 建立一個雙向迴圈連結串列(帶頭節點)
ListNode* CreateList()
{
    int i;
    int length = 0;
    int data = 0;
    ListNode *pNew = NULL;
    ListNode *pTail = NULL;

    ListNode *pHead = (ListNode*)malloc(sizeof(ListNode));
    if(pHead == NULL)
    {
        printf("記憶體分配失敗!\n");
        exit(0);
    }
    pHead->val = 0;
    pHead->prev = pHead;
    pHead->next = pHead;
    pTail = pHead;

    printf("請輸入連結串列的長度:");
    scanf("%d", &length);
    for(i = 0; i < length; ++i)
    {
        pNew = (ListNode*)malloc(sizeof(ListNode));
        if(pNew == NULL)
        {
            printf("記憶體分配失敗!\n");
            exit(0);
        }

        printf("請輸入第%d個節點的資料值:", (i + 1));
        scanf("%d", &data);

        pNew->val = data;
        pNew->prev = pTail;
        pNew->next = pHead;
        pTail->next = pNew;
        pHead->prev = pNew;
        pTail = pNew;
    }
    printf("連結串列建立成功!資料如下:\n");
    return pHead;
}

// 獲取連結串列的長度
int GetLength(ListNode *pHead)
{
    int length = 0;
    ListNode *pNode = pHead->next;
    while(pNode != pHead)
    {
        length++;
        pNode = pNode->next;
    }       
    return length;
}

// 增加節點(在pos所在的元素之前新增)
int AddNode(ListNode *pHead, int pos, int data)
{
    ListNode *pNew = NULL;
    if(pos > 0 && pos < (GetLength(pHead) + 2))
    {
        pNew = (ListNode*)malloc(sizeof(ListNode));
        if(pNew == NULL)
        {
            printf("記憶體分配失敗!\n");
            exit(0);
        }

        while(--pos)
        {
            pHead = pHead->next;
        }

        pNew->val = data;
        pNew->prev = pHead;
        pNew->next = pHead->next;
        pHead->next->prev = pNew;
        pHead->next = pNew;

        return 1;
    }
    return 0;
}

// 刪除資料
int DelNode(ListNode *pHead, int pos)
{
    if(pos > 0 && pos <= GetLength(pHead))
    {
        while(pos--)
        {
            pHead = pHead->next;
        }

        pHead->prev->next = pHead->next;
        pHead->next->prev = pHead->prev;

        return 1;
    }
    return 0;
}

// 修改資料
int ModNode(ListNode *pHead, int pos, int data)
{
    ListNode *pNode = pHead;
    if(pos > 0 && pos <= GetLength(pHead))
    {
        while(pos--)
        {
            pNode = pNode->next;
        }

        pNode->val = data;

        return 1;
    }
    return 0;
}

// 查詢資料
int FindNode(ListNode *pHead, int data)
{
    ListNode *pNode = pHead->next;
    int pos = 0;
    while(pNode != pHead)
    {
        pos++;
        if(pNode->val == data)
        {
            printf("資料%d所在位置為:%d\n", data, pos);
            return 1;
        }
        pNode = pNode->next;
    }
    return 0;
}

// 顯示所有資料
void DisplayList(ListNode *pHead)
{
    ListNode *pNode = pHead->next;
    printf("連結串列中的資料如下:");
    while(pNode != pHead)
    {
        printf("%d ", pNode->val);
        pNode = pNode->next;
    }
    printf("\n");
}

// 刪除整個連結串列,並釋放記憶體空間
void FreeMemory(ListNode **pHead)
{
    ListNode *pNode = NULL;
    while(*pHead != NULL)
    {
        pNode = (*pHead)->next->next;
        if((*pHead)->next == *pHead)
        {
            free(*pHead);
            *pHead = NULL;
        }
        else
        {
            free((*pHead)->next);
            (*pHead)->next = NULL;
            (*pHead)->next = pNode;
            pNode->prev = (*pHead);
        }
    }
}

int main()
{
    int opt;
    int pos;
    int data;
    int bool = 0;
    DisplayMenu();
    ListNode *pHead;
    while(1)
    {
        if(bool == 1)
            break;
        scanf("%d", &opt);
        switch(opt)
        {
            case 1:
                pHead = CreateList();
                DisplayList(pHead);
                break;
            case 2:
                printf("請輸入您想增加的節點的位置:\n");
                scanf("%d", &pos);
                printf("請輸入您想增加的節點的值:\n");
                scanf("%d", &data);
                if(AddNode(pHead, pos, data) == 1)
                {
                    printf("新增成功!新連結串列如下:\n");
                    DisplayList(pHead);
                }
                else
                {
                    printf("新增失敗!請再次操作!\n");
                }
                break;
            case 3:
                printf("請輸入您想刪除的節點的位置:\n");
                scanf("%d", &pos);
                if(DelNode(pHead, pos) == 1)
                {
                    printf("刪除成功!新連結串列如下:\n");
                    DisplayList(pHead);
                }
                else
                {
                    printf("刪除失敗!請再次操作!\n");
                }
                break;
            case 4:
                printf("請輸入您想查詢的節點的值:\n");
                scanf("%d", &data);
                if(FindNode(pHead, data) == 0)
                {
                    printf("查詢不到該資料!請再次操作!\n");
                }
                break;
            case 5:
                printf("請輸入您想修改的節點的位置:\n");
                scanf("%d", &pos);
                printf("請輸入您想修改節點的值為:\n");
                scanf("%d", &data);
                if(ModNode(pHead, pos, data) == 1)
                {
                    printf("修改成功!新連結串列如下:\n");
                    DisplayList(pHead);
                }
                else
                {
                    printf("修改失敗!請再次操作!\n");
                }
                break;
            case 6:
                DisplayList(pHead);
                break;
            case 7:
                FreeMemory(&pHead);
                bool = 1;
                break;
            default:
                printf("您的輸入有誤!請重新選擇!\n");
                break;
        }
    }
    return 0;
}

程式執行效果如下圖:
這裡寫圖片描述

相關文章