收藏 分享(赏)

无头非循环单链表的常见操作.doc

上传人:buyk185 文档编号:4746140 上传时间:2019-01-10 格式:DOC 页数:6 大小:93.50KB
下载 相关 举报
无头非循环单链表的常见操作.doc_第1页
第1页 / 共6页
无头非循环单链表的常见操作.doc_第2页
第2页 / 共6页
无头非循环单链表的常见操作.doc_第3页
第3页 / 共6页
无头非循环单链表的常见操作.doc_第4页
第4页 / 共6页
无头非循环单链表的常见操作.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

1、无头非循环单链表的增删查/slist.h#pragma oncetypedef int elem_t;struct node_t elem_t data;struct node_t *next;/ADT: 抽象数据类型(强调两点:数据抽象和数据封装)/数据抽象:关注的是所要完成的功能,以及对外部如何定义接口/数据封装:将数据或对象的外部特征和具体函数的实现分离,隐藏了函数的实现细节/链表类struct slist_t struct node_t *head; /指向首元节点的指针struct node_t *(*add)(struct slist_t *, elem_t);struct nod

2、e_t *(*add_tail)(struct slist_t *, elem_t);struct node_t *(*for_each)(struct slist_t *, void (*)(struct node_t *);struct node_t *(*search)(struct slist_t *, struct node_t *);struct node_t *(*search_last)(struct slist_t *);void (*del)(struct slist_t *, elem_t);int (*is_empty)(struct slist_t *);void (

3、*destroy)(struct slist_t *); /析构函数;/初始化链表void slist_init(struct slist_t *); /构造函数/slist.c#include #include #include “slist.h“static struct node_t *list_add(struct slist_t *slist, elem_t data)struct node_t *node = (struct node_t *)malloc(sizeof(struct node_t);if (!node) fprintf(stderr, “no enough mem

4、!n“);return NULL;/初始化节点node-data = data;node-next = NULL;if (!slist-head) /链表为空,head 指向新插入的第一个节点slist-head = node; else node-next = slist-head;slist-head = node;return node;static struct node_t *list_add_tail(struct slist_t *slist, elem_t data)struct node_t *node = (struct node_t *)malloc(sizeof(str

5、uct node_t);if (!node) fprintf(stderr, “no enough mem!n“);return NULL;/初始化节点node-data = data;node-next = NULL;struct node_t *cur = slist-head;if (!slist-head) /链表为空,head 指向新插入的第一个节点slist-head = node; else for (; cur-next; cur = cur-next) cur-next = node;return node;static struct node_t *list_for_eac

6、h(struct slist_t *slist, void (*todo)(struct node_t *)if (slist-is_empty(slist) /链表为空return NULL;struct node_t *cur = slist-head;for (; cur; cur = cur-next) todo(cur);printf(“n“);return slist-head;static struct node_t *search(struct slist_t *slist, struct node_t *node)if (slist-is_empty(slist) /链表为空

7、return NULL;struct node_t *cur = slist-head;for (; cur != node; cur = cur-next) if (cur) /没找到printf(“not found!n“);return NULL;return cur;static struct node_t *search_last(struct slist_t *slist)if (slist-is_empty(slist) /链表为空return NULL;struct node_t *cur = slist-head;for (; cur-next; cur = cur-next

8、) return cur;static void list_del(struct slist_t *slist, elem_t data)if (slist-is_empty(slist) /链表为空return;struct node_t *tmp = NULL;struct node_t *cur = slist-head;while (cur) if (cur-data = data) /找到了if (slist-head = cur) /要删除的节点是第一个节点slist-head = cur-next;cur-next = NULL;free(cur);cur = slist-hea

9、d; else /要删除的节点是除第一个节点之外的节点tmp-next = cur-next;cur-next = NULL;free(cur);cur = tmp-next; else /没找到tmp = cur;cur = tmp-next;static int list_is_empty(struct slist_t *slist)return !slist-head;/安全遍历/ for循环中的第二个部分的1,是为了解决当 cur是最后一个节点时,不能进入 for循环进行操作的问题。#define list_for_each_safe(cur, tmp, list) for (cur

10、= (list)-head; cur cur = tmp)static void list_destroy(struct slist_t *slist)if (slist-is_empty(slist) /链表为空return;struct node_t *cur = NULL;struct node_t *tmp = NULL;list_for_each_safe(cur, tmp, slist) slist-del(slist, cur-data);free(slist);void slist_init(struct slist_t *slist)slist-head = NULL;sli

11、st-add = list_add;slist-add_tail = list_add_tail;slist-for_each = list_for_each;slist-search = search;slist-search_last = search_last;slist-del = list_del;slist-is_empty = list_is_empty;slist-destroy = list_destroy;/测试 test.c#include #include #include “slist.h“static void callback(struct node_t *nod

12、e)printf(“%d “, node-data);int main(void)struct slist_t *slist = (struct slist_t *)malloc(sizeof(struct slist_t);if (!slist) printf(“no enough mem!n“);return 1;slist_init(slist);#define CNT 10int i = 0;for (; i add(slist, i);slist-add_tail(slist, i);slist-for_each(slist, callback);printf(“search:n“)

13、;struct node_t *find = NULL;if (find = slist-search(slist, slist-head) printf(“found, data = %dn“, find-data); printf(“the last node = %dn“, slist-search_last(slist)-data);printf(“insert new node: n“);slist-add(slist, 0);slist-add_tail(slist, 0);slist-for_each(slist, callback);printf(“del:n“);slist-del(slist, 0);slist-for_each(slist, callback);slist-destroy(slist); return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 实用文档 > 统计图表

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报