1.头文件
#ifndef __LINK_H__
#define __LINK_H__
typedef struct node {
int num;
struct node *p_next;
struct node *p_prev;
} node;
typedef struct {
node head, tail;
node *p_cur;
} link;
//初始化函数
void link_init(link *);
//清理函数
void link_deinit(link *);
//判断链表是否空的函数
int link_empty(const link *);
//判断链表是否满的函数
int link_full(const link *);
//获得有效节点个数的函数
int link_size(const link *);
//把新数字加入到所有数字的前面
void link_add_head(link *, int);
//把新数字加入到原有数字的最后
void link_append(link *, int);
//按顺序加入新数字
void link_insert_in_order(link *, int);
//删除最前面数字的函数
void link_remove_head(link *);
//删除最后有效节点
void link_remove_tail(link *);
//删除某个数字所在的节点
void link_remove(link *, int);
//获得最前面的数字
int link_get_head(const link *, int *);
//获得最后一个数字的函数
int link_get_tail(const link *, int *);
//根据编号找到数字的函数
int link_get(const link *, int, int *);
//开始从前向后依次遍历链表中每个有效节点
void link_begin(link *p_link);
//在从前向后的遍历过程中获得下一个数字
int link_next(link *p_link, int *);
void link_rbegin(link *p_link);
int link_prev(link *p_link, int *);
#endif //__LINK_H__
2.源文件
/************link.c************/
#include <stdlib.h>
#include "link.h"
//初始化函数
void link_init(link *p_link) {
p_link->head.p_next = &(p_link->tail);
p_link->tail.p_next = NULL;
p_link->tail.p_prev = &(p_link->head);
p_link->head.p_prev = NULL;
p_link->p_cur = NULL;
}
//清理函数
void link_deinit(link *p_link) {
while (p_link->head.p_next != &(p_link->tail)) {
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
p_last->p_prev = p_first;
free(p_mid);
p_mid = NULL;
}
p_link->p_cur = NULL;
}
//判断链表是否空的函数
int link_empty(const link *p_link) {
return p_link->head.p_next == &(p_link->tail);
}
//判断链表是否满的函数(此处假设永远不为满)
int link_full(const link *p_link) {
return 0;
}
//获得有效节点个数的函数
int link_size(const link *p_link) {
int cnt = 0;
const node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) {
const node *p_first = p_node;
const node *p_mid = p_first->p_next;
const node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail)) {
cnt++;
}
}
return cnt;
}
//把新数字加入到所有数字的前面
void link_add_head(link *p_link, int num) {
node *p_first = NULL, *p_mid = NULL, *p_last = NULL;
node *p_node = (node *)malloc(sizeof(node));
if (!p_node) {
return ;
}
p_node->num = num;
p_node->p_next = NULL;
p_node->p_prev = NULL;
p_first = &(p_link->head);
p_mid = p_first->p_next;
p_last = p_mid->p_next;
p_first->p_next = p_node;
p_node->p_prev = p_first;
p_node->p_next = p_mid;
p_mid->p_prev = p_node;
p_link->p_cur = NULL;
}
//把新数字加入到原有数字的最后
void link_append(link *p_link, int num) {
node *p_first = NULL, *p_mid = NULL, *p_last = NULL;
node *p_node = (node *)malloc(sizeof(node));
if (!p_node) {
return ;
}
p_node->num = num;
p_node->p_next = NULL;
p_node->p_prev = NULL;
p_first = p_link->tail.p_prev;
p_mid = p_first->p_next;
p_last = p_mid->p_next;
p_first->p_next = p_node;
p_node->p_prev = p_first;
p_node->p_next = p_mid;
p_mid->p_prev = p_node;
p_link->p_cur = NULL;
}
//按顺序加入新数字
void link_insert_in_order(link *p_link, int num) {
node *p_tmp = NULL;
node *p_node = (node *)malloc(sizeof(node));
if (!p_node) {
return ;
}
p_node->num = num;
p_node->p_next = NULL;
p_link->p_cur = NULL;
for (p_tmp = &(p_link->head);p_tmp != &(p_link->tail);p_tmp = p_tmp->p_next) {
node *p_first = p_tmp;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid == &(p_link->tail) || p_mid->num > num) {
p_first->p_next = p_node;
p_node->p_prev = p_first;
p_node->p_next = p_mid;
p_mid->p_prev = p_node;
break;
}
}
}
//删除最前面数字的函数
void link_remove_head(link *p_link) {
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_link->head.p_next == &(p_link->tail)) {
return ;
}
p_first->p_next = p_last;
p_last->p_prev = p_first;
free(p_mid);
p_mid = NULL;
p_link->p_cur = NULL;
}
//删除最后有效节点
void link_remove_tail(link *p_link) {
node *p_first = NULL, *p_mid = NULL, *p_last = NULL;
if (p_link->head.p_next == &(p_link->tail)) {
return ;
}
p_first = p_link->tail.p_prev->p_prev;
p_mid = p_first->p_next;
p_last = p_mid->p_next;
p_first->p_next = p_last;
p_last->p_prev = p_first;
free(p_mid);
p_mid = NULL;
p_link->p_cur = NULL;
}
//删除某个数字所在的节点
void link_remove(link *p_link, int num) {
node *p_node = NULL;
if (p_link->head.p_next == &(p_link->tail)) {
return ;
}
p_link->p_cur = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) {
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && p_mid->num == num) {
p_first->p_next = p_last;
p_last->p_prev = p_first;
free(p_mid);
p_mid = NULL;
break;
}
}
}
//获得最前面的数字
int link_get_head(const link *p_link, int *p_num) {
if (p_link->head.p_next == &(p_link->tail)) {
return 0;
}
*p_num = p_link->head.p_next->num;
return 1;
}
//获得最后一个数字的函数
int link_get_tail(const link *p_link, int *p_num) {
const node *p_node = NULL;
if (p_link->head.p_next == &(p_link->tail)) {
return 0;
}
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) {
const node *p_first = p_node;
const node *p_mid = p_first->p_next;
const node *p_last = p_mid->p_next;
if (p_last == &(p_link->tail)) {
*p_num = p_mid->num;
return 1;
}
}
}
//根据编号找到数字的函数
int link_get(const link *p_link, int sn, int *p_num) {
int cnt = 0;
const node *p_node = NULL;
if (p_link->head.p_next == &(p_link->tail)) {
return 0;
}
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) {
const node *p_first = p_node;
const node *p_mid = p_first->p_next;
const node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && cnt == sn) {
*p_num = p_mid->num;
return 1;
}
cnt++;
}
return 0;
}
//准备开始从前向后遍历链表中的每个有效节点
void link_begin(link *p_link) {
p_link->p_cur = &(p_link->head);
}
//在从前向后的变量过程中获得链表中下一个数字
int link_next(link *p_link, int *p_num) {
if (!(p_link->p_cur)) {
return 0;
}
p_link->p_cur = p_link->p_cur->p_next;
if (p_link->p_cur == &(p_link->tail)) {
p_link->p_cur = NULL;
return 0;
}
else {
*p_num = p_link->p_cur->num;
return 1;
}
}
//准备开始从后向前遍历每个有效节点
void link_rbegin(link *p_link) {
p_link->p_cur = &(p_link->tail);
}
//在从后向前遍历过程中获得前一个数字
int link_prev(link *p_link, int *p_num) {
if (!(p_link->p_cur)) {
return 0;
}
p_link->p_cur = p_link->p_cur->p_prev;
if (p_link->p_cur == &(p_link->head)) {
p_link->p_cur = NULL;
return 0;
}
else {
*p_num = p_link->p_cur->num;
return 1;
}
}
3.测试函数
#include <stdio.h>
#include "link.h"
int main() {
int num = 0, num1 = 0;
link lnk = {0};
link_init(&lnk);
link_append(&lnk, 50);
link_append(&lnk, 60);
link_append(&lnk, 70);
link_append(&lnk, 80);
link_append(&lnk, 90);
link_add_head(&lnk, 40);
link_add_head(&lnk, 30);
link_add_head(&lnk, 20);
link_add_head(&lnk, 10);
link_insert_in_order(&lnk, 45);
link_insert_in_order(&lnk, 65);
link_insert_in_order(&lnk, 25);
link_insert_in_order(&lnk, 55);
link_remove_head(&lnk);
link_remove_tail(&lnk);
link_remove(&lnk, 50);
printf("数字个数是%d\n", link_size(&lnk));
link_get_head(&lnk, &num);
printf("最前面的数字是%d\n", num);
link_get_tail(&lnk, &num);
printf("最后面的数字是%d\n", num);
for (num1 = 0;;num1++) {
if (!link_get(&lnk, num1, &num)) {
break;
}
printf("%d ", num);
}
printf("\n");
link_begin(&lnk);
while (1) {
if (!link_next(&lnk, &num)) {
break;
}
printf("%d ", num);
}
printf("\n");
link_rbegin(&lnk);
while (1) {
if (!link_prev(&lnk, &num)) {
break;
}
printf("%d ", num);
}
printf("\n");
link_deinit(&lnk);
return 0;
}