日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

双链表的实现

發布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双链表的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

出自《算法精解:C語言描述》,dlist.c沒問題,但是在使用的demo上有些問題。

注意:原書有問題就是dlist_remove之后沒有free()

相對而言,redis那種風格的刪除節點更好,因為是在函數體內釋放節點的,而不是外面。關于二者的比較參考:說透一級指針和二級指以及(void**)&在雙鏈表中的應用

整體來說,這本書中的代碼還是很精彩的:

/*****************************************************************************
*                                                                            *
*  ------------------------------- dlist.h --------------------------------  *
*                                                                            *
*****************************************************************************/#ifndef DLIST_H
#define DLIST_H#include <stdlib.h>/*****************************************************************************
*                                                                            *
*  Define a structure for doubly-linked list elements.                       *
*                                                                            *
*****************************************************************************/typedef struct DListElmt_ {void               *data;
struct DListElmt_  *prev;
struct DListElmt_  *next;} DListElmt;/*****************************************************************************
*                                                                            *
*  Define a structure for doubly-linked lists.                               *
*                                                                            *
*****************************************************************************/typedef struct DList_ {int                size;int                (*match)(const void *key1, const void *key2);
void               (*destroy)(void *data);DListElmt          *head;
DListElmt          *tail;} DList;/*****************************************************************************
*                                                                            *
*  --------------------------- Public Interface ---------------------------  *
*                                                                            *
*****************************************************************************/void dlist_init(DList *list, void (*destroy)(void *data));void dlist_destroy(DList *list);int dlist_ins_next(DList *list, DListElmt *element, const void *data);int dlist_ins_prev(DList *list, DListElmt *element, const void *data);int dlist_remove(DList *list, DListElmt *element, void **data);#define dlist_size(list) ((list)->size)#define dlist_head(list) ((list)->head)#define dlist_tail(list) ((list)->tail)#define dlist_is_head(element) ((element)->prev == NULL ? 1 : 0)#define dlist_is_tail(element) ((element)->next == NULL ? 1 : 0)#define dlist_data(element) ((element)->data)#define dlist_next(element) ((element)->next)#define dlist_prev(element) ((element)->prev)#endif

/*****************************************************************************
*                                                                            *
*  ------------------------------- dlist.c --------------------------------  *
*                                                                            *
*****************************************************************************/#include <stdlib.h>
#include <string.h>#include "dlist.h"/*****************************************************************************
*                                                                            *
*  ------------------------------ dlist_init ------------------------------  *
*                                                                            *
*****************************************************************************/void dlist_init(DList *list, void (*destroy)(void *data)) {/*****************************************************************************
*                                                                            *
*  Initialize the list.                                                      *
*                                                                            *
*****************************************************************************/list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;return;}/*****************************************************************************
*                                                                            *
*  ---------------------------- dlist_destroy -----------------------------  *
*                                                                            *
*****************************************************************************/void dlist_destroy(DList *list) {void               *data;/*****************************************************************************
*                                                                            *
*  Remove each element.                                                      *
*                                                                            *
*****************************************************************************/while (dlist_size(list) > 0) {if (dlist_remove(list, dlist_tail(list), (void **)&data) == 0 && list->destroy != NULL) {/************************************************************************                                                                      **  Call a user-defined function to free dynamically allocated data.    **                                                                      ************************************************************************/list->destroy(data);}}/*****************************************************************************
*                                                                            *
*  No operations are allowed now, but clear the structure as a precaution.   *
*                                                                            *
*****************************************************************************/memset(list, 0, sizeof(DList));return;}/*****************************************************************************
*                                                                            *
*  ---------------------------- dlist_ins_next ----------------------------  *
*                                                                            *
*****************************************************************************/int dlist_ins_next(DList *list, DListElmt *element, const void *data) {DListElmt          *new_element;/*****************************************************************************
*                                                                            *
*  Do not allow a NULL element unless the list is empty.                     *
*                                                                            *
*****************************************************************************/if (element == NULL && dlist_size(list) != 0)return -1;/*****************************************************************************
*                                                                            *
*  Allocate storage for the element.                                         *
*                                                                            *
*****************************************************************************/if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)return -1;/*****************************************************************************
*                                                                            *
*  Insert the new element into the list.                                     *
*                                                                            *
*****************************************************************************/new_element->data = (void *)data;if (dlist_size(list) == 0) {/***************************************************************************                                                                         **  Handle insertion when the list is empty.                               **                                                                         ***************************************************************************/list->head = new_element;list->head->prev = NULL;list->head->next = NULL;list->tail = new_element;}else {/***************************************************************************                                                                         **  Handle insertion when the list is not empty.                           **                                                                         ***************************************************************************/new_element->next = element->next;new_element->prev = element;if (element->next == NULL)list->tail = new_element;elseelement->next->prev = new_element;element->next = new_element;}/*****************************************************************************
*                                                                            *
*  Adjust the size of the list to account for the inserted element.          *
*                                                                            *
*****************************************************************************/list->size++;return 0;}/*****************************************************************************
*                                                                            *
*  ---------------------------- dlist_ins_prev ----------------------------  *
*                                                                            *
*****************************************************************************/int dlist_ins_prev(DList *list, DListElmt *element, const void *data) {DListElmt          *new_element;/*****************************************************************************
*                                                                            *
*  Do not allow a NULL element unless the list is empty.                     *
*                                                                            *
*****************************************************************************/if (element == NULL && dlist_size(list) != 0)return -1;/*****************************************************************************
*                                                                            *
*  Allocate storage to be managed by the abstract data type.                 *
*                                                                            *
*****************************************************************************/if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)return -1;/*****************************************************************************
*                                                                            *
*  Insert the new element into the list.                                     *
*                                                                            *
*****************************************************************************/new_element->data = (void *)data;if (dlist_size(list) == 0) {/***************************************************************************                                                                         **  Handle insertion when the list is empty.                               **                                                                         ***************************************************************************/list->head = new_element;list->head->prev = NULL;list->head->next = NULL;list->tail = new_element;}else {/***************************************************************************                                                                         **  Handle insertion when the list is not empty.                           **                                                                         ***************************************************************************/new_element->next = element; new_element->prev = element->prev;if (element->prev == NULL)list->head = new_element;elseelement->prev->next = new_element;element->prev = new_element;}/*****************************************************************************
*                                                                            *
*  Adjust the size of the list to account for the new element.               *
*                                                                            *
*****************************************************************************/list->size++;return 0;}/*****************************************************************************
*                                                                            *
*  ----------------------------- dlist_remove -----------------------------  *
*                                                                            *
*****************************************************************************/int dlist_remove(DList *list, DListElmt *element, void **data) {/*****************************************************************************
*                                                                            *
*  Do not allow a NULL element or removal from an empty list.                *
*                                                                            *
*****************************************************************************/if (element == NULL || dlist_size(list) == 0)return -1;/*****************************************************************************
*                                                                            *
*  Remove the element from the list.                                         *
*                                                                            *
*****************************************************************************/*data = element->data;if (element == list->head) {/***************************************************************************                                                                         **  Handle removal from the head of the list.                              **                                                                         ***************************************************************************/list->head = element->next;if (list->head == NULL)list->tail = NULL;elseelement->next->prev = NULL;}else {/***************************************************************************                                                                         **  Handle removal from other than the head of the list.                   **                                                                         ***************************************************************************/element->prev->next = element->next;if (element->next == NULL)list->tail = element->prev;elseelement->next->prev = element->prev;}/*****************************************************************************
*                                                                            *
*  Free the storage allocated by the abstract data type.                     *
*                                                                            *
*****************************************************************************/free(element);/*****************************************************************************
*                                                                            *
*  Adjust the size of the list to account for the removed element.           *
*                                                                            *
*****************************************************************************/list->size--;return 0;}

/*****************************************************************************
*                                                                            *
*  ex-1.c                                                                    *
*  ======                                                                    *
*                                                                            *
*  Description: Illustrates using a doubly-linked list (see Chapter 5).      *
*                                                                            *
*****************************************************************************/#include <stdio.h>
#include <stdlib.h>#include "dlist.h"/*****************************************************************************
*                                                                            *
*  ------------------------------ print_list ------------------------------  *
*                                                                            *
*****************************************************************************/void print_list(const DList *list) {DListElmt          *element;int                *data,i;/*****************************************************************************
*                                                                            *
*  Display the doubly-linked list.                                           *
*                                                                            *
*****************************************************************************/fprintf(stdout, "List size is %d\n", dlist_size(list));i = 0;
element = dlist_head(list);while (1) {data = dlist_data(element);fprintf(stdout, "list[%03d]=%03d\n", i, *data);i++;if (dlist_is_tail(element))break;elseelement = dlist_next(element);}return;}/*****************************************************************************
*                                                                            *
*  --------------------------------- main ---------------------------------  *
*                                                                            *
*****************************************************************************/int main(int argc, char **argv) {DList              list;
DListElmt          *element;int                *data,i;/*****************************************************************************
*                                                                            *
*  Initialize the doubly-linked list.                                        *
*                                                                            *
*****************************************************************************/dlist_init(&list, free);/*****************************************************************************
*                                                                            *
*  Perform some doubly-linked list operations.                               *
*                                                                            *
*****************************************************************************/element = dlist_head(&list);for (i = 10; i > 0; i--) {if ((data = (int *)malloc(sizeof(int))) == NULL)return 1;*data = i;if (dlist_ins_prev(&list, dlist_head(&list), data) != 0)return 1;}print_list(&list);element = dlist_head(&list);for (i = 0; i < 8; i++)element = dlist_next(element);data = dlist_data(element);
fprintf(stdout, "Removing an element after the one containing %03d\n", *data);if (dlist_remove(&list, element, (void **)&data) != 0)return 1;print_list(&list);fprintf(stdout, "Inserting 011 at the tail of the list\n");*data = 11;
if (dlist_ins_next(&list, dlist_tail(&list), data) != 0)return 1;print_list(&list);fprintf(stdout, "Removing an element at the tail of the list\n");element = dlist_tail(&list);
if (dlist_remove(&list, element, (void **)&data) != 0)return 1;print_list(&list);fprintf(stdout, "Inserting 012 just before the tail of the list\n");*data = 12;
if (dlist_ins_prev(&list, dlist_tail(&list), data) != 0)return 1;print_list(&list);fprintf(stdout, "Iterating and removing the fourth element\n");element = dlist_head(&list);
element = dlist_next(element);
element = dlist_prev(element);
element = dlist_next(element);
element = dlist_prev(element);
element = dlist_next(element);
element = dlist_next(element);
element = dlist_next(element);if (dlist_remove(&list, element, (void **)&data) != 0)return 1;print_list(&list);fprintf(stdout, "Inserting 013 before the first element\n");*data = 13;
if (dlist_ins_prev(&list, dlist_head(&list), data) != 0)return 1;print_list(&list);fprintf(stdout, "Removing an element at the head of the list\n");if (dlist_remove(&list, dlist_head(&list), (void **)&data) != 0)return 1;print_list(&list);fprintf(stdout, "Inserting 014 just after the head of the list\n");
*data = 14;if (dlist_ins_next(&list, dlist_head(&list), data) != 0)return 1;print_list(&list);fprintf(stdout, "Inserting 015 two elements after the head of the list\n");if ((data = (int *)malloc(sizeof(int))) == NULL)return 1;*data = 15;
element = dlist_head(&list);
element = dlist_next(element);if (dlist_ins_next(&list, element, data) != 0)return 1;print_list(&list);i = dlist_is_head(dlist_head(&list));
fprintf(stdout, "Testing dlist_is_head...Value=%d (1=OK)\n", i);
i = dlist_is_head(dlist_tail(&list));
fprintf(stdout, "Testing dlist_is_head...Value=%d (0=OK)\n", i);
i = dlist_is_tail(dlist_tail(&list));
fprintf(stdout, "Testing dlist_is_tail...Value=%d (1=OK)\n", i);
i = dlist_is_tail(dlist_head(&list));
fprintf(stdout, "Testing dlist_is_tail...Value=%d (0=OK)\n", i);/*****************************************************************************
*                                                                            *
*  Destroy the doubly-linked list.                                           *
*                                                                            *
*****************************************************************************/fprintf(stdout, "Destroying the list\n");
dlist_destroy(&list);return 0;}

