1,双链表
#include "./01double_list.h"
//创建头节点
dou* create_doublelist()
{
dou* head=(dou*)malloc(sizeof(dou));
memset(head,0,sizeof(dou));
return head;
}
//头插
void insret_double(dou* head,datadType data)
{
dou* temp=(dou*)malloc(sizeof(dou));
if(NULL==temp)
{
printf("新节点创建失败\n");
}
temp->txt.data=data;
temp->next=NULL;
temp->prev=NULL;
if(head->next==NULL)
{
temp->next=head->next;
head->next=temp;
temp->prev=head;
}
else
{
head->next->prev=temp;
temp->prev=head;
temp->next=head->next;
head->next=temp;
}
head->txt.len++;
return;
}
//遍历
void BL(dou* head)
{
dou* p=head;
while(p->next != NULL)
{
p=p->next;
printf("%d ",p->txt.data);
}
putchar(10);
}
//尾插
void weicha_doublelist(dou* head, datadType data)
{
dou* temp=(dou*)malloc(sizeof(dou));
if(temp==NULL)
{
printf("创建节点失败");
return;
}
temp->txt.data=data;
temp->next=NULL;
temp->prev=NULL;
dou* p = head;
while(p->next != NULL)
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
temp->prev=p;
head->txt.len++;
return;
}
//按位置插入
void insret_doublebypos(dou* head, datadType data,int pos)
{
dou* temp=(dou*)malloc(sizeof(dou));
if(temp==NULL)
{
printf("创建节点失败\n");
return;
}
temp->txt.data=data;
temp->next=NULL;
temp->prev=NULL;
dou* p=head;
for(int i=0; i<pos-1; i++)
{
p=p->next;
}
if(p->next == NULL)
{
temp->next=p->next;
p->next=temp;
temp->prev=head;
}
else
{
temp->next=p->next;
p->next=temp;
temp->next->prev=temp;
temp->prev=p;
}
head->txt.len++;
return;
}
//头删
datadType Tshan_list(dou* head)
{
if(NULL==head->next)
{
printf("无数据删除失败\n");
return (datadType)-1;
}
dou* p=head;
dou* temp=p->next;
if(temp->next==NULL)
{
temp->next=NULL;
}
else
{
p->next=temp->next;
temp->next->prev=head;
}
datadType data = temp->txt.data;
free(temp);
head->txt.len--;
return data;
}
//尾删
datadType Wshan_list(dou* head)
{
if(head==NULL || head->next==NULL)
{
printf("链表为空或者能够非法\n");
return (datadType)-1;
}
dou* temp= head;
while (temp->next != NULL)
{
temp=temp->next;
}
datadType data=temp->txt.data;
temp->prev->next=NULL;
head->txt.len--;
return data;
}
//按位置删除
datadType shan_doublelist(dou* head,int pos)
{
if(pos<1 || pos>head->txt.len)
{
printf("pos超出数据范围");
return (datadType)-1;
}
if(head==NULL || head->next==NULL)
{
printf("链表为空或者能够非法\n");
return (datadType)-1;
}
dou* p=head;
for(int i=0;i<pos;i++)
{
p=p->next;
}
datadType data=p->txt.data;
if(p->next == NULL)
{
p->prev->next=NULL;
}
else
{
p->next->prev=p->prev;
p->prev->next=p->next;
head->txt.len--;
}
return data;
}