//设计双链表的数据节点类型
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
int data;
struct node *next;
struct node *prev;
}DN,*PDN;
//struct node -----DN
//struct node *----PDN
//新建空的带头节点的双向头节点链表
PDN doublelist_init()
{
//申请头节点内存
PDN phead=(PDN*)malloc(sizeof(DN));
if(phead==NULL)
{
printf("malloc fail\n");
return false;
}
phead->next=phead;
phead->prev=phead;
return phead;
}
bool is_empty(PDN phead)
{
if(phead->prev==NULL && phead->next==NULL)
return true;
else
return false;
}
//向后遍历
show_head(PDN phead)
{
if(phead==NULL)
{
printf("malloc fail\n");
return false;
}
if(is_empty(phead))
return;
PDN p=phead->next;
while(p!=phead)
{
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
return;
}
//向前遍历
show_tail(PDN phead)
{
if(phead==NULL)
{
printf("malloc fail\n");
return false;
}
PDN p=phead->prev;
while(p!=phead)
{
printf("%d\t",p->data);
p=p->prev;
}
printf("\n");
return;
}
//把d作为数据域,构造新节点,然后把新节点插入到phead指向的头节点
//插入成功返回true,失败返回false
bool insert_node_head(PDN phead,int d)
{
//把d作为数据域,构造新节点
//申请新节点内存空间
PDN pnew=(PDN*)malloc(sizeof(DN));
if(pnew==NULL)
{
printf("malloc fail\n");
return false;
}
pnew->data=d;
pnew->prev=NULL;
pnew->next=NULL;
if(phead==NULL||pnew==NULL)
{
printf("malloc fail\n");
return;
}
pnew->next=phead->next;
pnew->prev=phead;
phead->next->prev=pnew;
phead->next=pnew;
return true;
}
//把d作为数据域,构造新节点,然后把新节点插入到phead指向的头节点前面(整条链表的末尾)
//插入成功返回true,失败返回false
bool insert_node_tail(PDN phead,int d)
{
PDN pnew=(PDN*)malloc(sizeof(DN));
/*PDN p = phead->prev; //聪和佳的写法*/
if(pnew==NULL)
{
printf("malloc fail\n");
return false;
}
pnew->data=d;
pnew->prev=NULL;
pnew->next=NULL;
//聪和佳的写法
//pnew->prev = p;
//pnew->next = phead;
//phead->prev = pnew;
//p->next = pnew;
pnew->prev=phead->prev;
pnew->next=phead;
phead->prev->next=pnew;
phead->prev=pnew;
return true;
}
int delete(PDN phead,int d,PDN pdata)
{
/*int flag = 0;*/
if(is_empty(phead))
return false ;
PDN pdel = phead->next;
PDN pdata_del = NULL;
while(pdel!=phead)
{
if(pdel->data==d)
{
PDN pdata_del = pdel;
pdel->prev->next=pdel->next;
pdel->next->prev=pdel->prev;
/*flag=1*/;
break;
}
pdel=pdel->next;
}
//if (flag == 0)
// return false;
//else
// return pdata_del;
return pdata_del;
}
int main()
{
//创建空的带头节点的双向循环链表
PDN phead=doublelist_init();
PDN pdata = NULL;
//向链表头部中插入节点
printf("this is doublelist after insert node from head\n");
printf("show head and tail:\n");
insert_node_head(phead,1);
insert_node_head(phead,2);
insert_node_head(phead,3);
insert_node_head(phead,4);
show_head(phead);
show_tail(phead);
//向链表尾部中插入节点
printf("this is doublelist after insert node from tail");
printf("show head and tail:\n");
insert_node_tail(phead,1);
insert_node_tail(phead,2);
insert_node_tail(phead,3);
insert_node_tail(phead,5);
show_head(phead);
show_tail(phead);
//删除链表的数据
printf("this is doulelist after delete tail\n");
printf("show head and tail:\n");
if(pdata=delete(phead,5,&pdata))
printf("delete success\n");
else
printf("delete fail\n");
show_head(phead);
show_tail(phead);
}
如何将删除功能中,以删除的链表节点(没有清除数据),重新插入想插入的链表节点位置,该节点也返回给主函数