c语言数据结构链表源代码----笛风读书笔记系列

2024-06-23 09:58

本文主要是介绍c语言数据结构链表源代码----笛风读书笔记系列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

读书笔记系列之:c语言数据结构链表源代码

                                                                              笛风

                                                                              2013.10.11

在linux下已通过编译,运行毫无压力~


头文件的定义:

Linked_list.h

#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
#define NOT_FOUND -1
#define SUCCESS 0
typedef struct linked_list_t linked_list_t;
//链表提供接口,可以继续增加功能
struct linked_list_t {int (*get_count)(linked_list_t *this);int (*remove_first)(linked_list_t *this, void **item);void (*insert_last)(linked_list_t *this, void *item);void (*destroy)(linked_list_t *this);
};linked_list_t *linked_list_create(void);#endif

链表的函数定义:

Linked_list.c

#include "stdlib.h"
#include "stdio.h"
#include "linked_list.h"typedef struct element_t element_t;
struct element_t {void *value;element_t *previous;element_t *next;
};
//创建链表节点
element_t *element_create(void *value)
{element_t *this = malloc(sizeof(element_t));this->previous = NULL;this->next = NULL;this->value = value;return (this);
}typedef struct private_linked_list_t private_linked_list_t;//链表
struct private_linked_list_t {linked_list_t public;int count;element_t *first;element_t *last;
};static int get_count(private_linked_list_t *this){return this->count;
}
static int get_first(private_linked_list_t *this, void **item)
{if (this->count == 0){return NOT_FOUND;}*item = this->first->value;return SUCCESS;
}
static element_t* remove_element(private_linked_list_t *this, element_t *element)
{element_t *next, *previous;next = element->next;previous = element->previous;free(element);if (next){next->previous = previous;}else{this->last = previous;}if (previous){previous->next = next;}else{this->first = next;}if (--this->count == 0){this->first = NULL;this->last = NULL;}return next;
}
//线程从任务队列上取下任务
static int remove_first(private_linked_list_t *this, void **item)
{//获得第一个元素的值结构体指针if (get_first(this, item) == SUCCESS){//删除第一个元素remove_element(this, this->first);return SUCCESS;}return NOT_FOUND;
}static void insert_last(private_linked_list_t *this , void * item){element_t *element = element_create(item);if (this->count == 0){/* first entry in list */this->first = element;this->last = element;element->previous = NULL;element->next = NULL;}else{element_t *old_last_element = this->last;element->previous = old_last_element;element->next = NULL;old_last_element->next = element;this->last = element;}this->count++;
}
static void destroy(private_linked_list_t *this){printf("linked list destroy!\n");
}
linked_list_t *linked_list_create(void){private_linked_list_t *this = malloc(sizeof(private_linked_list_t));this->public.get_count = (int(*)(linked_list_t *))get_count;this->public.remove_first = (int(*)(linked_list_t * ,void **item))remove_first;	this->public.insert_last = (void(*)(linked_list_t * ,void *item))insert_last;this->public.destroy = (void(*)(linked_list_t *))destroy;this->first = NULL;this->last = NULL;this->count = 0;return &(this->public);
}

主函数://链表的使用:

#include "stdlib.h"
#include "stdio.h"
#include "linked_list.h"main() 
{linked_list_t *list=linked_list_create();int a=list->get_count(list);int c=25;void *b=(void *)&c;printf("链表元素的个数为:%d\n",a);list->insert_last(list,b);printf("链表插入元素成功!\n");a=list->get_count(list);printf("链表元素的个数为:%d\n",a);}







这篇关于c语言数据结构链表源代码----笛风读书笔记系列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086833

相关文章

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

Go语言中json操作的实现

《Go语言中json操作的实现》本文主要介绍了Go语言中的json操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 一、jsOChina编程N 与 Go 类型对应关系️ 二、基本操作:编码与解码 三、结构体标签(Struc

python语言中的常用容器(集合)示例详解

《python语言中的常用容器(集合)示例详解》Python集合是一种无序且不重复的数据容器,它可以存储任意类型的对象,包括数字、字符串、元组等,下面:本文主要介绍python语言中常用容器(集合... 目录1.核心内置容器1. 列表2. 元组3. 集合4. 冻结集合5. 字典2.collections模块

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

GO语言短变量声明的实现示例

《GO语言短变量声明的实现示例》在Go语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路

Java集合中的链表与结构详解

《Java集合中的链表与结构详解》链表是一种物理存储结构上非连续的存储结构,数据元素的逻辑顺序的通过链表中的引用链接次序实现,文章对比ArrayList与LinkedList的结构差异,详细讲解了链表... 目录一、链表概念与结构二、当向单链表的实现2.1 准备工作2.2 初始化链表2.3 打印数据、链表长