91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

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

/*鏈表節點*/
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);

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

//求尾節點
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);

//刪除節點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;
}

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

//求尾節點
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);
}

//刪除節點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問一下細節

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

AI

嘉义县| 淮北市| 明星| 西吉县| 河东区| 张家界市| 象州县| 衡山县| 临桂县| 英山县| 衡水市| 革吉县| 公主岭市| 共和县| 洪湖市| 东辽县| 岚皋县| 巢湖市| 绥宁县| 三亚市| 濉溪县| 桓台县| 沈丘县| 理塘县| 东乡族自治县| 白朗县| 贵阳市| 大荔县| 竹北市| 上虞市| 乌兰察布市| 思茅市| 荥经县| 东方市| 神农架林区| 黑河市| 凤山市| 郴州市| 广昌县| 泸西县| 神池县|