LinuxC双向链表的各种操作

这篇博客详细介绍了如何在Linux环境下使用C语言进行双向链表的操作,包括创建空链表、头插入、尾插入、计算链表长度、在指定位置插入和删除元素、查找并修改元素以及排序链表的方法。示例代码清晰易懂,适合C语言初学者和进阶者学习。
摘要由CSDN通过智能技术生成
typedef struct doubnode
{
int dat;
struct doubnode *prev;
struct doubnode *next;

}Dnode;

/*创建一个双向循环链表头节点(空表)*/
Dnode *Create_doublist_head(void)
{

Dnode *dhead = NULL;


/*1.分配空间*/

dhead = (Dnode *)malloc(sizeof(Dnode));

if(NULL == dhead)

{

perror("malloc");

return NULL;

}


/*赋值*/

dhead->dat = -1;

dhead->prev = dhead->next = dhead;


return dhead;

}


/*头插入-子函数*/
static Dnode *_Insert_doublist_head(Dnode *dhead,int data)
{

Dnode *dnew = NULL;


/*1.为新插入节点分配空间*/

dnew = (Dnode *)malloc(sizeof(Dnode));

if(NULL == dnew)

{

perror("malloc");

return NULL;

}

dnew->dat = data;// 2.赋值


/*3.插入操作*/

dnew->next = dhead->next;

dhead->next->prev = dnew;

dnew->prev = dhead;

dhead->next = dnew;


return dhead;

}


/*头插入-控制函数*/
Dnode *Insert_doublist_head(Dnode *dhead)
{

int data;


do{

printf("Please input dat for doublist head insert(-1:quit)\n");

scanf("%d",&data);

if(-1 == data)

{

break;

}

dhead = _Insert_doublist_head(dhead,data);

}while(1);


return dhead;

}


/*尾插入-子函数*/
static Dnode *_Insert_doublist_tail(Dnode *dhead,int data)
{

Dnode *dnew = NULL;


/*1.为新插入节点分配空间*/

dnew = (Dnode *)malloc(sizeof(Dnode));

if(NULL == dnew)

{

perror("malloc");

return NULL;

}

dnew->dat = data;// 2.赋值


/*尾插入操作*/

dnew->prev = dhead->prev;

dhead->prev->next = dnew;

dhead->prev = dnew;

dnew->next = dhead;


return dhead;

}


/*尾插入-控制函数*/
Dnode *Insert_doublist_tail(Dnode *dhead)
{

int data;


do{

printf("Please input dat for doublist head insert(-1:quit)\n");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值