算法精解(C语言描述) 第5章 读书笔记
第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章 读书笔记的更多相关文章
- 机器学习|线性回归算法详解 (Python 语言描述)
原文地址 ? 传送门 线性回归 线性回归是一种较为简单,但十分重要的机器学习方法.掌握线性的原理及求解方法,是深入了解线性回归的基本要求.除此之外,线性回归也是监督学习回归部分的基石. 线性回归介绍 ...
- GC算法精解(五分钟让你彻底明白标记/清除算法)
GC算法精解(五分钟让你彻底明白标记/清除算法) 相信不少猿友看到标题就认为LZ是标题党了,不过既然您已经被LZ忽悠进来了,那就好好的享受一顿算法大餐吧.不过LZ丑话说前面哦,这篇文章应该能让各位彻底 ...
- GC算法精解(五分钟教你终极算法---分代搜集算法)
GC算法精解(五分钟教你终极算法---分代搜集算法) 引言 何为终极算法? 其实就是现在的JVM采用的算法,并非真正的终极.说不定若干年以后,还会有新的终极算法,而且几乎是一定会有,因为LZ相信高人们 ...
- [转帖]算法精解:DAG有向无环图
算法精解:DAG有向无环图 https://www.cnblogs.com/Evsward/p/dag.html DAG是公认的下一代区块链的标志.本文从算法基础去研究分析DAG算法,以及它是如何运用 ...
- 数据结构与算法分析——C语言描述 第三章的单链表
数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...
- 【2018.08.13 C与C++基础】C++语言的设计与演化读书笔记
先占坑 老实说看这本书的时候,有很多地方都很迷糊,但却说不清楚问题到底在哪里,只能和Effective C++联系起来,更深层次的东西就想不到了. 链接: https://blog.csdn.net/ ...
- 算法精解(C语言描述) 第4章 读书笔记
第4章 算法分析 1.最坏情况分析 评判算法性能的三种情况:最佳情况.平均情况.最坏情况. 为何要做最坏情况分析: 2.O表示法 需关注当算法处理的数据量变得无穷大时,算法性能将趋近一个什么样的值.一 ...
- 算法精解(C语言描述) 第3章 读书笔记
第3章 递归 1.基本递归 假设想计算整数n的阶乘,比如4!=4×3×2×1. 迭代法:循环遍历其中的每一个数,然后与它之前的数相乘作为结果再参与下一次计算.可正式定义为:n! = (n)(n-1)( ...
- JVM内存管理------GC算法精解(复制算法与标记/整理算法)
本次LZ和各位分享GC最后两种算法,复制算法以及标记/整理算法.上一章在讲解标记/清除算法时已经提到过,这两种算法都是在此基础上演化而来的,究竟这两种算法优化了之前标记/清除算法的哪些问题呢? 复制算 ...
随机推荐
- Realview MDK 中不用手动开中断的原因
startup.s启动代码文件: ; Enter Supervisor Mode and set its Stack Pointer MSR CPSR_c, #Mode_SVC:OR:I_Bit:OR ...
- maven项目打包
配置 你的pom.xml文件,配置 packaging为 war ,然后点击 pom.xml右键,run as 选择 install 或是 package: 如果项目没问题,配置没问题,就会在项目的t ...
- 利用js_API 运行对html文档元素的属性的CRUD操作
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- There is no result type defined for type 'json' mapped with name 'success'. Did you mean 'json'?
错误信息: 严重: Exception starting filter struts2 Unable to load configuration. - action - file:/C:/Users/ ...
- Memcached管理
安装memcached服务端: apt-get install memcached 配置文件存在于: /etc/memcached.conf 启动memcache: memcached -d -m - ...
- Python核心编程读笔 8: 文件和输入输出
第九章 文件和输入输出 一.文件内建函数.方法.属性 1 文件内建函数 file_object = open(file_name, access_mode='r', buffering=-1) 工厂函 ...
- leetcode Linked List Cycle II python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- fckeditor使用详解
FCKEditor是一个很好的用于Web页面中的格式化文本编译控件.现在越来越多的论坛的发帖页面中更多的使用了这个控件,我们这里将如何在基于Java的web开发中使用FCKEditor控件的步骤提供给 ...
- OpenCV学习 1:OpenCV安装与第一个图像显示程序
原创作品,转载请注明出处 为了提升逼格,决定学下OpenCV,想想如果可以做人脸识别,定点降落,让飞机跟着自己飞..想想都有点小激动.这只是想的,能不能学会还不知道..哈.. 1:先下载:h ...
- 通过Orchard认识的Autofac
反射Reflection 这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:'程序集(Assembly)'.'模块(Module)'.'类型(class)'组成,而反射提供一种编程 ...