双向链表

#ifndef   __03LINK_H__
#define   __03LINK_H__
//分配存储区的函数
void link_init();
//释放存储区的函数
void link_deinit();
//在链的开头插入新数字
void insert_head(int );
//删除最前边的数字
void remove_head();
//获得第一个数字的函数
int get_head();
//在链的末尾插入新数字
void insert_tail(int );
//删除最后边的数字
void remove_tail();
//获得最后一个数字的函数
int get_tail();
//判断链中是否为空
int link_empty();
//获得数字个数的函数
int link_size();
//获得下一个数字
int next();
//开始遍历
void begin();
//开始反向遍历
void rbegin();
//获得前一个数字
int prev();

#endif   //__03LINK_H__



#ifndef   __03LINK_H__
#define   __03LINK_H__
//分配存储区的函数
void link_init();
//释放存储区的函数
void link_deinit();
//在链的开头插入新数字
void insert_head(int );
//删除最前边的数字
void remove_head();
//获得第一个数字的函数
int get_head();
//在链的末尾插入新数字
void insert_tail(int );
//删除最后边的数字
void remove_tail();
//获得最后一个数字的函数
int get_tail();
//判断链中是否为空
int link_empty();
//获得数字个数的函数
int link_size();
//获得下一个数字
int next();
//开始遍历
void begin();
//开始反向遍历
void rbegin();
//获得前一个数字
int prev();
#endif   //__03LINK_H__





#include <stdlib.h>
#include "03link.h"
typedef struct node {
int num;
struct node *p_next;
struct node *p_prev;
} node;
static node head, tail;
static node *p_cur;
//分配存储区的函数
void link_init() {
head.p_next = &tail;
tail.p_prev = &head;
}
//释放存储区的函数
void link_deinit() {
while (head.p_next != &tail) {
node *p_first = &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;
}
}
//在链的开头插入新数字
void insert_head(int num) {
node *p_first = &head;
node *p_second = p_first->p_next;
    node *p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = num;
p_tmp->p_next = NULL;
p_tmp->p_prev = NULL;
p_first->p_next = p_tmp;
p_tmp->p_prev = p_first;
p_tmp->p_next = p_second;
p_second->p_prev = p_tmp;
}
}
//删除最前边的数字
void remove_head() {
if (head.p_next != &tail) {
node *p_first = &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_cur = NULL;
}
}
//获得第一个数字的函数
int get_head() {
if (head.p_next == &tail) {
return -1;
}
else {
return head.p_next->num;
}
}
//在链的末尾插入新数字
void insert_tail(int num) {
node *p_first = tail.p_prev;
node *p_second = &tail;
node *p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = num;
p_tmp->p_next = NULL;
p_tmp->p_prev = NULL;
p_first->p_next = p_tmp;
p_tmp->p_prev = p_first;
p_tmp->p_next = p_second;
p_second->p_prev = p_tmp;
}
}
//删除最后边的数字
void remove_tail() {
if (tail.p_prev != &head) {
node *p_last = &tail;
node *p_mid = p_last->p_prev;
node *p_first = p_mid->p_prev;
p_first->p_next = p_last;
p_last->p_prev = p_first;
free(p_mid);
p_mid = NULL;
p_cur = NULL;
}
}
//获得最后一个数字的函数
int get_tail() {
if (head.p_next == &tail) {
return -1;
}
else {
return tail.p_prev->num;
}
}
//判断链中是否为空
int link_empty() {
return head.p_next == &tail;
}
//获得数字个数的函数
int link_size() {
int cnt = 0;
node *p_node = NULL;
for (p_node = &head;p_node != &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 != &tail) {
cnt++;
}
}
return cnt;
}
//获得下一个数字
int next() {
if (p_cur) {
int ret = p_cur->num;
p_cur = p_cur->p_next;
if (p_cur == &tail) {
p_cur = NULL;
}
return ret;
}
return -1;
}
//开始遍历
void begin() {
if (head.p_next != &tail) {
p_cur = head.p_next;
}
}
//开始反向遍历
void rbegin() {
if (head.p_next != &tail) {
p_cur = tail.p_prev;
}
}
//获得前一个数字
int prev() {
if (p_cur) {
int ret = p_cur->num;
p_cur = p_cur->p_prev;
if (p_cur == &head) {
p_cur = NULL;
}
return ret;
}
return -1;
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值