8月25日
#include "head.h"
//1通过创建头节点创建双向链表
node_p creat_double_list_head_node()
{
node_p H=(node_p)malloc(sizeof(NODE));
if(H==NULL)
{
return NULL;
}
H->len=0;
H->next=NULL;
H->pri=NULL;
return H;
}
//创建普通数据节点
node_p create_data_node(int data)
{
node_p P= (node_p)malloc(sizeof(NODE));
if(S==NULL){return NULL;}
P->data = data;
P->pri = NULL;
P->next = NULL;
return P;
}
//3、判NULL空
int empty(node_p H)
{
if(H==NULL){return -1;}
return H->next==NULL;
}
//头插
void insert_head(node_p H,int value)
{
node_p new=create_data_node(value);
new->next=H->next;
new->pri=H;
if(H->next!=NULL)
{
H->next->pri=new;
}
H->next=new;
H->len++;
}
//尾插
void insert_tail(node_p H,int value)
{
node_p P=H;
while(P->next!=NULL)
{
P->next;
}
node_p new=create_data_node(value);
new->pri=P;
P->next=new;
H->len++;
}
//输出
void output(node_p H)
{
node_p P=H->next;
while(P)
{
printf("%d->",P->data);
P->next;
}
printf("NULL\n");
}
//头删
int delete_head(node_p H)
{
if(H==NULL||H->next==NULL)
{
return -1;
}
node_p del=H->next;
H->next=del->next;
if(del->next!=NULL)
{
del->next->prev=H;
}
free(del);del=NULL;
head->len--;
return 0;
}
//尾删
int delete_tail(node_p H)
{
if(H==NULL||H->next==NULL)
{
return -1;
}
node_p P=H;
while(P->next!=NULL)
{
P=P->next;
}
P->prev->next=NULL;
free(P);
P=NULL;
H->len--;
return 0
}
//任意位置插入
int insert_pos(node_p H,int pos,int data)
{
if(H==NULL||pos<1||pos>H->len+1)
{
return -1;
}
node_p P=H;
for(int i=0;i<pos-1;i++)
{
P=P->next;
}
node_p new=create_data_node(data);
if(new==NULL)
{
return -1;
}
new->next=P->next;
new->prev=P;
if(P->next!=NULL)
{
P->next->prev=new;
}
P->next=new;
H->len++;
return 0;
}
//按位置删除
int delete_pos(int pos,node_p H)
{
if(H==NULL||H-next==NULL||pos<1||pos>head->len)
{
return -1;
}
node_p P=H;
//找pos-1位置
for(int i=0;i<pos-1;i++)
{
P=P->next;
}
node_p del=P->next;
P->next=del->next;
if(del->next!=NULL)
{
del->next->prev=del->prev;
}
head->len--;
return 0;
}
//按值查找位置
int seach_value(int value,node_p H)
{
if(H==NULL||H->next==NULL||pos<1||pos>H->len)
{
return -1;
}
node_p P=H;
int pos=0;
while(P->data!=value)
{
P=P->next;
pos++;
}
return pos;
}
//按位置修改
int update_pos(int pos,int value,node_p H)
{
if(NULL==H || H->next==NULL|| pos<1 || pos>head->len)
{
return -1;
}
node_p P=H;
for(int i=0;i<pos;i++)
{
P=P->next;
}
P->data=value;
return 0;
}