链表1

链表也是一种数据,可以把链表看作是对链式
    物理结构进行管理的工具
链表中通常包含一个链式物理结构以及对它们进行
    管理的函数
使用链表可以把对链式物理结构进行管理的功能
    和其他功能区分开,这就可以降低开发的难度


链表基本功能包括插入,删除和遍历
遍历功能可以按顺序依次使用链式物理结构中每个
    数字


只能沿着一个方向遍历的链表叫单向链表
可以沿着两个方向遍历的链表叫双向链表
双向链表中每个节点里必须包含两个指针,一个
    指针指向前一个节点,另外一个指针指向
后一个节点
双向链表中两个相邻的节点之间的关系由两个
    指针决定,如果要调整他们之间的关系也
需要调整这两个指针



#ifndef   __02LINK_H__
#define   __02LINK_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();
//删除指定数字
void remove_num(int );
//获得下一个数字
int next();
//开始遍历
void begin();
#endif   //__02LINK_H__


/*

   链表练习
   */
#include <stdlib.h>
#include "02link.h"
typedef struct node {
int num;
struct node *p_next;
} node;
static node head, tail;
static node *p_cur;
//分配存储区的函数
void link_init() {
head.p_next = &tail;
}
//释放存储区的函数
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;
free(p_mid);
p_mid = NULL;
}
}
//在链的开头插入新数字
void insert_head(int num) {
    node *p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = num;
p_tmp->p_next = NULL;
node *p_first = &head;
node *p_second = p_first->p_next;
p_first->p_next = p_tmp;
p_tmp->p_next = p_second;
}
}
//删除最前边的数字
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;
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_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_first->p_next == &tail) {
node *p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = num;
p_tmp->p_next = NULL;
p_first->p_next = p_tmp;
p_tmp->p_next = p_mid;
}
break;
}
}
}
//删除最后边的数字
void remove_tail() {
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->p_next == &tail) {
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
p_cur = NULL;
break;
}
}
}
//获得最后一个数字的函数
int get_tail() {
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->p_next == &tail) {
return p_mid->num;
}
}
return -1;
}
//判断链中是否为空
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;
}
//从链中删除指定数字
void remove_num(int num) {
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 && p_mid->num == num) {
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
p_cur = NULL;
break;
}
}
}
//获得下一个数字
int next() {
if (!p_cur) {
return -1;
}
else {
   int ret = p_cur->num;
   p_cur = p_cur->p_next;
   if (p_cur == &tail) {
   p_cur = NULL;
   }
   return ret;
}
}
//开始遍历函数
void begin() {
if (head.p_next != &tail) {
p_cur = head.p_next;
}
else {
p_cur = NULL;
}
}

/*
   链表练习
   */
#include <stdio.h>
int main() {
int num = 0;
link_init();
insert_head(1);
insert_head(2);
insert_tail(3);
    begin();
while ((num = next()) != -1) {
printf("%d ", num);
}
printf("\n");
insert_head(4);
insert_head(5);
remove_num(1);
remove_head();
remove_tail();
    begin();
while ((num = next()) != -1) {
printf("%d ", num);
}
printf("\n");
link_deinit();
return 0;
}














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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零一2035

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值