您的位置:首页 > 其它

链表1

2016-07-13 17:22 253 查看
链表也是一种数据,可以把链表看作是对链式

    物理结构进行管理的工具

链表中通常包含一个链式物理结构以及对它们进行

    管理的函数

使用链表可以把对链式物理结构进行管理的功能

    和其他功能区分开,这就可以降低开发的难度

链表基本功能包括插入,删除和遍历

遍历功能可以按顺序依次使用链式物理结构中每个

    数字

只能沿着一个方向遍历的链表叫单向链表

可以沿着两个方向遍历的链表叫双向链表

双向链表中每个节点里必须包含两个指针,一个

    指针指向前一个节点,另外一个指针指向
后一个节点

双向链表中两个相邻的节点之间的关系由两个

    指针决定,如果要调整他们之间的关系也
需要调整这两个指针

#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: