C数据结构之双链表详细示例分析
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
复制代码 代码如下:
typedef struct node
{
struct node *prior;
struct node *next;
int num;
}NODE;
/*******双向链表的初始化********/
NODE *Init_link(void)
{
int i;
NODE *phead,*pb,*pi;
phead = (NODE *)malloc(sizeof(NODE));
printf("please input num:\n");
scanf("%d",&(phead->num));
phead->prior = NULL;
phead->next = NULL;
pb = phead;
for(i=0; i<3; i++)
{
pi = (NODE *)malloc(sizeof(NODE));
printf("please input num:\n");
scanf("%d",&(pi->num));
pi->next = NULL;
pb->next = pi;
pi->prior = pb;
pb = pi;
}
return phead;
}
/******链表的输出******/
void print_link(NODE *phead)
{
while(phead != NULL)
{
printf(" %d,",phead->num);
phead = phead->next;
}
printf("\n");
}
/******链表的逆序******/
NODE *reverse_link(NODE *phead)
{
NODE *pb,*pf,*temp;
pb = phead;
pf = pb->next;
while(pf != NULL)
{
temp = pf->next;
pf->next = pb;
pb->prior = pf;
pb = pf;
pf = temp;
}
// phead->prior = pf->prior;
phead->next = NULL;
phead = pb;
return phead;
}
/*****链表的排序*****/
NODE *Order_link(NODE *phead)
{
NODE *pb,*pf,*ptr,temp;
pb = phead;
while(pb->next != NULL)
{
pf = pb->next;
while(pf != NULL)
{
if(pb->num > pf->num)
{
temp = *pb;
*pb = *pf;
*pf = temp;
ptr = pb->next;
pb->next = pf->next;
pf->next = ptr;
ptr = pb->prior;
pb->prior = pf->prior;
pf->prior = ptr;
}
pf = pf->next;
}
pb = pb->next;
}
return phead;
}
/******链表的有序插入*****/
NODE *insert_link(NODE *phead,NODE *pi)
{
NODE *pb,*pf;
pb = phead;
if(pi == NULL)
return phead;
if(phead == NULL)
{
phead = pi;
pi->prior = NULL;
pi->next =NULL;
}
else
{
while( (pi->num > pb->num)&&(pb->next!=NULL))
{
pf = pb;
pb = pb->next;
}
if(pi->num <= pb->num)
{
if(pb == phead)
{
pi->next = phead;
pi->prior = NULL;
phead->prior = pi;
phead = pi;
}
else
{
pi->next = pb;
pb->prior = pi;
pi->prior = pf;
pf->next = pi;
}
}
else
{
pi->next = NULL;
pi->prior = pb;
pb->next = pi;
}
}
return phead;
}
/******链表的删除*****/
NODE *delete_link(NODE *phead, NODE *pi)
{
NODE *pb,*pf;
pb = phead;
while(pb != NULL)
{
if(pb == pi)
{
if(pb == phead)
phead = phead->next;
else if(pb->next == NULL)
pf->next = NULL;
else
{
pf->next = pb->next;
//pb->next->prior = pf;
}
}
pf = pb;
pb = pb->next;
}
return phead;
}
/*******链表的释放******/
void free_link(NODE *phead)
{
NODE *pb;
while(phead != NULL)
{
pb = phead->next;
free(phead);
phead = pb;
}
printf("released link success!\n");
}
/*******测试程序******/
int main(void)
{
NODE *phead,*pi;
phead = Init_link();
printf("init link:\n");
print_link(phead);
pi = (NODE *)malloc(sizeof(NODE));
printf("plaese input insert num:");
scanf("%d",&(pi->num));
phead = insert_link(phead,pi);
printf("after insert link:\n");
print_link(phead);
phead = Order_link(phead);
printf("after sort link:\n");
print_link(phead);
printf("after reverse link:\n");
phead = reverse_link(phead);
print_link(phead);
phead = delete_link(phead, pi);
printf("after delete link:\n");
print_link(phead);
free_link(phead);
return 0;
}
栏 目:C语言
下一篇:浅析c#中如何在form的webbrowser控件中获得鼠标坐标
本文标题:C数据结构之双链表详细示例分析
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/4248.html
您可能感兴趣的文章
- 01-10数据结构课程设计- 解析最少换车次数的问题详解
- 01-10数据结构课程设计-用栈实现表达式求值的方法详解
- 01-10深入理解链表的各类操作详解
- 01-10用C语言实现单链表的各种操作(一)
- 01-10用C语言实现单链表的各种操作(二)
- 01-10深入单链表的快速排序详解
- 01-10探讨:将两个链表非降序合并为一个链表并依然有序的实现方法
- 01-10C++ 构造双向链表的实现代码
- 01-10用C++实现单向循环链表的解决方法
- 01-10如何用C++实现双向循环链表
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10SublimeText编译C开发环境设置
- 04-02jquery与jsp,用jquery
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10delphi制作wav文件的方法
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10C#中split用法实例总结
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10使用C语言求解扑克牌的顺子及n个骰子