#ifndef __DOUBLE_LIST_H__
#define __DOUBLE_LIST_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
struct node *pri;
}node,*node_p;
node_p create_double(); //创建链表
node_p create_node(int data); //创建节点
void insert_head(node_p H,int data); //头插
void show(node_p H); //输出
void dele_head(node_p H);
void insert_tail(node_p H,int data); //尾插
void dele_tail(node_p H); //尾删
void insert_pos(node_p H,int data,int pos);//按位置插入
void dele_pos(node_p H,int pos); //按位置删除
#endif
#include "double_list.h"
node_p create_double()
{
node_p H=(node_p)malloc(sizeof(node));
if(H==NULL)
{
printf("内存申请失败\n");
return NULL;
}
H->data=0;
H->next=NULL;
H->pri=NULL;
return H;
}
node_p create_node(int data)
{
node_p H=(node_p)malloc(sizeof(node));
if(H==NULL)
{
printf("内存申请失败\n");
return NULL;
}
H->data=data;
H->next=NULL;
H->pri=NULL;
return H;
}
int empty_double(node_p H)
{
if(H==NULL)
{
printf("参数为空\n");
return -1;
}
return H->next==NULL?1:0;
}
void insert_head(node_p H,int data)
{
if(H==NULL)
{
printf("参数为空\n");
return;
}
node_p new=create_node(data);
new->next=H->next;
if(H->next!=NULL)
{
H->next->pri=new;
}
new->pri=H;
H->next=new;
H->data++;
}
void show(node_p H)
{
if(H==NULL)
{
printf("参数为空\n");
return;
}
if(empty_double(H))
{
printf("链表为空,无需输出\n");
return;
}
node_p p=H->next;
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("NULL\n");
}
void dele_head(node_p H)
{
if(H==NULL)
{
printf("参数为空\n");
return;
}
if(empty_double(H))
{
printf("链表为空,无需删除\n");
return;
}
node_p del=H->next;
if(del->next!=NULL)
{
H->next->pri=H;
H->next=del->next;
}
else
{
H->next=NULL;
}
free(del);
H->data--;
}
void insert_tail(node_p H,int data)
{
if(H==NULL)
{
printf("参数为空\n");
return;
}
node_p p=H;
while(p->next!=NULL)
{
p=p->next;
}
node_p new=create_node(data);
p->next=new;
new->pri=p;
H->data++;
}
void dele_tail(node_p H)
{
if(H==NULL)
{
printf("参数为空\n");
return;
}
if(empty_double(H))
{
printf("链表为空,无需删除\n");
return;
}
node_p p=H;
while(p->next->next!=NULL)
{
p=p->next;
}
node_p del=p->next;
p->next=NULL;
free(del);
H->data--;
}
void insert_pos(node_p H,int data,int pos)
{
if(H==NULL)
{
printf("参数为空\n");
return;
}
node_p p=H;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
node_p new=create_node(data);
if(p->next!=NULL)
{
new->next=p->next;
p->next->pri=new;
}
new->pri=p;
p->next=new;
H->data++;
}
void dele_pos(node_p H,int pos)
{
if(H==NULL)
{
printf("参数为空\n");
return;
}
if(empty_double(H))
{
printf("链表为空,无需删除\n");
return;
}
node_p p=H;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
node_p del=p->next;
if(del->next!=NULL)
{
p->next->pri=H;
p->next=del->next;
}
else
{
p->next=NULL;
}
free(del);
H->data--;
}
2:链表和顺序表的区别
顺序表和链表都是线性结构,是一对一的关系,但是顺序表的物理结构是顺序结构,地址上是连续的,但是链表的物理结构是链式结构,物理地址是不连续的。
3:逆置
void reve_double(node_p H)
{
if(H==NULL)
{
printf("参数为空\n");
return;
}
if(empty_double(H))
{
printf("链表为空,无需逆置\n");
return;
}
if(H->next->next==NULL)
{
printf("只有一个元素,无需逆置\n");
return;
}
node_p p=H->next->next;
H->next->next=NULL;
while(p!=NULL)
{
node_p q=p->next;
p->next=H->next;
H->next->pri=p;
p->pri=H;
H->next=p;
p=q;
}
/* p->next=H->next;
H->next->pri=p;
p->pri=H;
H->next=p;*/
}