C语言实现通用链表初步(一)
注意:本文讨论的是无头单向非循环链表。
假设不采用Linux内核链表的思路,怎样用C语言实现通用链表呢?
一种常用的做法是:
typedef int element_t;
struct node_info
{
element_t data;
struct node_info *next;
};
其实这样的链表算不上通用,因为无法同时使用不同的数据类型。参考网友的思路,以上可以改为下面的定义:
//无头非循环单向链表
struct node_info {
void *data;
struct node_info *next;
};
先看看头文件:
#pragma once //无头非循环单向链表
struct node_info {
void *data;
struct node_info *next;
}; struct student
{
char name[20];
unsigned char age; };//for test struct slist_info {
struct node_info *first; //指向第一个节点 void (*insert_head)(void *one_data,
struct slist_info *info);//头插 int (*del)(struct node_info *node,
struct slist_info *info);//删除节点 struct node_info* (*find)(struct slist_info *info,
int(*compare)(void *dest,void *key),void *key);
//这里的compare是回调函数,由用户提供 void (*for_each)(const struct slist_info *info,
void (*todo)(void *one_data));//遍历,todo是回调函数 void (*for_each_safe)(const struct slist_info *info,
void (*todo)(void *one_data));//安全遍历(觉得在这里这个方法意义不大) void (*invert)(struct slist_info *info);//反转链表 int (*is_dead_loop)(struct slist_info *info);//判断是否有死环,是返回1,不是返回0
}; #define slist_is_empty(info) ((info)->first == NULL) //链表是否为空 //构造和析构
void slist_init(struct slist_info *info);
void slist_destroy(struct slist_info *info);
与这个链表相关的操作如下。
1.插入元素(头插法)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "slist.h" /*无头非循环单链表*/
static void slist_insert_head(void *one_data,
struct slist_info *info)
{//头插法
assert(one_data != NULL && info != NULL); struct node_info *node =
(struct node_info *)malloc(sizeof(struct node_info));//分配内存
assert(node!=NULL); node->data = one_data;//注意,这里只是简单地把指针指向用户的数据,并没有把用户数据复制过来 if (slist_is_empty(info)) {//1.空链表
info->first = node;
node->next = NULL;
}else { //2. 非空
node->next = info->first;
info->first = node;
}
}
2.遍历
static void slist_for_each(const struct slist_info *info,
void (*todo)(void *one_data))
{
assert(info != NULL && todo != NULL);
struct node_info *cur = NULL; for (cur = info->first; cur != NULL; cur = cur->next) {
todo(cur->data);
}
} static void slist_for_each_safe(const struct slist_info *info,
void (*todo)(void *one_data))
{
assert(info != NULL && todo != NULL);
struct node_info *cur = NULL;
struct node_info *Next = NULL;
for (cur = info->first; cur != NULL; cur = Next) {
Next = cur->next;//Next保存了下一个节点,如果这个节点被删除了,那么下一个节点还可以找到
todo(cur->data);
}
}
3.反转(面试的时候,有可能会问到,直接背过好了!)
static void slist_invert(struct slist_info *info)
{
assert(info != NULL); struct node_info *Cur = NULL;
struct node_info *Prev = NULL;
struct node_info *Next = NULL;
//1.移动Next 2.反向 3.移动Prev 4.移动Cur
for (Cur = info->first; Cur != NULL; Cur = Next) {
Next = Cur->next;
Cur->next = Prev;
Prev = Cur;
}
//修改头指针,指向首节点
info->first = Prev;
}
4.查找
struct node_info *slist_find(struct slist_info *info,int(*compare)(void *dest,void *key),void *key)
{
assert(info != NULL && compare != NULL);
struct node_info *cur = NULL; for (cur = info->first; cur != NULL; cur = cur->next) {
if(compare(cur->data,key)==1)//回调函数,把链表中的每一个数据和用户传入的关键字做比较,符合条件返回1
return cur;//返回节点的地址给用户
}
return NULL;//没有找到返回空指针
}
注意,这里的第三个参数(关键字)也是用户传进来的。
5.构造和析构
void slist_init(struct slist_info *info)
{
//空链表, 第一个节点为NULL
info->first = NULL; info->insert_head = slist_insert_head;
info->del = slist_del;
info->find = slist_find;
info->invert = slist_invert;
info->is_dead_loop = slist_is_dead_loop;
info->for_each = slist_for_each;
info->for_each_safe = slist_for_each_safe;
} void slist_destroy(struct slist_info *info)
{
if(!slist_is_empty(info))
{
struct node_info *cur = NULL;
struct node_info *Next = NULL;
for (cur = info->first; cur != NULL; cur = Next)
{
Next = cur->next;//Next保存了下一个节点,如果这个节点被删除了,那么下一个节点还可以找到
free(cur);//释放每个节点占用的内存
}
} }
细心的读者会发现,好像有的函数没有实现啊。下一篇博文我们再讨论!
C语言实现通用链表初步(一)的更多相关文章
- C语言实现通用链表初步(四)----双向链表
在前面的文章中,我们讨论了如何实现通用类型的链表,方法是用void *类型的指针,指向数据.那么还有其他的方法吗(不考虑内核链表)? 答案是肯定的.用零长数组也可以实现. struct node_in ...
- C语言实现通用链表初步(三)----单元测试
前两节,我们已经完成了链表的一些操作,快来测试一下吧. 这里使用的单元测试工具名字叫"check". START_TEST(my_slist_1) { struct student ...
- C语言实现通用链表初步(二)
接着上次的内容,我们继续! 还是无头单向非循环链表.假如要删除某个节点,如何实现? //删除成功返回0,失败返回-1 int slist_del(struct node_info *node, str ...
- C语言实现单链表-03版
在C语言实现单链表-02版中我们只是简单的更新一下链表的组织方式: 它没有更多的更新功能,因此我们这个版本将要完成如下功能: Problem 1,搜索相关节点: 2,前插节点: 3,后追加节点: 4, ...
- C语言实现单链表-02版
我们在C语言实现单链表-01版中实现的链表非常简单: 但是它对于理解单链表是非常有帮助的,至少我就是这样认为的: 简单的不能再简单的东西没那么实用,所以我们接下来要大规模的修改啦: Problem 1 ...
- C语言实现单链表,并完成链表常用API函数
C语言实现单链表,并完成链表常用API函数: 1.链表增.删.改.查. 2.打印链表.反转打印.打印环形链表. 3.链表排序.链表冒泡排序.链表快速排序. 4.求链表节点个数(普通方法.递归方法). ...
- C 封装一个通用链表 和 一个简单字符串开发库
引言 这里需要分享的是一个 简单字符串库和 链表的基库,代码也许用到特定技巧.有时候回想一下, 如果我读书的时候有人告诉我这些关于C开发的积淀, 那么会走的多直啊.刚参加工作的时候做桌面开发, 服务是 ...
- C语言实现单链表节点的删除(带头结点)
我在之前一篇博客<C语言实现单链表节点的删除(不带头结点)>中具体实现了怎样在一个不带头结点的单链表的删除一个节点,在这一篇博客中我改成了带头结点的单链表.代码演示样例上传至 https: ...
- C/C++语言实现单链表(带头结点)
彻底理解链表中为何使用二级指针或者一级指针的引用 数据结构之链表-链表实现及常用操作(C++篇) C语言实现单链表,主要功能为空链表创建,链表初始化(头插法),链表元素读取,按位置插入,(有序链表)按 ...
随机推荐
- .NET 开源GIS解决方案一 概述
写在前面 最近开始研究开源GIS,国内开源GIS的资料很少,而基于.net的又是少之又少.所以决定把自己研究的资料进行总结整理,技术在于分享,本系列(计划是写一个系列,如果我可以坚持下来的话)部分是自 ...
- 937. Reorder Log Files
You have an array of logs. Each log is a space delimited string of words. For each log, the first w ...
- vm安装centos后unknown host问题和yum install安装不成功问题
网上差了很多说要在vi /etc/sysconfig/network新增GATEWAY=192.168.0.1 还有vi /etc/sysconfig/network-scripts/ifcfg-et ...
- powershell 操作sharepoint命令集
打开SharePoint 2013 Management Shell, and then run as administrator.执行如下命令 1. 添加wsp和安装Add-SPSolution - ...
- foreach循环遍历 行合并
<%@ page contentType="text/html;charset=UTF-8" %> <%@ include file="/WEB-INF ...
- Java 大数相乘、大数相加、大数相减
思路来源:: https://blog.csdn.net/lichong_87/article/details/6860329 /** * @date 2018/6/22 * @description ...
- City Game UVALive - 3029(悬线法求最大子矩阵)
题意:多组数据(国外题好像都这样),每次n*m矩形,F表示空地,R表示障碍 求最大子矩阵(悬线法模板) 把每个格子向上延伸的空格看做一条悬线 以le[i][j],re[i][j],up[i][j]分别 ...
- 「模拟赛20181025」御风剑术 博弈论+DP简单优化
题目描述 Yasuo 和Riven对一排\(n\)个假人开始练习.斩杀第\(i\)个假人会得到\(c_i\)个精粹.双方轮流出招,他们在练习中互相学习,所以他们的剑术越来越强.基于对方上一次斩杀的假人 ...
- 树莓派-开启spi
1. sudo raspi-config #进入树莓派配置页 2. #进入每5项,进入启用spi即可
- java Pattern和Matcher完全解析
基本使用: 本文不讲解正则表达式,需要请看API Scanner中的使用正则表达式 //Scanner 支持的分组 Scanner cin=new Scanner("red a bbc&qu ...