c 语言双向链表排序,C实现的可排序的双向链表

这篇博客介绍了一种使用C语言实现的双向链表,该链表具有排序功能,支持数字和字符串的升序排列。文章分享了链表的头文件定义,包括节点结构、链表操作如插入、获取链表大小、比较节点值等,并提供了测试代码以展示其功能,例如插入数字和字符串并进行排序、取出链表元素等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天用C实现了一个双向链表,这个链表有排序功能,默认按升序排列,接受的参数可以是数字,也可以是字符串。现在把自己写的代码,分享出来。

头文件:

#define DOUBLE_LINK_H_INCLUDED

//双向链表节点

typedef struct Double_Node

{

struct Double_Node* next;

struct Double_Node* pre;

const void* value;//注意,此处声明为const,是考虑到了字符常量

} node;

//判断链表是否为空

extern bool dlink_is_empty();

//链表的大小

extern int dlink_size();

//比较节点值的大小,仅限于数字和字符串

extern int compare(const void* a,const void* b);

//将"value"插入到链表,成功返回0,否则返回-1

extern int dlink_insert(const void *pval);

//返回链表的第一个元素

extern node* peek();

//取出链表的第一个元素,free操作,在poll方法后,不能忘记,否则,会内存泄漏

extern node* poll();

#endif // DOUBLE_LINK_H_INCLUDED

#include #include #include #include #include #include #include "double_link.h" using namespace std;

//链表头部 static node* head = NULL; static node* tail = NULL; //节点数量 static int count = 0;

/**  * 创建节点,成功后返回节点指针  * \param  void* value 传入的指针  * \return node*  */ static node* createNode(const void* value) {     node* n = (node*)malloc(sizeof(node));     if (!n)     {         cout << "failed in createNode!" << endl;         return NULL;    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值