1.概念
由于在单向链表中,知道其中某个结点地址不能够找到其他所有的结点,所以将单向链表该为单向循环链表,这样知道其中一个结点的地址,可以找到其他每一个结点。但是,当链表中的数据特别多的时候,查找起来效率就非常低, 故,设想,如果在创建链表时,能够在结点中同时保存前后结点的地址,则可以提高效率,例如:
2.节点类型
typedef int datatype;
typedef struct double_listnode {
datatype data;
struct double_listnode *next, *prev;
} doublelist_node, *doublelist_pnode;
3.初始化
void init_doublelist(doublelist_pnode *H)
{
*H = (doublelist_pnode)malloc(sizeof(doublelist_node));
if ((*H) == NULL) {
perror("malloc");
exit(1);
}
(*H)->next = (*H)->prev = (*H);
}
4.插入(前插、后插)
5.删除(删除p指向的结点后面、前面,p节点)
6.遍历
7.实例
// doublelist.h
#ifndef __DOUBLELIST_H__
#define __DOUBLELIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int datatype;
typedef struct double_listnode {
datatype data;
struct double_listnode *next, *prev;
} doublelist_node, *doublelist_pnode;
extern void init_doublelist(doublelist_pnode *H);
extern void befor_inster_doublelist(doublelist_pnode new, doublelist_pnode p);
extern void after_inster_doublelist(doublelist_pnode new, doublelist_pnode p);
extern void befor_del_doublelist(doublelist_pnode p);
extern void now_del_doublelist(doublelist_pnode p);
extern void after_del_doublelist(doublelist_pnode p);
extern void show_doublelist(doublelist_pnode h);
#endif
// doublelist.c
#include "doublelist.h"
// 初始化
void init_doublelist(doublelist_pnode *H)
{
*H = (doublelist_pnode)malloc(sizeof(doublelist_node));
if ((*H) == NULL) {
perror("malloc");
exit(1);
}
(*H)->next = (*H)->prev = (*H);
}
// p前插入
void befor_inster_doublelist(doublelist_pnode new, doublelist_pnode p)
{
new->prev = p->prev;
p->prev->next = new;
new->next = p;
p->prev = new;
}
// p后插入
void after_inster_doublelist(doublelist_pnode new, doublelist_pnode p)
{
new->next = p->next;
p->next->prev = new;
new->prev = p;
p->next = new;
}
// 删除P指向的结点前面的结点
void befor_del_doublelist(doublelist_pnode p)
{
doublelist_pnode q;
q = p->prev;
p->prev = q->prev;
q->prev->next = p;
}
// 删除P指向的结点
void now_del_doublelist(doublelist_pnode p)
{
p->next->prev = p->prev;
p->prev->next = p->next;
}
// 删除P指向结点后面的结点
void after_del_doublelist(doublelist_pnode p)
{
doublelist_pnode q;
q = p->next;
p->next = q->next;
q->next->prev = p;
}
// 遍历
void show_doublelist(doublelist_pnode h)
{
doublelist_pnode p;
for (p = h->next; p != h; p = p->next)
printf("%d\t",p->data);
printf("\n");
}
// doubletest.h
#ifndef __DOUBLETEST_H__
#define __DOUBLETEST_H__
#include "doublelist.h"
extern void creat_doubletest(doublelist_pnode h, int n);
extern void sort_doubletest(doublelist_pnode h);
#endif
// doubletest.c
#include "doubletest.h"
void creat_doubletest(doublelist_pnode h, int n)
{
doublelist_pnode new;
for (int i = 0; i < n; i++) {
new = (doublelist_pnode)malloc(sizeof(doublelist_node));
if (NULL == new) {
perror("malloc");
exit(1);
}
new->data = i+1;
befor_inster_doublelist(new, h);
show_doublelist(h);
}
}
void sort_doubletest(doublelist_pnode h)
{
doublelist_pnode q, p;
for (p = h->prev; p != h; ) {
if(p->data % 2 != 0) {
p = p->prev;
} else {
q = p;
p = p->prev;
now_del_doublelist(q);
befor_inster_doublelist(q, h);
}
}
}
// double_main.c
#include "doublelist.h"
#include "doubletest.h"
int main(void)
{
doublelist_pnode h;
int n;
init_doublelist(&h);
printf("请输入链表的长度:");
scanf("%d",&n);
creat_doubletest(h,n);
printf("奇数升序偶数降序\n");
sort_doubletest(h);
show_doublelist(h);
return 0;
}