目录
一、结构体
typedef struct dblist
{
union {
int len;
int data;
};
struct dblist *front;
struct dblist *next;
} dblist;
二、创建双向头结点
dblist *dblist_create()
{
dblist *L = (dblist *)malloc(sizeof(dblist));
if (L == NULL)
{
printf("内存申请失败\n");
return NULL;
}
printf("内存申请成功\n");
L->len = 0;
L->front = NULL;
L->next = NULL;
return L;
}
三、创建双向子节点
dblist *dbnode_create(int data)
{
dblist *p = (dblist *)malloc(sizeof(dblist));
if (p == NULL)
{
printf("节点内存空间创建失败\n");
return NULL;
}
p->data = data;
p->front = NULL;
p->next = NULL;
return p;
}
四、判空
int empty_dblist(dblist *L)
{
if (L == NULL)
{
printf("入参为空\n");
return -1;
}
return L->next == NULL && L->front == NULL ? 1 : 0;
}
五、插入
1.头插
void insert_head_dblist(dblist *L, int data)
{
if (L == NULL)
{
printf("入参为空\n");
return;
}
dblist *p = dbnode_create(data);
if (L->next == NULL)
{
p->next = L->next;
p->front = L;
L->next = p;
}
else
{
p->next = L->next;
L->next->front = p;
p->front = L;
L->next = p;
}
L->len++;
}
2.尾插
void insert_tail_dblist(dblist *L, int data)
{
if (L == NULL)
{
printf("入参为空\n");
return;
}
dblist *p = L;
while (p->next != NULL)
p = p->next;
dblist *newnode = dbnode_create(data);
newnode->next = NULL;
p->next = newnode;
newnode->front = p;
L->len++;
}
3.任意位置插入
void insert_index_dblist(dblist *L, int index, int data)
{
if (L == NULL)
{
printf("入参为空\n");
return;
}
else if (index < 1 || index > L->len + 1)
{
printf("位置不合理,请检查\n");
return;
}
else if (index == L->len + 1)
{
insert_tail_dblist(L, data);
return;
}
dblist *p = L;
for (int i = 1; i < index; i++, p = p->next)
;
dblist *newnode = dbnode_create(data);
newnode->next = p->next;
p->next->front = newnode;
p->next = newnode;
newnode->front = p;
L->len++;
}
六、按位置查找
dblist *search_dblist(dblist *L, int index)
{
if (L == NULL)
{
printf("入参为空\n");
return NULL;
}
if (index < 1 || index > L->len)
{
printf("位置不合理,请检查\n");
return NULL;
}
dblist *p = L->next;
for (int i = 1; i < index; i++, p = p->next)
;
return p;
}
七、头删
1.头删
void delete_head_dblist(dblist *L)
{
if (L == NULL)
{
printf("入参为空\n");
return;
}
if (empty_dblist(L))
{
printf("链表为空,无法删除\n");
return;
}
dblist *p = L->next;
if (p->next == NULL)
{
free(p);
L->next = NULL;
}
else
{
L->next = p->next;
p->next->front = p->front;
free(p);
}
L->len--;
}
2.按位置删除
void delete_index_dblist(dblist *L, int index)
{
if (L == NULL)
{
printf("入参为空\n");
return;
}
if (index < 1 || index > L->len)
{
printf("位置不合理,请检查\n");
return;
}
dblist *p = L->next;
for (int i = 1; i < index; i++, p = p->next)
;
if (p->next == NULL)
{
p->front->next = NULL;
free(p);
return;
}
p->front->next = p->next;
p->next->front = p->front;
free(p);
L->len--;
}
八、打印
void print_dblist(dblist *L)
{
if (L == NULL)
{
printf("入参为空\n");
return;
}
else if (empty_dblist(L))
{
printf("链表为空\n");
return;
}
dblist *p = L->next;
while (p != NULL)
{
printf("p->data=%d\n", p->data);
p = p->next;
}
}