思维导图
作业:完成双向链表的剩余功能
#include "head.h"
Double_List_Ptr Headnode_Create()
{
Double_List_Ptr H=(Double_List_Ptr)malloc(sizeof(Double_List));
if(H==NULL)
{
printf("head node create failed!\n");
return NULL;
}
printf("head node create successfully!\n");
H->prev=NULL;
H->len=0;
H->next=0;
return H;
}
Double_List_Ptr Regularnode_Create(Datatype node_data)
{
Double_List_Ptr P=(Double_List_Ptr)malloc(sizeof(Double_List));
if(P==NULL)
return NULL;
P->prev=NULL;
P->data=node_data;
P->next=NULL;
return P;
}
int Double_List_Empty(Double_List_Ptr H)
{
if(H==NULL)
return -1;
return H->len==0;
}
void Head_add(Double_List_Ptr H,Datatype Add_data)
{
Double_List_Ptr P=Regularnode_Create(Add_data);
if(H==NULL)
{
printf("head add failed!\n");
return;
}
printf("head add successfully!\n");
if(Double_List_Empty(H))
{
H->next=P;
P->prev=H;
}
else
{
P->next=H->next;
H->next->prev=P;
H->next=P;
P->prev=H;
}
H->len++;
}
void Last_add(Double_List_Ptr H,Datatype Add_data)
{
Double_List_Ptr P=Regularnode_Create(Add_data);
if(H==NULL)
{
printf("last add failed!\n");
return;
}
Double_List_Ptr S=H;
printf("last add successfully!\n");
if(Double_List_Empty(H))
{
H->next=P;
P->prev=H;
}
else
{
for(int i=0;i<H->len;i++)
{
S=S->next;
}
S->next=P;
P->prev=S;
}
H->len++;
}
void Show_Double_List(Double_List_Ptr H)
{
if(H==NULL || Double_List_Empty(H))
{
printf("show double list failed!\n");
return;
}
printf("show double list successfully!\n");
Double_List_Ptr P=H;
for(int i=0;i<H->len;i++)
{
P=P->next;
printf("%d\n",P->data);
}
}
void Insert(Double_List_Ptr H,int location,Datatype Insert_data)
{
Double_List_Ptr P=Regularnode_Create(Insert_data);
if(H==NULL || location<1)
{
printf("insert failed!\n");
return;
}
printf("insert successfully!\n");
Double_List_Ptr S=H;
if(Double_List_Empty(H))
{
P->next=NULL;
P->prev=H;
H->next=P;
H->len++;
}
else if(location>H->len)
{
for(int i=0;i<location-1;i++)
{
S=S->next;
}
P->next=S->next;
S->next->prev=P;
P->prev=S;
S->next=P;
H->len++;
}
else
{
P->next=H->next;
H->next->prev=P;
H->next=P;
P->prev=H;
H->len++;
}
}
void Head_delete(Double_List_Ptr H)
{
if(H==NULL || Double_List_Empty(H))
{
printf("head delete failed!\n");
return;
}
printf("head delete successfully!\n");
Double_List_Ptr P=H->next;
if(H->len!=1)
{
H->next=P->next;
P->next->prev=H;
H->len--;
free(P);
P=NULL;
}
else
{
H->next=NULL;
H->len--;
free(P);
P=NULL;
}
}
void Last_delete(Double_List_Ptr H)
{
if(H==NULL || Double_List_Empty(H))
{
printf("last delete failed!\n");
return;
}
printf("last delete successfully!\n");
Double_List_Ptr P=H;
if(H->len!=1)
{
for(int i=0;i<H->len-1;i++)
{
P=P->next;
}
P->next->prev=NULL;
Double_List_Ptr S=P->next;
free(S);
S=NULL;
P->next=NULL;
H->len--;
}
else
{
P=H->next;
P->prev=NULL;
H->next=NULL;
free(P);
P=NULL;
H->len--;
}
}
void Any_delete(Double_List_Ptr H,int location)
{
if(H==NULL || Double_List_Empty(H) || location<1 || location>H->len)
{
printf("anywhere delete failed!\n");
return;
}
printf("anywhere delete successfully!\n");
Double_List_Ptr P=H;
Double_List_Ptr S=H;
for(int i=0;i<location;i++)
{
P=P->next;
}
if(H->len!=1)
{
for(int j=0;j<location-1;j++)
{
S=S->next;
}
S->next=P->next;
P->next->prev=S;
free(P);
P=NULL;
H->len--;
}
else
{
P->prev=NULL;
S->next=NULL;
free(P);
P=NULL;
H->len--;
}
}
void Modify_bylocation(Double_List_Ptr H,int location,Datatype mod_data)
{
if(H==NULL || Double_List_Empty(H) || location<1 || location>H->len)
{
printf("modify by location failed!\n");
return;
}
printf("modify by location successfully!\n");
Double_List_Ptr P=H;
for(int i=0;i<location;i++)
{
P=P->next;
}
P->data=mod_data;
}
void Modify_byvalue(Double_List_Ptr H,Datatype value,Datatype mod_data)
{
if(H==NULL || Double_List_Empty(H))
{
printf("modify by value failed!\n");
return;
}
printf("modify by value successfully!\n");
Double_List_Ptr P=H->next;
while(P->data!=value && P->next!=NULL)
{
P=P->next;
}
P->data=mod_data;
if(P->next==NULL)
{
printf("we cannot find the value,cannot modify!\n");
}
}
void Find_bylocation(Double_List_Ptr H,int location)
{
if(H==NULL || Double_List_Empty(H) || location<1 || location>H->len)
{
printf("find by location failed!\n");
return;
}
printf("find by location successfully!\n");
Double_List_Ptr P=H;
for(int i=0;i<location;i++)
{
P=P->next;
}
printf("Value of the location is:%d\n",P->data);
}
void Find_byvalue(Double_List_Ptr H,Datatype find_data)
{
if(H==NULL || Double_List_Empty(H))
{
printf("find by value failed!\n");
return;
}
printf("find by value successfully!\n");
Double_List_Ptr P=H->next;
while(P->data!=find_data && P->next!=NULL)
{
P=P->next;
}
printf("Location of the value is:%p\n",P);
if(P->next==NULL && P->data!=find_data)
{
printf("we cannot find the value,cannot find!\n");
}
}