溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言實現雙向鏈表(DoublyLinkedList)

發(fā)布時間:2020-07-21 22:32:11 來源:網絡 閱讀:1927 作者:暮色伊人 欄目:編程語言
#ifndef DOUBLY_LINKED_LIST
#define DOUBLY_LINKED_LIST
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>

/*鏈表節(jié)點*/
typedef struct DoublyLinkedListNodeS{
    struct DoublyLinkedListNodeS *prev,*next;
}DoublyLinkedListNode;

typedef struct DoublyLinkedList{
    DoublyLinkedListNode root;//sentinel list element, only &root, root.prev, and root.next are used
    int length;
}DoublyLinkedList;

//初始化雙向鏈表
DoublyLinkedList* DoublyLinkedList_Init();

//銷毀鏈表
void DoublyLinkedList_Destory(DoublyLinkedList* list);

//鏈表長度
int DoublyLinkedList_Len(DoublyLinkedList* list);

//求頭節(jié)點
DoublyLinkedListNode* DoublyLinkedList_Front(DoublyLinkedList* list);

//求尾節(jié)點
DoublyLinkedListNode* DoublyLinkedList_Back(DoublyLinkedList* list);

//在at位置后面插入e
DoublyLinkedListNode* DoublyLinkedList_InsertAfter(DoublyLinkedList* list, DoublyLinkedListNode* at,DoublyLinkedListNode* e);

//在at前面插入e,成功返回e
DoublyLinkedListNode* DoublyLinkedList_InsertBefore(DoublyLinkedList* list, DoublyLinkedListNode* at, DoublyLinkedListNode* e);

//在頭部插入e
DoublyLinkedListNode* DoublyLinkedList_PushFront(DoublyLinkedList* list, DoublyLinkedListNode* e);

//在尾部插入e
DoublyLinkedListNode* DoublyLinkedList_PushBack(DoublyLinkedList* list, DoublyLinkedListNode* e);

//刪除節(jié)點e,并返回被刪除的e
DoublyLinkedListNode* DoublyLinkedList_Remove(DoublyLinkedList* list, DoublyLinkedListNode* e);

//將e移動到頭部
void DoublyLinkedList_MoveFront(DoublyLinkedList* list, DoublyLinkedListNode* e);

//將e移動到尾部
void DoublyLinkedList_MoveBack(DoublyLinkedList* list, DoublyLinkedListNode* e);

#endif // DOUBLY_LINKED_LIST
#include "doublyLinkedList.h"

DoublyLinkedList* DoublyLinkedList_Init() {
    DoublyLinkedList *list = (DoublyLinkedList *)malloc(sizeof(DoublyLinkedList));
    if (list == NULL) {
        fprintf(stderr,"malloc DoublyLinkedList error.\n");
        return NULL;
    }
    list->length = 0;
    list->root.next = &list->root;
    list->root.prev = &list->root;
    return list;
}

//銷毀鏈表
void DoublyLinkedList_Destory(DoublyLinkedList* list) {
    if (list != NULL) {
        free(list);
        list = NULL;
    }
}

int DoublyLinkedList_Len(DoublyLinkedList* list) {
    if (list == NULL) {
        return 0;
    }
    return list->length;
}

//求頭節(jié)點
DoublyLinkedListNode* DoublyLinkedList_Front(DoublyLinkedList* list) {
    if (list == NULL || list->length == 0) {
        return 0;
    }
    return list->root.next;
}

//求尾節(jié)點
DoublyLinkedListNode* DoublyLinkedList_Back(DoublyLinkedList* list) {
    if (list == NULL || list->length == 0) {
        return 0;
    }
    return list->root.prev;
}

//在at后面插入e,成功返回e
DoublyLinkedListNode* DoublyLinkedList_InsertAfter(DoublyLinkedList* list, DoublyLinkedListNode* at, DoublyLinkedListNode* e) {
    if (list == NULL || at == NULL) {
        fprintf(stderr,"wrong argument\n");
        return NULL;
    }
    DoublyLinkedListNode* next = at->next;
    at->next = e;
    e->prev = at;
    e->next = next;
    next->prev = e;
    list->length++;
    return e;
}

//在at前面插入e,成功返回e
DoublyLinkedListNode* DoublyLinkedList_InsertBefore(DoublyLinkedList* list, DoublyLinkedListNode* at, DoublyLinkedListNode* e) {
    if (list == NULL || at == NULL) {
        fprintf(stderr, "wrong argument\n");
        return NULL;
    }
    return DoublyLinkedList_InsertAfter(list, at->prev, e);
}

//在頭部插入e,返回e
DoublyLinkedListNode* DoublyLinkedList_PushFront(DoublyLinkedList* list, DoublyLinkedListNode* e) {
    return DoublyLinkedList_InsertAfter(list,&list->root,e);
}

//在尾部插入e,返回e
DoublyLinkedListNode* DoublyLinkedList_PushBack(DoublyLinkedList* list, DoublyLinkedListNode* e) {
    return DoublyLinkedList_InsertBefore(list, &list->root, e);
}

//刪除節(jié)點e,并返回被刪除的e
DoublyLinkedListNode* DoublyLinkedList_Remove(DoublyLinkedList* list, DoublyLinkedListNode* e) {
    if (list == NULL || e == NULL) {
        return NULL;
    }

    e->prev->next = e->next;
    e->next->prev = e->prev;
    e->next = NULL;
    e->prev = NULL;
    list->length--;
    return e;
}

//將e移動到頭部
void DoublyLinkedList_MoveFront(DoublyLinkedList* list, DoublyLinkedListNode* e) {
    if(list == NULL || e == NULL){
        return;
    }

    DoublyLinkedList_InsertAfter(list, &list->root, DoublyLinkedList_Remove(list,e));
}

//將e移動到尾部
void DoublyLinkedList_MoveBack(DoublyLinkedList* list, DoublyLinkedListNode* e) {
    if (list == NULL || e == NULL) {
        return;
    }

    DoublyLinkedList_InsertBefore(list, &list->root, DoublyLinkedList_Remove(list, e));
}
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI