第5章

5.1 单链表

/*  -------------------------------- list.h --------------------------------  */
#ifndef LIST_H
#define LIST_H #include <stdlib.h> /* Define a structure for linked list elements.*/
typedef struct ListElmt_
{
void *data;
struct ListElmt_ *next;
} ListElmt; /* Define a structure for linked lists.*/
typedef struct List_
{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
ListElmt *head;
ListElmt *tail;
} List; /* --------------------------- Public Interface --------------------------- */
void list_init(List *list, void (*destroy)(void *data));//whl-?其destroy参数的用法?
void list_destroy(List *list);
int list_ins_next(List *list, ListElmt *element, const void *data);
int list_rem_next(List *list, ListElmt *element, void **data); #define list_size(list) ((list)->size)
#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
#define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
#define list_is_tail(element) ((element)->next == NULL ? 1 : 0)
#define list_data(element) ((element)->data)
#define list_next(element) ((element)->next) #endif
/*  -------------------------------- list.c --------------------------------  */
#include <stdlib.h>
#include <string.h>
#include "list.h"
/* ------------------------------- list_init ------------------------------ */
void list_init(List *list, void (*destroy)(void *data))//若链表包含不该释放的数据,则destroy应设置为NULL
{
/* Initialize the list. */
list->size = ;
list->destroy = destroy;//把函数指针成员destroy设置为定义的析构函数
list->head = NULL;
list->tail = NULL;
return;
}
/* ----------------------------- list_destroy ----------------------------- */
void list_destroy(List *list)//若传给list_init的参数destroy不为NULL,则移除链表中每个元素时都调用该函数一次
{
void *data;
/* Remove each element. */
while (list_size(list) > )
{
if (list_rem_next(list, NULL, (void **)&data) == && 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, , sizeof(List));
return;
}
/* ----------------------------- list_ins_next ---------------------------- */
int list_ins_next(List *list, ListElmt *element, const void *data) //在list指定的链表中element后面插入一个新元素
{
ListElmt *new_element;
/* Allocate storage for the element. */
if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
{
return -;
}
/* Insert the element into the list. */
//new_element->data = (void *)data;
new_element->data = (void *)data;
if (element == NULL) //若element设置为NULL,则新元素插入链表头部
{
/* Handle insertion at the head of the list. */
if (list_size(list) == )
list->tail = new_element;
new_element->next = list->head;
list->head = new_element;
}
else
{
/* Handle insertion somewhere other than at the head. */
if (element->next == NULL)
list->tail = new_element;
new_element->next = element->next;
element->next = new_element;
}
/* Adjust the size of the list to account for the inserted element. */
list->size++;
return ;
}
/* ----------------------------- list_rem_next ---------------------------- */
int list_rem_next(List *list, ListElmt *element, void **data)//移除由list指定的链表中element后的那个元素
{
ListElmt *old_element; /* Do not allow removal from an empty list. */
if (list_size(list) == )
return -; /* Remove the element from the list. */
if (element == NULL)//若element设置为NULL,则移除链表头部元素
{
/* Handle removal from the head of the list. */
*data = list->head->data;
old_element = list->head;
list->head = list->head->next;
if (list_size(list) == )
list->tail = NULL;
}
else {
/* Handle removal from somewhere other than the head. */
if (element->next == NULL)
return -;
*data = element->next->data;
old_element = element->next;
element->next = element->next->next;
if (element->next == NULL)
list->tail = element;
}
/* Free the storage allocated by the abstract data type. */
free(old_element);
/* Adjust the size of the list to account for the removed element. */
list->size--;
return ;
}

算法精解(C语言描述) 第5章 读书笔记的更多相关文章

  1. 机器学习|线性回归算法详解 (Python 语言描述)

    原文地址 ? 传送门 线性回归 线性回归是一种较为简单,但十分重要的机器学习方法.掌握线性的原理及求解方法,是深入了解线性回归的基本要求.除此之外,线性回归也是监督学习回归部分的基石. 线性回归介绍 ...

  2. GC算法精解(五分钟让你彻底明白标记/清除算法)

    GC算法精解(五分钟让你彻底明白标记/清除算法) 相信不少猿友看到标题就认为LZ是标题党了,不过既然您已经被LZ忽悠进来了,那就好好的享受一顿算法大餐吧.不过LZ丑话说前面哦,这篇文章应该能让各位彻底 ...

  3. GC算法精解(五分钟教你终极算法---分代搜集算法)

    GC算法精解(五分钟教你终极算法---分代搜集算法) 引言 何为终极算法? 其实就是现在的JVM采用的算法,并非真正的终极.说不定若干年以后,还会有新的终极算法,而且几乎是一定会有,因为LZ相信高人们 ...

  4. [转帖]算法精解:DAG有向无环图

    算法精解:DAG有向无环图 https://www.cnblogs.com/Evsward/p/dag.html DAG是公认的下一代区块链的标志.本文从算法基础去研究分析DAG算法,以及它是如何运用 ...

  5. 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...

  6. 【2018.08.13 C与C++基础】C++语言的设计与演化读书笔记

    先占坑 老实说看这本书的时候,有很多地方都很迷糊,但却说不清楚问题到底在哪里,只能和Effective C++联系起来,更深层次的东西就想不到了. 链接: https://blog.csdn.net/ ...

  7. 算法精解(C语言描述) 第4章 读书笔记

    第4章 算法分析 1.最坏情况分析 评判算法性能的三种情况:最佳情况.平均情况.最坏情况. 为何要做最坏情况分析: 2.O表示法 需关注当算法处理的数据量变得无穷大时,算法性能将趋近一个什么样的值.一 ...

  8. 算法精解(C语言描述) 第3章 读书笔记

    第3章 递归 1.基本递归 假设想计算整数n的阶乘,比如4!=4×3×2×1. 迭代法:循环遍历其中的每一个数,然后与它之前的数相乘作为结果再参与下一次计算.可正式定义为:n! = (n)(n-1)( ...

  9. JVM内存管理------GC算法精解(复制算法与标记/整理算法)

    本次LZ和各位分享GC最后两种算法,复制算法以及标记/整理算法.上一章在讲解标记/清除算法时已经提到过,这两种算法都是在此基础上演化而来的,究竟这两种算法优化了之前标记/清除算法的哪些问题呢? 复制算 ...

随机推荐

  1. KDE子项目一览 good

    https://www.kde.org/applications/development/ https://www.kde.org/applications/graphics/ https://www ...

  2. 快学Scala习题解答—第一章 基础

    1 简介 近期对Scala比较感兴趣,买了本<快学Scala>,感觉不错.比<Programming Scala:Tackle Multi-Core Complexity on th ...

  3. java--工具方法

    根据时间戳得到具体的时间: public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat(& ...

  4. aix5.1 5.2 5.3 6.1 7.1运维技术总结

    ++++++++++++++++++++++++++++ + Ruiy 20014-10 zz + +Technology Area; + Tel:150 55198367 + QQ:5160 591 ...

  5. AngularJs(三) deployd 服务的使用

    使用服务建立数据 在AngularJS(二)中,我搭建好了deployd服务,现在启动服务,创建正在的数据(开始是使用模拟数据),使用cmd命令 一.开启Mongodb数据. 贴图: 二:测试是否正常 ...

  6. 关于js中的类型内容总结(类型识别)

    JS 有7种数据类型: 6种原始类型:Boollean    String   Number    Null    Underfined   Symbol 引用类型:Object 类型识别主要有以下四 ...

  7. 创建基于maven的项目模版

    我们在实际工作中 ,有些项目的架构是相似的,例如基于 restful的接口项目,如果每次都重新搭建一套架构或者通过拷贝建立一个项目难免有些得不偿失,这里我们可以用maven的archtype建立项目模 ...

  8. 在 IIS 中配置 ASP.NET 应用程序

     注意事项: 1.注册.NET 如果先安装.net平台,后安装IIS,那么在IIS中可能就没有出现ASP.NET版本的下拉菜单,就要手动注册: 一般.Net版本都存放在:C:\WINDOWS\Micr ...

  9. ASP.Net MVC @Html类

    Model中的类 using System.Web;using System.ComponentModel; public class GuestBook { public int Id { get; ...

  10. leetcode Binary Tree Inorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...