c语言用类封装单向链表

时间: 2023-07-12 12:45:01 浏览: 59
C语言中没有类的概念,但可以使用结构体和函数指针来实现链表的封装,下面是一个简单的单向链表封装示例。 ```c #include <stdio.h> #include <stdlib.h> typedef struct _node { int value; struct _node* next; } ListNode; typedef struct _linked_list { ListNode* head; int size; void (*insert)(struct _linked_list*, int); void (*remove)(struct _linked_list*, int); void (*print)(struct _linked_list*); void (*clear)(struct _linked_list*); } LinkedList; void insert(LinkedList* list, int value) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->value = value; node->next = NULL; if (list->head == NULL) { list->head = node; } else { ListNode* cur = list->head; while (cur->next) { cur = cur->next; } cur->next = node; } list->size++; } void remove_node(LinkedList* list, int value) { ListNode* cur = list->head; ListNode* prev = NULL; while (cur) { if (cur->value == value) { if (prev == NULL) { list->head = cur->next; } else { prev->next = cur->next; } free(cur); list->size--; return; } prev = cur; cur = cur->next; } } void print(LinkedList* list) { ListNode* cur = list->head; while (cur) { printf("%d ", cur->value); cur = cur->next; } printf("\n"); } void clear(LinkedList* list) { ListNode* cur = list->head; while (cur) { ListNode* next = cur->next; free(cur); cur = next; } list->head = NULL; list->size = 0; } LinkedList* new_linked_list() { LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList)); list->head = NULL; list->size = 0; list->insert = insert; list->remove = remove_node; list->print = print; list->clear = clear; return list; } int main() { LinkedList* list = new_linked_list(); list->insert(list, 1); list->insert(list, 2); list->insert(list, 3); list->print(list); list->remove(list, 2); list->print(list); list->clear(list); free(list); return 0; } ``` 在这个示例中,我们首先定义了两个结构体:`ListNode`和`LinkedList`。`ListNode`表示链表节点,包含一个整数值和指向下一个节点的指针。`LinkedList`表示链表本身,包含指向头节点的指针和链表中节点的数量,以及链表操作的函数指针。 然后,我们定义了一些函数来操作链表。`insert`函数用于在链表末尾插入一个新节点,新节点的值为传入的参数。`remove_node`函数用于从链表中删除一个节点,值为传入的参数。`print`函数用于打印链表中所有节点的值。`clear`函数用于清空链表中的所有节点,释放内存。 在`new_linked_list`函数中,我们创建了一个新的`LinkedList`对象,并将链表操作的函数指针赋值给相应的成员变量。在`main`函数中,我们使用`new_linked_list`函数创建了一个新的链表对象,然后使用链表操作的函数指针来操作链表。 这个示例只是一个简单的单向链表封装,如果需要更复杂的功能,可以在`LinkedList`结构体中添加更多的成员变量和函数,并在函数中实现更多的链表操作。

相关推荐

最新推荐

recommend-type

C语言数据结构实现链表逆序并输出

主要介绍了C语言数据结构实现链表逆序并输出的相关资料,需要的朋友可以参考下
recommend-type

C语言链表题目(附答案).docx

大一初学C语言时的期末作业,涉及到链表的建立和功能的实现,涉及指针、函数、动态结构建立等方面的知识,初学者可以参考参考尝试尝试哟!!!
recommend-type

C语言实现带头结点的单向链表的基本操作

使用C语言实现了单向链表的创建,输出,插入元素和删除元素以及单向链表的逆序连接和两个有序线性表的归并
recommend-type

C语言实现带头结点的链表的创建、查找、插入、删除操作

主要介绍了C语言实现带头结点的链表的创建、查找、插入、删除操作方法,对于了解数据结构中链表的各项操作有很好的借鉴价值,需要的朋友可以参考下
recommend-type

C语言基于循环链表解决约瑟夫环问题的方法示例

主要介绍了C语言基于循环链表解决约瑟夫环问题的方法,简单描述了约瑟夫环问题并结合实例形式分析了C语言使用循环链表解决约瑟夫环问题的具体操作技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。