LinuxC双向链表的各种操作
来源:互联网 发布:ubuntu 桌面u盘路径 编辑:程序博客网 时间:2024/05/01 09:02
{
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");
scanf("%d",&data);
if(-1 == data)
{
break;
}
dhead = _Insert_doublist_tail(dhead,data);
}while(1);
return dhead;
}
int Length_doublist(Dnode *dhead)
{
int len = 0;
Dnode *tmp = NULL;
for(tmp = dhead->next; tmp != dhead; tmp = tmp->next)
{
len++;
}
return len;
}
/*在i位置插入数据data-控制函数*/
static Dnode *_Insert_doublist_addr(Dnode *dhead,int data,int i)
{
int j;
int len;
Dnode *dnew = NULL;
Dnode *tmp = NULL;
/*1.判断是否越界*/
len = Length_doublist(dhead);
if((i <= 0) || (i > len+1))
{
printf("亲,你越界了....\n");
return dhead;
}
/*分配空间*/
dnew = (Dnode *)malloc(sizeof(Dnode));
if(NULL == dnew)
{
perror("malloc");
return NULL;
}
dnew->dat = data;
/*3.找到i位置*/
j = 0;
tmp = dhead;
while(j < i)
{
tmp = tmp->next;
j++;
}}
/*4.插入操作*/
dnew->prev = tmp->prev;
tmp->prev->next = dnew;
dnew->next = tmp;
tmp->prev = dnew;
return dhead;
/*在i位置插入数据data-控制函数*/
Dnode *Insert_doublist_addr(Dnode *dhead)
{
int i;
int data;
do{
printf("Please input dat for doublist mid insert(-1:quit)\n");scanf("%d",&data);if(-1 == data){
break;
}printf("Please input address do you want\n");scanf("%d",&i);dhead = _Insert_doublist_addr(dhead,data,i);Print_doublist(dhead);//打印链表
}while(1);return dhead;
}
/*删除i位置节点*/
static Dnode *_Delete_doublist_addr(Dnode *dhead,int i)
{
int j;
int len;
Dnode *tmp = NULL;
/*1.判断是否越界*/
len = Length_doublist(dhead);
if((i <= 0) || (i > len))
{
printf("亲,你越界了....\n");
return dhead;
}
/*2.找到i位置*/
j = 0;
tmp = dhead;
while(j < i)
{
tmp = tmp->next;
j++;
/*删除i位置节点*/}
/*删除操作*/
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
free(tmp);
return dhead;
}
Dnode *Delete_doublist_addr(Dnode *dhead)
{
int i;
do{
printf("Please input address do you want delete(-1:quit)\n");scanf("%d",&i);if(-1 == i){
break;
}dhead = _Delete_doublist_addr(dhead,i);Print_doublist(dhead);//打印链表
}while(1);
return dhead;
}
/*查找并修改*/
static Dnode *_Search_doublist_data_modify(Dnode *dhead,int data, int newdata)
{
int i = 0;
int flag = 0;
Dnode *tmp = NULL;
if(dhead == dhead->next)
{
return dhead;
}
for(tmp = dhead->next; tmp != dhead; tmp = tmp->next)
{
i++;
if(tmp->dat == data)
{
flag = 1;
printf("on the %d addr data is %d\n",i,tmp->dat);
tmp->dat = newdata;
}
}
if(!flag)
{
printf("查无此数。。。。\n");
}
return dhead;
}
/*查找并修改*/
Dnode *Search_doublist_data_modify(Dnode *dhead)
{
int data;
int newdata;
do{
printf("Please input src data do you want serach(-1:quit)\n");
scanf("%d",&data);
if(-1 == data)
{
break;
}
printf("Please input new data do you want modify(-1:quit)\n");
scanf("%d",&newdata);
dhead = _Search_doublist_data_modify(dhead,data,newdata);
Print_doublist(dhead);//打印链表
}while(1);
return dhead;
}
#if 0
/*排序*/
Dnode *Sort_doublist(Dnode *dhead)
{
Dnode *dnew = NULL;
Dnode *cur = NULL;
Dnode *pnext = NULL;
Dnode *tmp = NULL;
Dnode *pprev = NULL;
dnew = Create_doublist_head();//创建新的头节点
cur = dhead->next;
pnext = cur->next;
/*构建新的链表*/
dnew->next->prev = cur;
cur->next = dnew->next;
dnew->next = cur;
cur->prev = dnew;
for(cur = pnext; cur != dhead; cur = pnext)
{
pnext = cur->next;
pprev = dnew;
for(tmp = dnew->next; tmp != dnew; tmp = tmp->next)
{
if(cur->dat < tmp->dat)
{
break;
}
pprev = tmp;
}
/*插入操作*/
pprev->next = cur;
cur->prev = pprev;
cur->next = tmp;
tmp->prev = cur;
}
return dnew;
}
#endif
/*排序*/
Dnode *Sort_doublist(Dnode *dhead)
{
Dnode *dnew = NULL;
Dnode *cur = NULL;
Dnode *pnext = NULL;
Dnode *tmp = NULL;
dnew = Create_doublist_head();//创建新的头节点
cur = dhead->next;
pnext = cur->next;
/*构建新的链表*/
dnew->next->prev = cur;
cur->next = dnew->next;
dnew->next = cur;
cur->prev = dnew;
for(cur = pnext; cur != dhead; cur = pnext)
{
pnext = cur->next;
for(tmp = dnew->next; tmp != dnew; tmp = tmp->next)
{
if(cur->dat < tmp->dat)
{
break;
}
}
/*插入操作*/
tmp->prev->next = cur;
cur->prev = tmp->prev;
cur->next = tmp;
tmp->prev = cur;
}
return dnew;
}
void Print_doublist(Dnode *dhead)
{
Dnode *tmp = NULL;
if(dhead == dhead->next)
{
printf("亲!么有数据....\n");
return ;
}
for(tmp = dhead->next; tmp != dhead; tmp = tmp->next)
{
printf("%d\t",tmp->dat);
}
printf("\n");
}
void Destroy_doublist(Dnode **head)
{
Dnode *tmp = NULL;
Dnode *pnext = NULL;
if((*head) == (*head)->next)
{
printf("亲!么有节点....\n");
return ;
}
for(tmp = (*head); tmp != (*head); tmp = pnext)
{
pnext= tmp->next;
free(tmp);
}
*head = NULL;
}
- LinuxC双向链表的各种操作
- 双向链表的操作
- 双向链表的操作
- 双向链表的操作
- 双向链表的操作
- 双向链表的操作
- 双向链表的操作
- 【C++】模版实现双向链表的各种操作(如:逆置、去重Unique、分类(冒泡)、合并)
- 双向链表操作
- 双向链表操作
- 双向链表操作
- 双向链表操作
- 双向链表操作
- 双向循环链表的基本操作
- 双向链表的基本操作
- 双向链表的基本操作
- 双向循环链表的操作源码
- c++的双向链表操作
- 蓝桥杯-结果填空题-原题
- iOS消息推送实现过程记录
- 在Markdown中紧跟list列表后的四空格缩进代码被错误解析问题与解决方法
- 今天面完了聚成网络的php研发,面经一枚,为自己攒人品。
- iOS 多线程编程之Grand Central Dispatch(GCD)
- LinuxC双向链表的各种操作
- STL源码剖析 - 第5章 关联式容器 - hash_multiset
- DDMS全解
- 折半查找
- Rector模式和Proactor模式
- JavaScript权威指南_70_第9章_类和模块_9.0-概述
- MVC编程模式
- #pragma预处理命令
- 读书笔记