實現流程


下面是一個網友寫的demo,修正了原demo中remove后沒有free的問題,可以參考:

/** filename:main.c* author:zhm* date:2012-12-08*/#include<stdio.h>
#include<stdlib.h>
#include<string.h>#include "dlist.h"typedef struct Cuboid_
{int length;int width;int height;
}Cuboid;Cuboid *cube_instance(const int length, const int width, const int height)
{Cuboid *cb_ptr;cb_ptr = (Cuboid *)malloc(sizeof(Cuboid));if( cb_ptr == NULL )return NULL;cb_ptr->length = length;cb_ptr->width = width;cb_ptr->height = height;return cb_ptr;
}/*destroy */
void destroy(void *data)
{free(data);return;
}/* main */
int main(int argc, char **argv)
{int i;DList dlist_exp;DListElmt *p = NULL;Cuboid *cb1_ptr, *cb2_ptr, *cb3_ptr, *cb4_ptr, *cb5_ptr;Cuboid *cb_ptr;//cb1_ptr ~ cb5_ptr are the data of the 5 elements.cb1_ptr = cube_instance(1,2,3);cb2_ptr = cube_instance(6,10,8);cb3_ptr = cube_instance(5,20,30);cb4_ptr = cube_instance(17,100,25);cb5_ptr = cube_instance(3,6,9);//init the double linked list.dlist_init(&dlist_exp, destroy);//insert the 5 elements into the dlist dlist_ins_next(&dlist_exp, NULL, (void *)cb1_ptr );  //insert data:cb1p = dlist_head(&dlist_exp); //get the address of the first elementdlist_ins_next(&dlist_exp, p , (void *)cb2_ptr );   //insert data:cb2    cb1- cb2p = dlist_next(p);          //pointer to the element containing the data cb2.dlist_ins_prev(&dlist_exp, p, (void *)cb3_ptr );    //insert data:cb3   cb1- cb3- cb2dlist_ins_prev(&dlist_exp, p, (void *)cb4_ptr );    //insert data:cb4   cb1- cb3- cb4- cb2p = dlist_prev(p);          //pointer to the element conatining the data cb4.dlist_ins_prev(&dlist_exp, p, (void *)cb5_ptr );      //insert data:cb5   cb1- cb3- cb5- cb4- cb2//now the sequence is: head->cb1->cb3->cb5->cb4->cb2printf("traverse and print:\n");p = dlist_head(&dlist_exp); //get the head element;for( i = 0; i < dlist_size(&dlist_exp); i++ ){cb_ptr = (Cuboid *)dlist_data(p); //get the element's data, every data is a Cuboid's pointer.printf("i = %d: ",i);printf("length = %d, width = %d, height = %d\n",cb_ptr->length,cb_ptr->width,cb_ptr->height);p = dlist_next(p); //pointer to next element;}//we'll remove the third element:that's containing the data of cb5(3,6,9)p = dlist_head(&dlist_exp);p = dlist_next(p);p = dlist_next(p);dlist_remove(&dlist_exp, p, (void **)&cb_ptr);printf("the data of the third element: length = %d, width = %d, height = %d\n",cb_ptr->length,cb_ptr->width,cb_ptr->height);destroy(cb_ptr); //free the memory//now we'll show you the remained elements,the sequence is :(head)cb1->cb3->cb4->cb2(tail)printf("after remove the third elements:\n");p = dlist_head(&dlist_exp);for(i = 0; i < dlist_size(&dlist_exp); i++ ){cb_ptr = (Cuboid *)dlist_data(p);printf("i = %d: ",i);printf("length = %d, width = %d, height = %d\n",cb_ptr->length,cb_ptr->width,cb_ptr->height);p = dlist_next(p);}//destroy the double linked listdlist_destroy(&dlist_exp);printf("after destroy the list,its size = %d\n", dlist_size(&dlist_exp));return 0;
}



總結

以上是生活随笔為你收集整理的双链表的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。