雙鏈表(Double Linked List)代碼實現(xiàn)

雙鏈表:

雙向性:每個節(jié)點都包含兩個指針,一個指向前一個節(jié)點(prev),一個指向后一個節(jié)點(next)。插入和刪除操作比較靈活,時間復雜度為O(1)。但是隨機訪問的時間復雜度為O(n),需要從頭節(jié)點或者尾節(jié)點開始順序查找。適合于頻繁進行插入和刪除操作的場景

代碼:

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

// 定義鏈表節(jié)點結構
struct Node {
    int data;           // 數(shù)據(jù)域
    struct Node* prev;  // 指向前一個節(jié)點的指針
    struct Node* next;  // 指向下一個節(jié)點的指針
};

// 初始化雙鏈表
void initDoubleList(struct Node** head) {
    *head = NULL;  // 將頭指針置為空,表示鏈表為空
}

// 在雙鏈表末尾添加節(jié)點
void appendDoubleNode(struct Node** head, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;

    if (*head == NULL) {
        *head = newNode;
    } else {
        struct Node* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
        newNode->prev = temp;
    }
}

// 在雙鏈表指定位置插入節(jié)點
void insertDoubleNode(struct Node** head, int data, int position) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;

    if (position == 0) {
        newNode->next = *head;
        if (*head != NULL) {
            (*head)->prev = newNode;
        }
        *head = newNode;
    } else {
        struct Node* temp = *head;
        int count = 0;
        while (temp != NULL && count < position - 1) {
            temp = temp->next;
            count++;
        }
        if (temp == NULL) {
            printf("Invalid position\n");
            return;
        }
        newNode->prev = temp;
        newNode->next = temp->next;
        if (temp->next != NULL) {
            temp->next->prev = newNode;
        }
        temp->next = newNode;
    }
}

// 刪除雙鏈表指定位置的節(jié)點
void deleteDoubleNode(struct Node** head, int position) {
    if (*head == NULL) {
        printf("List is empty\n");
        return;
    }

    struct Node* temp = *head;
    if (position == 0) {
        *head = temp->next;
        if (*head != NULL) {
            (*head)->prev = NULL;
        }
        free(temp);
    } else {
        int count = 0;
        while (temp != NULL && count < position) {
            temp = temp->next;
            count++;
        }
        if (temp == NULL) {
            printf("Invalid position\n");
            return;
        }
        temp->prev->next = temp->next;
        if (temp->next != NULL) {
            temp->next->prev = temp->prev;
        }
        free(temp);
    }
}

// 打印雙鏈表
void printDoubleList(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

// 釋放雙鏈表內存
void freeDoubleList(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        struct Node* nextNode = temp->next;
        free(temp);
        temp = nextNode;
    }
}

int main() {
    struct Node* head;
    initDoubleList(&head);

    // 在雙鏈表末尾添加節(jié)點
    appendDoubleNode(&head, 10);
    appendDoubleNode(&head, 20);
    appendDoubleNode(&head, 30);
    appendDoubleNode(&head, 40);

    // 打印雙鏈表
    printf("Double List: ");
    printDoubleList(head);  // 輸出: 10 20 30 40

    // 在雙鏈表指定位置插入節(jié)點
    insertDoubleNode(&head, 25, 2);

    // 打印雙鏈表
    printf("Double List after insertion: ");
    printDoubleList(head);  // 輸出: 10 20 25 30 40

    // 刪除雙鏈表指定位置的節(jié)點
    deleteDoubleNode(&head, 3);

    // 打印雙鏈表
    printf("Double List after deletion: ");
    printDoubleList(head);  // 輸出: 10 20 25 40

    // 釋放雙鏈表內存
    freeDoubleList(head);

    return 0;
}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容