C语言链表的示例分析
小编给大家分享一下C语言链表的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
我们提供的服务有:网站建设、成都网站制作、微信公众号开发、网站优化、网站认证、姑苏ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的姑苏网站制作公司
头文件:link_0505.h
/* 链表演示 */ #ifndef __LINK_0505 #define __LINK_0505 typedef struct node{ int num; struct node* p_next; }node; typedef struct { node head,tail; }link; //链表的初始化函数 void link_init(link *); //链表的清理函数 void link_deinit(link *); //判断链表是否空的函数 int link_empty(link *); //判断链表是否满的函数 int link_full(link *); //统计有效数字个数的函数 int link_size(link *); //在最前边插入数字的函数 int link_add_head(link *, int ); //在最后边插入新的数字的函数 int link_append(link *, int ); //把数字按照顺序插入到链表的函数 int link_insert(link *, int); //删除最前面数字的函数 int link_remove_head(link *); //删除最后一个有效数字 int link_remove_tail(link *); //删除某个给定数字的函数 int link_remove(link *, int ); //获得第一个有效数字的函数 int link_get_head(link *, int *); //获得最后一个有效数字的函数 int link_get_tail(link *, int *); //获得指定编号数字的函数 int link_get(link *, int *, int ); #endif
实现代码: link_0505.cpp
/* 链表演示 */ #include "stdlib.h" #include "link_0505.h" //链表的初始化函数 void link_init(link *p_link) { p_link->head.p_next = &(p_link->tail); } //链表的清理函数 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; free(p_mid); p_mid = NULL; } } //判断链表是否空的函数 int link_empty(link *p_link) { return p_link->head.p_next == &(p_link->tail); } //判断链表是否满的函数 int link_full(link *p_link) { return 0; } //统计有效数字个数的函数 int link_size(link *p_link) { int cnt = 0; node *p_node = 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)) { cnt++; } } return cnt; } //在最前边插入数字的函数 int link_add_head(link *p_link, int num) { node *p_temp = (node *)malloc(sizeof(node)); if (!p_temp) { return 0; } p_temp->num = num; 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_temp; p_temp->p_next = p_mid; return 1; } //在最后边插入新的数字的函数 int link_append(link *p_link, int num) { node *p_tmp = (node *)malloc(sizeof(node)); node *p_node = NULL; if (!p_tmp) { return 0; } p_tmp->num = num; 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_first->p_next = p_tmp; p_tmp->p_next = p_mid; break; } } return 1; } //把数字按照顺序插入到链表的函数 int link_insert(link *p_link, int num) { node* p_temp = (node *)malloc(sizeof(node)); node* p_node = NULL; if (!p_temp) { return 0; } p_temp->num = num; p_temp->p_next = 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 > p_temp->num) { p_first->p_next = p_temp; p_temp->p_next = p_mid; break; } } return 0; } //删除最前面数字的函数 int 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 0; } p_first->p_next = p_last; free(p_mid); p_mid = NULL; } //删除最后一个有效数字 int link_remove_tail(link *p_link) { node *p_node = 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_last == &(p_link->tail)) { p_first->p_next = p_last; free(p_mid); p_mid = NULL; return 1; } } return 0; } //删除某个给定数字的函数 int link_remove(link *p_link, int num) { node *p_node = 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; free(p_mid); p_mid = NULL; return 1; } } return 0; } //获得第一个有效数字的函数 int link_get_head(link *p_link, int *p_num) { if (p_link->head.p_next == &(p_link->tail)) { return 0; } 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_num = p_mid->num; return 1; } //获得最后一个有效数字的函数 int link_get_tail(link *p_link, int *p_num) { node *p_node = 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_last == &(p_link->tail)) { *p_num = p_mid->num; return 1; } } return 0; } //获得指定编号数字的函数 int link_get(link *p_link, int *p_num, int num) { int cnt = 0; node *p_node = 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) && cnt == num) { *p_num = p_mid->num; return 1; } cnt++; } return 0; }
测试函数:
/* * 链表测试 * */ #include#include "link_0505.h" int main() { int size = 0, num = 0, val = 0; link lnk = {0}; link_init(&lnk); link_add_head(&lnk, 30); link_add_head(&lnk, 20); link_append(&lnk, 90); link_append(&lnk, 100); link_insert(&lnk, 50); link_insert(&lnk, 60); link_insert(&lnk, 40); link_insert(&lnk, 80); link_insert(&lnk, 70); size = link_size(&lnk); for (num = 0;num <= size - 1;num++) { link_get(&lnk, &val, num); printf("%d ", val); } printf("\n"); printf("------------------"); link_remove_head(&lnk); link_remove_tail(&lnk); link_remove(&lnk, 70); size = link_size(&lnk); for (num = 0;num <= size - 1;num++) { link_get(&lnk, &val, num); printf("%d ", val); } printf("\n"); link_get_head(&lnk, &val); printf("最前面的数字是%d\n", val); link_get_tail(&lnk, &val); printf("最后面的数字是%d\n", val); link_deinit(&lnk); return 0; }
以上是“C语言链表的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
当前文章:C语言链表的示例分析
标题来源:http://abwzjs.com/article/ppsssc.html