当前位置:   article > 正文

数据结构大总结:有头单链表、循环链表、双线链表、栈、队列、递归、快速排序、树和哈希。_哪些排序用到单链,双链,循环链

哪些排序用到单链,双链,循环链

一、什么是数据结构?

1.1数据结构的概念

数据:能够输入到计算机中的描述客观事物的信息集合 (需要计算机处理的对象)

结构:数据之间的关系

算法:对特定问题求解的一种描述

数据结构是独立于计算机、不依赖开发语言

数据结构是研究 数据逻辑关系、存储关系、及操作的知识

1.2数据结构的三要素

 1.2.1逻辑结构:

1.2.1.1概念:

        数据元素之间的关系,和存储无关,独立于计算机指数据之间在逻辑上的关系,主要指邻接关系逻辑关系在考虑数据间关系的时候会涉及直接前趋和直接后继的概念.

1.2.1.2数据的逻辑结构:

        线性结构:一对一的关系,除了端点之外的每一个数据都有一个前驱和一个后继

        注意:线性结构要求数据是连续的

        

        非线性结构:              

                树形结构:

                一对多的关系,每个节点都有一个前驱和多个后继     

                图形结构:

                多对多的关系 

                集合结构:

                数据元素间除“同属于一个集合”外,无其它关系,一般编程时不考虑。

1.2.2存储结构:

1.2.2.1概念:        

        指数据在计算机内存中的分布方式.存储结构:逻辑结构在计算机中的具体实现方法,也就是说是数据在内存上实际的存储方式。存储结构是通过计算机语言所编制的程序来实现的,因而是依赖于具体的计算机语言的。

1.2.2.2 顺序存储:

将数据结构中各元素按照其逻辑顺序存放于存储器一片连续的存储空间中,如c语言的数组。

5.2.2 链式存储:

将数据结构中各元素分布到存储器的不同点,用指针方式建立它们之间的联系。

5.2.3 索引存储:

根据数据元素的特殊字段(称为关键字key),计算数据元素的存放地址,然后数据元素按地址存放

如:使用百家姓作为关键字来存储市民信息,访问时效率会比遍历所有市民高很多。

      

1.3 算法相关的概念

1.3.1 算法的定义

所谓的算法,就是解决问题的一种方法。在有限步骤内解决数学问题的过程

注意:计算机中处理的数据都是有限的,那些无限的概念只出现在数学模型中。

算法是不依赖于编程语言的

1.3.2 算法的特点

1.有穷性 算法是由若干指令组成的有穷序列,总是在执行若干次后结束

2.确定性 每条指令都有特定的含义 且 没有歧义,同样的输入要有同样的输出

3.可行性 算法可以在当前环境下通过有限次运行实现

4.输入输出 由 0 个输入或多个输入 有一个或多个输出

1.3.3 好的算法标准:

1.正确性 运行正常、结果正确

2.易读性 简单、易读 遵循公司命名规则、适当的注释

3.健壮性 对非法的数据和操作有良好的反应能力和处理能力

4.高效性 执行效率高、运行时间短

5.低存储 算法所占空间尽可能低

1.3.4算法的复杂度

1.3.4.1 概念

        算法的复杂度分为时间复杂度和空间复杂度空间复杂度:指的就是算法在实现的过程中需要用到的额外的临时内存的大小现在技术比较成熟了,内存空间一般都是够用的,所以空间复杂度一般不考虑时间复杂度:指的是算法实现的过程中,需要耗时的多少。如果只说算法的复杂度,一般情况下,指的都是时间复杂度。程序由三种结构构成:顺序结构、分支结构和循环结构。顺序结构和分支结构中的每段代码只运行一次;循环结构中的代码的运行时间要看循环的次数。由于是估算算法的时间复杂度,相比而言,循环结构对算法的执行时间影响更大。所以,算法的时间复杂度,主要看算法中使用到的循环结构中代码循环的次数(称为“频度”)。次数越少,算法的时间复杂度越低。

1.3.4.2 时间复杂度的表示

算法的时间复杂度定义为算法中可执行语句的频度之和 记作 T(n)

语句频度是指同一代码执行的次数

T(n)是算法所需时间的一种估值,n 表示问题的规模

算法的时间复杂度的表示方式为:

O(频度); 称为 大 O 表示法

使用大O表示法的简化方式:

1.去掉运行时间中的所有常数项。

(例如 2+2n+2n^2,直接变为 2n^2+2n)

2.保留最高次幂项。

(2n^2+2n 变成 2n^2)

3.最高次幂项存在但是系数不是1,把系数改成1。

2n^2 变成 n^2

所有,上述例子中算法的时间复杂度为 O(n^2)

1.3.4.3常见的时间复杂度

时间复杂度是 O(n):

  1. int i = 1;
  2. int sum = 0;
  3. for(i = 1; i <= n; i++){
  4. sum+=i;
  5. }

时间复杂度 O(1):

  1. int sum = 0;
  2. sum = (1+n)*n/2;

时间复杂度 O(n^2):

  1. for(i = 0; i < n; i++){
  2. for(j = 0; j < n; j++){
  3. //时间复杂度为 O(1) 的语句
  4. }
  5. }

时间复杂度 O(n^3):

  1. for(i = 0; i < n; i++){
  2. for(j = 0; j < n; j++){
  3. for(k = 0; k < n; k++){
  4. //时间复杂度为 O(1) 的语句
  5. }
  6. }
  7. }

时间复杂度 O(logn):

  1. int i = 1;
  2. while(i<n){
  3. i *= 2;
  4. }

时间复杂度优劣的排序:

O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^n)

1.4顺序表

1.4.1 概念:

逻辑结构:线性结构

存储结构:顺序存储(顺序表)--基础数组实现

缺点:

空间使用不够灵活,需要提前确定数组的大小

插入和删除元素比较麻烦,因为需要批量的移动元素

优点:

按照位置查找元素比较快,因为可以通过数组下标来访问元素

1.4.2 操作:

创建线性表 

插入数据元素(尾插、任意位置插入法) 

删除数据元素(尾删法、任意位置删除法) 

修改数据元素(根据位置、值修改) 

在顺序表中查找元素(按位置查找) 

顺序表的排序 --排序

清空顺序表 

释放顺序表 

打印顺序表中的成员----实际用不到,我们写他是用来学习阶段看现象的 

1.4.3代码实现

头文件

  1. #ifndef __SEQ_LIST_H__
  2. #define __SEQ_LIST_H__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #define N 5
  6. typedef struct _Student{
  7. int data;
  8. }stu_t;
  9. typedef struct _Seq_list{
  10. stu_t s[N];
  11. int count;
  12. }seq_list_t;
  13. seq_list_t *create_seq_list1();
  14. int create_seq_list2(seq_list_t **);
  15. int insert_seq_list_by_tail(seq_list_t *my_class, int in_data);
  16. int print_seq_list(seq_list_t *my_class);
  17. int insert_seq_list_by_pos(seq_list_t *my_class, int in_data, int pos);
  18. int delete_from_deq_list_by_pos(seq_list_t *my_class, int pos);
  19. int clean_seq_list(seq_list_t *my_class);
  20. int destroy_seq_list(seq_list_t **my_class);
  21. int search_data_from_seq_list(seq_list_t *my_class, int pos, int *out_data);
  22. int update_seq_list_by_pos(seq_list_t *my_class, int pos, int new_value);
  23. int sort_seq_list(seq_list_t *my_class, int flag);
  24. #endif

函数

  1. #include "seq_list.h"
  2. //创建顺序表的方式1 使用指针函数创建
  3. seq_list_t *create_seq_list1(){
  4. seq_list_t *temp = (seq_list_t *)malloc(sizeof(seq_list_t));
  5. if(NULL == temp){
  6. printf("内存分配失败\n");
  7. exit(-1);//结束进程
  8. }
  9. temp->count = 0;
  10. return temp;
  11. }
  12. //创建顺序表的方式2 使用地址传参方式创建
  13. int create_seq_list2(seq_list_t **p){
  14. if(NULL == p){
  15. printf("入参为 NULL, 请检查\n");
  16. return -1;
  17. }
  18. *p = (seq_list_t *)malloc(sizeof(seq_list_t));
  19. if(NULL == *p){
  20. printf("内存分配失败\n");
  21. return -1;
  22. }
  23. (*p)->count = 0;
  24. return 0;
  25. }
  26. //向顺序表中插入数据的函数--尾插法
  27. int insert_seq_list_by_tail(seq_list_t *my_class, int in_data){
  28. //入参合理性检查
  29. if(NULL == my_class){
  30. printf("入参为 NULL, 请检查\n");
  31. return -1;
  32. }
  33. if(my_class->count == N){
  34. printf("表已满,插入失败\n");
  35. return -1;
  36. }
  37. my_class->s[my_class->count].data = in_data;
  38. my_class->count++;
  39. return 0;
  40. }
  41. //打印顺序表中的元素
  42. int print_seq_list(seq_list_t *my_class){
  43. if(NULL == my_class){
  44. printf("入参为 NULL, 请检查\n");
  45. return -1;
  46. }
  47. int i = 0;
  48. for(i = 0; i < my_class->count; i++){
  49. printf("%d ", my_class->s[i].data);
  50. }
  51. printf("\n");
  52. return 0;
  53. }
  54. //向顺序表中插入数据的函数--任意位置插入
  55. int insert_seq_list_by_pos(seq_list_t *my_class, int in_data, int pos){
  56. if(NULL == my_class){
  57. printf("入参为 NULL, 请检查\n");
  58. return -1;
  59. }
  60. if(my_class->count == N){
  61. printf("表已满,插入失败\n");
  62. return -1;
  63. }
  64. //对插入位置的合理性做检查
  65. if(pos<0 || pos>my_class->count){
  66. printf("插入位置不合理,请检查\n");
  67. return -1;
  68. }
  69. //开始插入
  70. //先将待插入位置后面的数据依次向后移动一步
  71. int i = 0;
  72. for(i = my_class->count-1; i >= pos; i--){
  73. my_class->s[i+1] = my_class->s[i];
  74. }
  75. //将数据插入待插入位置
  76. my_class->s[pos].data = in_data;
  77. my_class->count++;
  78. return 0;
  79. }
  80. int delete_from_deq_list_by_pos(seq_list_t *my_class, int pos){
  81. if(NULL == my_class){
  82. printf("入参为 NULL, 请检查\n");
  83. return -1;
  84. }
  85. if(my_class->count == 0){
  86. printf("表已空,删除失败\n");
  87. return -1;
  88. }
  89. if(pos<0 || pos>=my_class->count){
  90. printf("删除位置不合理,请检查\n");
  91. return -1;
  92. }
  93. //执行删除操作
  94. //从待删除位置开始,后面的元素依次向前移动一步
  95. int i = 0;
  96. for(i = pos; i < my_class->count-1; i++){
  97. my_class->s[i] = my_class->s[i+1];
  98. }
  99. my_class->count--;
  100. return 0;
  101. }
  102. //清空顺序表
  103. int clean_seq_list(seq_list_t *my_class){
  104. if(NULL == my_class){
  105. printf("入参为 NULL, 请检查\n");
  106. return -1;
  107. }
  108. my_class->count = 0;
  109. return 0;
  110. }
  111. //释放顺序表
  112. int destroy_seq_list(seq_list_t **my_class){
  113. if(NULL == my_class || NULL == *my_class){
  114. printf("入参为 NULL, 请检查\n");
  115. return -1;
  116. }
  117. free(*my_class);
  118. *my_class = NULL;//之所以传二级指针 是为了这一步操作
  119. return 0;
  120. }
  121. //查找顺序表中指定位置的数据
  122. int search_data_from_seq_list(seq_list_t *my_class, int pos, int *out_data){
  123. if(NULL == my_class || NULL == out_data){
  124. printf("入参为 NULL, 请检查\n");
  125. return -1;
  126. }
  127. if(pos<0 || pos>=my_class->count){
  128. printf("查找位置不合理,请检查\n");
  129. return -1;
  130. }
  131. *out_data = my_class->s[pos].data;
  132. return 0;
  133. }
  134. //按照位置修改元素
  135. int update_seq_list_by_pos(seq_list_t *my_class, int pos, int new_value){
  136. if(NULL == my_class){
  137. printf("入参为 NULL, 请检查\n");
  138. return -1;
  139. }
  140. if(pos<0 || pos>=my_class->count){
  141. printf("修改位置不合理,请检查\n");
  142. return -1;
  143. }
  144. my_class->s[pos].data = new_value;
  145. return 0;
  146. }
  147. //顺序表排序
  148. // flag 0 升序 1 降序
  149. int sort_seq_list(seq_list_t *my_class, int flag){
  150. if(NULL == my_class){
  151. printf("入参为 NULL, 请检查\n");
  152. return -1;
  153. }
  154. int i = 0;
  155. int j = 0;
  156. stu_t temp;
  157. if(0 == flag){//升序
  158. for(i = 0; i < my_class->count-1; i++){
  159. for(j = 0; j < my_class->count-1-i; j++){
  160. if(my_class->s[j].data > my_class->s[j+1].data){
  161. temp = my_class->s[j];
  162. my_class->s[j] = my_class->s[j+1];
  163. my_class->s[j+1] = temp;
  164. }
  165. }
  166. }
  167. }else if(1 == flag){//降序
  168. for(i = 0; i < my_class->count-1; i++){
  169. for(j = 0; j < my_class->count-1-i; j++){
  170. if(my_class->s[j].data < my_class->s[j+1].data){
  171. temp = my_class->s[j];
  172. my_class->s[j] = my_class->s[j+1];
  173. my_class->s[j+1] = temp;
  174. }
  175. }
  176. }
  177. }
  178. return 0;
  179. }


main.c

  1. #include "seq_list.h"
  2. int main(){
  3. seq_list_t *my_class = NULL;
  4. //my_class = create_seq_list1();
  5. //创建顺序表
  6. create_seq_list2(&my_class);
  7. //向顺序表中插入数据
  8. insert_seq_list_by_tail(my_class, 10);
  9. insert_seq_list_by_tail(my_class, 20);
  10. insert_seq_list_by_tail(my_class, 30);
  11. //打印顺序表中的元素
  12. print_seq_list(my_class);
  13. int num = 0;
  14. search_data_from_seq_list(my_class, 1, &num);
  15. printf("num = %d\n", num);
  16. //将下标为1的元素的值修改成 520
  17. update_seq_list_by_pos(my_class, 1, 520);
  18. //打印顺序表中的元素
  19. print_seq_list(my_class);
  20. //升序排序
  21. sort_seq_list(my_class, 0);
  22. //打印顺序表中的元素
  23. print_seq_list(my_class);
  24. //升序排序
  25. sort_seq_list(my_class, 1);
  26. //打印顺序表中的元素
  27. print_seq_list(my_class);
  28. delete_from_deq_list_by_pos(my_class, 1);
  29. //打印顺序表中的元素
  30. print_seq_list(my_class);
  31. //清空顺序表
  32. clean_seq_list(my_class);
  33. //打印顺序表中的元素
  34. print_seq_list(my_class);
  35. //释放顺序表
  36. destroy_seq_list(&my_class);
  37. printf("%p\n", my_class);
  38. return 0;
  39. }

1.5链表

1.5.1图示:

1.5.2链表相关的操作:

1.创建链表 

2.清空链表 

3.销毁链表 

4.头插法 

5.尾插法 

6.任意位置插入法 

7.头删法 

8.尾删法 

9.任意位置删除法 

10.查询链表中是否有想要的数据(在链表中获取指定位置的数据) 

11.按照位置修改链表中的数据 

12.按照值修改链表中的数据 

13.两个链表合并 

14.链表的排序

15.链表的翻转

16.遍历链表的节点----学习阶段看现象用的 

1.5.3操作示意图:

1.5.3.1 链表插入数据示意图

 1.5.3.2链表删除数据示意图

 1.5.3.3链表翻转示意图

  1.5.3.4链表的排序示意图

1.5.3.5 代码实现

头文件

  1. #ifndef __LINK_LIST_H__
  2. #define __LINK_LIST_H__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. typedef struct __Node{
  7. int data;//数据域 课上以int为例 实际开发过程中可以自己封装结构体
  8. struct __Node *next;//指针域 注意此时还不认是 node_t 不能使用
  9. }node_t;
  10. int create_node(node_t **p, int in_data);
  11. int insert_data_by_head(node_t *phead, int in_data);
  12. int print_link_list(node_t *phead);
  13. int insert_data_by_tail(node_t *phead, int in_data);
  14. int insert_data_by_pos(node_t *phead, int in_data, int pos);
  15. int del_from_list_by_head(node_t *phead);
  16. int del_from_list_by_tail(node_t *phead);
  17. int del_from_list_by_pos(node_t *phead, int pos);
  18. int get_data_from_list(node_t *phead, int pos, int *num);
  19. int update_list_by_pos(node_t *phead, int pos, int new_data);
  20. int update_list_by_value(node_t *phead, int old_value, int new_value);
  21. int clean_list(node_t *phead);
  22. int destroy_list(node_t **p);
  23. int merge_two_list(node_t *phead1, node_t **phead2);
  24. int overturn_list(node_t *phead);
  25. int sort_list(node_t *phead);
  26. #endif

函数:

  1. #include "link_list.h"
  2. //创建节点的函数
  3. int create_node(node_t **p, int in_data){
  4. if(NULL == p){
  5. printf("入参为NULL, 请检查\n");
  6. return -1;
  7. }
  8. *p = (node_t *)malloc(sizeof(node_t));
  9. if(NULL == *p){
  10. printf("内存分配失败\n");
  11. exit(-1);
  12. }
  13. (*p)->data = in_data;
  14. (*p)->next = NULL;
  15. return 0;
  16. }
  17. //头插法
  18. int insert_data_by_head(node_t *phead, int in_data){
  19. if(NULL == phead){
  20. printf("入参为NULL, 请检查\n");
  21. return -1;
  22. }
  23. //创建新节点
  24. node_t *pnew = NULL;
  25. create_node(&pnew, in_data);
  26. //执行头插操作
  27. pnew->next = phead->next;
  28. phead->next = pnew;
  29. return 0;
  30. }
  31. //遍历链表
  32. int print_link_list(node_t *phead){
  33. if(NULL == phead){
  34. printf("入参为NULL, 请检查\n");
  35. return -1;
  36. }
  37. node_t *ptemp = phead;
  38. while(NULL != ptemp->next){
  39. ptemp = ptemp->next;
  40. printf("%d ", ptemp->data);
  41. }
  42. printf("\n");
  43. return 0;
  44. }
  45. //尾插法插入数据
  46. int insert_data_by_tail(node_t *phead, int in_data){
  47. if(NULL == phead){
  48. printf("入参为NULL, 请检查\n");
  49. return -1;
  50. }
  51. //创建新节点
  52. node_t *pnew = NULL;
  53. create_node(&pnew, in_data);
  54. //执行尾插操作
  55. node_t *ptemp = phead;
  56. //遍历链表找到链表的尾节点
  57. while(ptemp->next != NULL){
  58. ptemp = ptemp->next;
  59. }
  60. //让尾结点的指针域指向新节点
  61. ptemp->next = pnew;
  62. return 0;
  63. }
  64. //任意位置插入法
  65. int insert_data_by_pos(node_t *phead, int in_data, int pos){
  66. if(NULL == phead){
  67. printf("入参为NULL, 请检查\n");
  68. return -1;
  69. }
  70. if(pos<=0){
  71. printf("插入位置不合理,请检查\n");
  72. return -1;
  73. }
  74. //找到待插入位置的前一节点
  75. node_t *ptemp = phead;
  76. int i = 0;
  77. for(i = 0; i < pos-1; i++){
  78. ptemp = ptemp->next;
  79. if(NULL == ptemp){
  80. break;
  81. }
  82. }
  83. //如果是由于break导致的循环结束 说明位置不合理
  84. if(i < pos-1){
  85. printf("插入位置不合理,请检查\n");
  86. return -1;
  87. }
  88. //位置合理 创建新节点
  89. node_t *pnew = NULL;
  90. create_node(&pnew, in_data);
  91. //将新节点插入
  92. pnew->next = ptemp->next;
  93. ptemp->next = pnew;
  94. return 0;
  95. }
  96. //头删法
  97. int del_from_list_by_head(node_t *phead){
  98. if(NULL == phead){
  99. printf("入参为NULL, 请检查\n");
  100. return -1;
  101. }
  102. //判断是否有数据用来删除
  103. if(NULL == phead->next){
  104. printf("链表中没有数据 头删失败\n");
  105. return -1;
  106. }
  107. //定义一个指针 pdel 保存 第一个数据节点的地址
  108. node_t *pdel = phead->next;
  109. phead->next = phead->next->next;
  110. free(pdel);
  111. pdel = NULL;
  112. return 0;
  113. }
  114. //尾删法
  115. int del_from_list_by_tail(node_t *phead){
  116. if(NULL == phead){
  117. printf("入参为NULL, 请检查\n");
  118. return -1;
  119. }
  120. //判断是否有数据用来删除
  121. if(NULL == phead->next){
  122. printf("链表中没有数据 尾删失败\n");
  123. return -1;
  124. }
  125. //找到最后一个节点的前一节点
  126. node_t *ptemp = phead;
  127. while(NULL != ptemp->next->next){
  128. ptemp = ptemp->next;
  129. }
  130. free(ptemp->next);
  131. ptemp->next = NULL;
  132. return 0;
  133. }
  134. //任意位置删除法
  135. int del_from_list_by_pos(node_t *phead, int pos){
  136. if(NULL == phead){
  137. printf("入参为NULL, 请检查\n");
  138. return -1;
  139. }
  140. if(pos <= 0 ){
  141. printf("删除位置不合理 请检查\n");
  142. return -1;
  143. }
  144. node_t *ptemp = phead;
  145. int i = 0;
  146. //找到待删除位置的前一节点
  147. for(i = 0; i < pos-1; i++){
  148. ptemp = ptemp->next;
  149. if(ptemp->next == NULL){
  150. break;
  151. }
  152. }
  153. if(i < pos-1){
  154. printf("删除位置不合理 请检查\n");
  155. return -1;
  156. }
  157. //执行删除操作
  158. node_t *pdel = ptemp->next;
  159. ptemp->next = pdel->next;
  160. free(pdel);
  161. pdel = NULL;
  162. return 0;
  163. }
  164. //获取指定位置的数据
  165. int get_data_from_list(node_t *phead, int pos, int *num){
  166. if(NULL == phead){
  167. printf("入参为NULL, 请检查\n");
  168. return -1;
  169. }
  170. if(pos <= 0 ){
  171. printf("获取数据的位置不合理 请检查\n");
  172. return -1;
  173. }
  174. node_t *ptemp = phead;
  175. int i = 0;
  176. //找到要获取数据的节点 并做位置合理性的检查
  177. for(i = 0; i < pos; i++){
  178. ptemp = ptemp->next;
  179. if(NULL == ptemp){
  180. printf("获取数据的位置不合理,请检查\n");
  181. return -1;
  182. }
  183. }
  184. //如果运行到这儿了 说明位置合理
  185. *num = ptemp->data;
  186. return 0;
  187. }
  188. //按照位置修改链表中的数据
  189. int update_list_by_pos(node_t *phead, int pos, int new_data){
  190. if(NULL == phead){
  191. printf("入参为NULL, 请检查\n");
  192. return -1;
  193. }
  194. if(pos <= 0 ){
  195. printf("修改数据的位置不合理 请检查\n");
  196. return -1;
  197. }
  198. node_t *ptemp = phead;
  199. int i = 0;
  200. //找到要修改数据的节点 并做位置合理性的检查
  201. for(i = 0; i < pos; i++){
  202. ptemp = ptemp->next;
  203. if(NULL == ptemp){
  204. printf("修改数据的位置不合理,请检查\n");
  205. return -1;
  206. }
  207. }
  208. //如果运行到这儿了 说明位置合理
  209. ptemp->data = new_data;
  210. return 0;
  211. }
  212. //根据值修改链表中的数据
  213. int update_list_by_value(node_t *phead, int old_value, int new_value){
  214. if(NULL == phead){
  215. printf("入参为NULL, 请检查\n");
  216. return -1;
  217. }
  218. //遍历链表
  219. node_t *ptemp = phead;
  220. while(ptemp->next != NULL){
  221. ptemp = ptemp->next;
  222. if(ptemp->data == old_value){
  223. ptemp->data = new_value;
  224. }
  225. }
  226. return 0;
  227. }
  228. //清空链表 --释放所有数据节点 保留头结点
  229. int clean_list(node_t *phead){
  230. if(NULL == phead){
  231. printf("入参为NULL, 请检查\n");
  232. return -1;
  233. }
  234. //循环头删所有数据节点
  235. node_t *pdel = NULL;
  236. while(phead->next != NULL){
  237. pdel = phead->next;
  238. phead->next = pdel->next;
  239. free(pdel);
  240. }
  241. pdel = NULL;
  242. return 0;
  243. }
  244. //销毁链表
  245. int destroy_list(node_t **p){
  246. if(NULL == p || NULL == *p){
  247. printf("入参为NULL, 请检查\n");
  248. return -1;
  249. }
  250. //防止先调用销毁没调用清空导致的内存泄漏
  251. //可以在释放头结点之前 先调用一下清空的函数
  252. clean_list(*p);
  253. free(*p);
  254. *p = NULL;
  255. return 0;
  256. }
  257. //两个链表合并
  258. int merge_two_list(node_t *phead1, node_t **phead2){
  259. if(NULL == phead1 || NULL == phead2 || NULL == *phead2){
  260. printf("入参为NULL, 请检查\n");
  261. return -1;
  262. }
  263. //先找到第一个链表的最后一个节点
  264. node_t *ptemp = phead1;
  265. while(ptemp->next != NULL){
  266. ptemp = ptemp->next;
  267. }
  268. //让第一个链表的最后一个节点的指针域指向第二个链表的第一个数据节点
  269. ptemp->next = (*phead2)->next;
  270. //释放第二个链表的 头结点
  271. free(*phead2);
  272. *phead2 = NULL;
  273. return 0;
  274. }
  275. //链表的翻转
  276. int overturn_list(node_t *phead){
  277. if(NULL == phead){
  278. printf("入参为NULL, 请检查\n");
  279. return -1;
  280. }
  281. if(phead->next == NULL){
  282. printf("链表为空 无需翻转\n");
  283. return -1;
  284. }
  285. if(phead->next->next == NULL){
  286. printf("链表只有一个数据节点 无需翻转\n");
  287. return -1;
  288. }
  289. node_t *p = phead->next;
  290. node_t *q = p->next;
  291. p->next = NULL;
  292. while(NULL != q){
  293. p = q->next;
  294. q->next = phead->next;
  295. phead->next = q;
  296. q = p;
  297. }
  298. printf("翻转完成\n");
  299. return 0;
  300. }
  301. //链表的排序 --以升序为例
  302. int sort_list(node_t *phead){
  303. if(NULL == phead){
  304. printf("入参为NULL, 请检查\n");
  305. return -1;
  306. }
  307. if(phead->next == NULL){
  308. printf("链表为空 无需排序\n");
  309. return -1;
  310. }
  311. if(phead->next->next == NULL){
  312. printf("链表只有一个数据节点 无需排序\n");
  313. return -1;
  314. }
  315. int temp = 0;
  316. node_t *p = phead->next;
  317. node_t *q = p->next;
  318. while(NULL != p->next){
  319. while(NULL != q){
  320. if(q->data < p->data){
  321. //交换数据域
  322. temp = p->data;
  323. p->data = q->data;
  324. q->data = temp;
  325. }
  326. q = q->next;
  327. }
  328. p = p->next;
  329. q = p->next;
  330. }
  331. printf("升序排序完成\n");
  332. return 0;
  333. }

main.c

  1. #include "link_list.h"
  2. int main(){
  3. node_t *phead = NULL;
  4. //创建头结点
  5. create_node(&phead, -1);
  6. //头插法插入数据
  7. insert_data_by_head(phead, 10);
  8. insert_data_by_head(phead, 20);
  9. insert_data_by_head(phead, 30);
  10. insert_data_by_head(phead, 40);
  11. insert_data_by_head(phead, 50);
  12. //尾插法插入数据
  13. insert_data_by_tail(phead, 520);
  14. insert_data_by_tail(phead, 1314);
  15. //任意位置插入
  16. insert_data_by_pos(phead, 111, 8);
  17. //遍历链表
  18. print_link_list(phead);
  19. //头删法删除数据
  20. del_from_list_by_head(phead);
  21. //遍历链表
  22. print_link_list(phead);
  23. //尾删法删除数据
  24. del_from_list_by_tail(phead);
  25. //遍历链表
  26. print_link_list(phead);
  27. //任意位置删除数据
  28. del_from_list_by_pos(phead,3);
  29. //遍历链表
  30. print_link_list(phead);
  31. int num = 0;
  32. get_data_from_list(phead, 6, &num);
  33. printf("num = %d\n", num);
  34. //根据位置修改数据
  35. update_list_by_pos(phead, 4, 40);
  36. //遍历链表
  37. print_link_list(phead);
  38. //根据值修改数据
  39. update_list_by_value(phead, 40, 520);
  40. //遍历链表
  41. print_link_list(phead);
  42. printf("-------------------------\n");
  43. node_t *phead2 = NULL;
  44. create_node(&phead2, -1);
  45. insert_data_by_head(phead2, 111);
  46. insert_data_by_head(phead2, 222);
  47. insert_data_by_head(phead2, 333);
  48. merge_two_list(phead, &phead2);
  49. //遍历链表
  50. print_link_list(phead);
  51. printf("phead2 = %p\n", phead2);
  52. printf("-------------------------\n");
  53. //链表翻转
  54. overturn_list(phead);
  55. //遍历链表
  56. print_link_list(phead);
  57. //链表升序排序
  58. sort_list(phead);
  59. //遍历链表
  60. print_link_list(phead);
  61. //清空链表
  62. clean_list(phead);
  63. //遍历链表
  64. print_link_list(phead);
  65. //销毁链表
  66. destroy_list(&phead);
  67. printf("phead = %p\n", phead);
  68. return 0;
  69. }

1.6循环链表(基本上和单链表一样就不做各种操作,用一个练习代替了)

1.6.1图解

 

 1.6.2练习

循环链表练习:

一位叫约瑟夫的将军,战斗中,连同手下士兵一起被敌人俘虏了,

手下的士兵宁死不投降,所以,约瑟夫将军想出了一个办法,

让大家站成一个圈,从第一个人开始数数,谁数到7了,谁就自杀,

自杀之后,他的下一个人重新从1开始数,遇到7,再自杀,直到最后只能剩下一个人。

最后活下来的是约瑟夫,然后他不想死,他投降了。

这种圈,在现今社会中被叫做,"约瑟夫环"。

要求,编写程序,命令行 输入 总人数(>=2) 及 数到几(>=2)自杀,

程序自动计算,将第 n 次 杀掉的 第m个人输出

最后再输出剩下的是几号

例如:

输入 5人 数到3 自杀,

程序输出:

第 1 次杀的人是 3 号;

第 2 次杀的人是 1 号;

第 3 次杀的人是 5 号;

第 4 次杀的人是 2 号;

最后剩下的是 4 号

1.6.2.1代码实现:

头文件:

  1. #ifndef _MYHEAD_H__
  2. #define __MYHEAD_H__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. typedef struct _XUN_HU{
  7. int data;
  8. struct _XUN_HU *next;
  9. }xun_hun_t;
  10. int create_xun_hun(xun_hun_t **phead);
  11. int insert_xun_hun(xun_hun_t *phead,int in_data);
  12. int printf_xun_hun(xun_hun_t *phead);
  13. int yue_se_fu_xun_hun(xun_hun_t *phead,int n,int m);
  14. #endif

函数:

  1. #include "myhead.h"
  2. int create_xun_hun(xun_hun_t **phead)
  3. {
  4. if(phead==NULL){
  5. printf("1地址传参错误,请检查\n");
  6. return -1;
  7. }
  8. *phead=(xun_hun_t *)malloc(sizeof(xun_hun_t));
  9. (*phead)->next=*phead;
  10. (*phead)->data=1;
  11. return 0;
  12. }
  13. int insert_xun_hun(xun_hun_t *phead,int in_data)
  14. {
  15. if(phead==NULL){
  16. printf("2地址传参错误,请检查\n");
  17. return -1;
  18. }
  19. xun_hun_t *temp=(xun_hun_t *)malloc(sizeof(xun_hun_t));
  20. temp->next=NULL;
  21. temp->next=phead->next;
  22. phead->next=temp;
  23. temp->data=in_data;
  24. return 0;
  25. }
  26. int printf_xun_hun(xun_hun_t *phead)
  27. {
  28. if(phead==NULL){
  29. printf("3地址传参错误,请检查\n");
  30. return -1;
  31. }
  32. xun_hun_t *flags=phead;
  33. while(phead->next!=flags){
  34. printf("%d ",phead->data);
  35. phead=phead->next;
  36. }
  37. printf("%d\n",phead->data);
  38. return 0;
  39. }
  40. //n表示从几开始数,m表示数多少个数
  41. int yue_se_fu_xun_hun(xun_hun_t *phead,int n,int m)
  42. {
  43. if(phead==NULL){
  44. printf("3地址传参错误,请检查\n");
  45. return -1;
  46. }
  47. xun_hun_t *flags=phead;
  48. int i=0;
  49. while(phead->next!=phead&&i<n-1){
  50. phead=phead->next;
  51. i++;
  52. }
  53. //printf("%d\n",phead->data);
  54. i=0;
  55. while(phead!=phead->next){
  56. phead=phead->next;
  57. i++;
  58. if(i==m-2){
  59. xun_hun_t *temp=phead->next;
  60. phead->next=phead->next->next;
  61. printf("%d ",temp->data);
  62. free(temp);
  63. temp=NULL;
  64. i=0;
  65. phead=phead->next;
  66. }
  67. }
  68. printf("%d\n",phead->data);
  69. return 0;
  70. }

main.c:

  1. #include "myhead.h"
  2. int main(int argc, char const *argv[])
  3. {
  4. xun_hun_t *phead=NULL;
  5. create_xun_hun(&phead);
  6. insert_xun_hun(phead,8);
  7. insert_xun_hun(phead,7);
  8. insert_xun_hun(phead,6);
  9. insert_xun_hun(phead,5);
  10. insert_xun_hun(phead,4);
  11. insert_xun_hun(phead,3);
  12. insert_xun_hun(phead,2);
  13. printf_xun_hun(phead);
  14. yue_se_fu_xun_hun(phead,3,3);
  15. printf_xun_hun(phead);
  16. return 0;
  17. }

1.7双向循环链表(由于步骤和前面差不多,就只讲解一下思路)

1.7.1 双向链表示意图

 1.7.2双向链表插入节点流程图

 1.7.3双向链表删除节点流程图

 1.8栈

1.8.1 概念

栈是一种 先进后出 的数据结构 FILO(first in last out)

1.8.2栈的存储结构

顺序存储的栈,我们称之为顺序栈,

是基于数组实现的,一般会配合一个 "栈顶指针" top 来使用

顺序栈本身相当于对顺序表操作的一个约束:只允许在同一端插入和删除元素。

顺序栈的操作:

创建栈

清空栈

销毁栈

向栈中插入元素:入栈/压栈(push)

判断栈是否为满

获取栈中的元素:出栈/弹栈(pop)

判断栈是否为空

打印栈中所有元素--学习阶段看现象用的

1.8.3代码实现

头文件

  1. #ifndef __SEQ_STACK_H__
  2. #define __SEQ_STACK_H__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define N 3
  7. typedef struct _STACK{
  8. int s[N];//栈的数组 课上以int为例
  9. int top;//栈顶指针 作为数组下标使用
  10. }stack_t;
  11. int create_stack(stack_t **p);
  12. int print_stack(stack_t *my_stack);
  13. int is_full(stack_t *my_stack);
  14. int push_stack(stack_t *my_stack, int value);
  15. int pop_stack(stack_t *my_stack, int *num);
  16. int is_empty(stack_t *my_stack);
  17. int clean_stack(stack_t *my_stack);
  18. int destroy_stack(stack_t **my_stack);
  19. #endif

函数:

  1. #include "seq_stack.h"
  2. //创建栈
  3. int create_stack(stack_t **p){
  4. if(NULL == p){
  5. printf("入参为NULL 请检查\n");
  6. return -1;
  7. }
  8. *p = (stack_t *)malloc(sizeof(stack_t));
  9. if(NULL == *p){
  10. printf("内存分配失败\n");
  11. exit(-1);
  12. }
  13. (*p)->top = 0;
  14. return 0;
  15. }
  16. //入栈
  17. int push_stack(stack_t *my_stack, int value){
  18. if(NULL == my_stack){
  19. printf("入参为NULL 请检查\n");
  20. return -1;
  21. }
  22. if(is_full(my_stack)){
  23. printf("栈已满 入栈失败\n");
  24. return -1;
  25. }
  26. my_stack->s[my_stack->top] = value;
  27. my_stack->top++;
  28. return 0;
  29. }
  30. //判断栈是否为满 返回 1 满 返回 0 未满
  31. int is_full(stack_t *my_stack){
  32. if(NULL == my_stack){
  33. printf("入参为NULL 请检查\n");
  34. return -1;
  35. }
  36. return my_stack->top==N?1:0;
  37. }
  38. //出栈
  39. int pop_stack(stack_t *my_stack, int *num){
  40. if(NULL == my_stack){
  41. printf("入参为NULL 请检查\n");
  42. return -1;
  43. }
  44. if(is_empty(my_stack)){
  45. printf("栈已空 出栈失败\n");
  46. return -1;
  47. }
  48. my_stack->top--;
  49. *num = my_stack->s[my_stack->top];
  50. return 0;
  51. }
  52. //判断栈是否为空 返回 1 空 返回 0 非空
  53. int is_empty(stack_t *my_stack){
  54. if(NULL == my_stack){
  55. printf("入参为NULL 请检查\n");
  56. return -1;
  57. }
  58. return my_stack->top==0?1:0;
  59. }
  60. //打印栈中所有元素
  61. int print_stack(stack_t *my_stack){
  62. if(NULL == my_stack){
  63. printf("入参为NULL 请检查\n");
  64. return -1;
  65. }
  66. int i = 0;
  67. for(i = 0; i < my_stack->top; i++){
  68. printf("%d ", my_stack->s[i]);
  69. }
  70. printf("\n");
  71. return 0;
  72. }
  73. //清空栈 ---top置0 即可
  74. int clean_stack(stack_t *my_stack){
  75. if(NULL == my_stack){
  76. printf("入参为NULL 请检查\n");
  77. return -1;
  78. }
  79. my_stack->top = 0;
  80. return 0;
  81. }
  82. //销毁栈
  83. int destroy_stack(stack_t **my_stack){
  84. if(NULL == my_stack || NULL == *my_stack){
  85. printf("入参为NULL 请检查\n");
  86. return -1;
  87. }
  88. free(*my_stack);
  89. *my_stack = NULL;
  90. return 0;
  91. }

main.c:

  1. #include "seq_stack.h"
  2. int main(){
  3. stack_t *my_stack = NULL;
  4. //创建栈
  5. create_stack(&my_stack);
  6. //元素入栈
  7. push_stack(my_stack, 10);
  8. push_stack(my_stack, 20);
  9. push_stack(my_stack, 30);
  10. push_stack(my_stack, 40);//这次会失败 因为栈已经满了
  11. //遍历栈
  12. print_stack(my_stack);
  13. int num = 0;
  14. pop_stack(my_stack, &num);
  15. printf("num = %d\n", num);
  16. pop_stack(my_stack, &num);
  17. printf("num = %d\n", num);
  18. pop_stack(my_stack, &num);
  19. printf("num = %d\n", num);
  20. pop_stack(my_stack, &num);//会失败 因为栈已经空了
  21. printf("num = %d\n", num);
  22. //在入栈两个元素
  23. push_stack(my_stack, 10);
  24. push_stack(my_stack, 20);
  25. //遍历栈
  26. print_stack(my_stack);
  27. //清空栈
  28. clean_stack(my_stack);
  29. //遍历栈
  30. print_stack(my_stack);
  31. //销毁栈
  32. destroy_stack(&my_stack);
  33. printf("my_stack = %p\n", my_stack);
  34. return 0;
  35. }

1.8.3链式栈

1.8.4.1概念

链式存储的栈,我们称之为链式栈,

是基于链表实现的

链式栈本身相当于对链表表操作的一个约束:只允许在同一端插入和删除元素。

如果采用链表尾作为入栈和出栈的端点,那么入栈和出栈都需要遍历,时间复杂度都是O(n)

所以一般链式栈都是 使用链表头部进行 入栈和出栈 时间复杂度是 O(1)

1.8.4.2顺序栈的操作:

创建栈

清空栈

销毁栈

向栈中插入元素:入栈/压栈(push)

获取栈中的元素:出栈/弹栈(pop)

判断栈是否为空

打印栈中所有元素--学习阶段看现象用的

1.8.4.3顺序栈代码实现:

头文件:

  1. #ifndef __LINK_STACK_H__
  2. #define __LINK_STACK_H__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. typedef struct _Node{
  7. int data;
  8. struct _Node *next;
  9. }node_t;
  10. typedef struct _Stack{
  11. unsigned int count;
  12. node_t *top;
  13. }stack_t;
  14. int create_stack(stack_t **my_stack);
  15. int push_stack(stack_t *my_stack, int value);
  16. int pop_stack(stack_t *my_stack, int *num);
  17. int is_empty(stack_t *my_stack);
  18. int clean_stack(stack_t *my_stack);
  19. int destroy_stack(stack_t **my_stack);
  20. int print_stack(stack_t *my_stack);
  21. #endif

函数:

  1. #include "link_stack.h"
  2. //创建栈
  3. int create_stack(stack_t **my_stack){
  4. if(NULL == my_stack){
  5. printf("入参为NULL 请检查\n");
  6. return -1;
  7. }
  8. *my_stack = (stack_t *)malloc(sizeof(stack_t));
  9. if(NULL == *my_stack){
  10. printf("内存分配失败\n");
  11. exit(-1);
  12. }
  13. (*my_stack)->count = 0;
  14. (*my_stack)->top = NULL;
  15. return 0;
  16. }
  17. //入栈
  18. int push_stack(stack_t *my_stack, int value){
  19. if(NULL == my_stack){
  20. printf("入参为NULL 请检查\n");
  21. return -1;
  22. }
  23. //先创建新的 数据节点
  24. node_t *pnew = (node_t *)malloc(sizeof(node_t));
  25. if(NULL == pnew){
  26. printf("内存分配失败\n");
  27. exit(-1);
  28. }
  29. pnew->data = value;
  30. pnew->next = NULL;
  31. //将新节点头插到链表中
  32. pnew->next = my_stack->top;
  33. my_stack->top = pnew;
  34. my_stack->count++;
  35. return 0;
  36. }
  37. //出栈
  38. int pop_stack(stack_t *my_stack, int *num){
  39. if(NULL == my_stack){
  40. printf("入参为NULL 请检查\n");
  41. return -1;
  42. }
  43. if(is_empty(my_stack)){
  44. printf("栈为空 出栈失败\n");
  45. return -1;
  46. }
  47. //数据出栈
  48. *num = my_stack->top->data;
  49. //删除已经出栈数据的节点--头删
  50. node_t *pdel = my_stack->top;
  51. my_stack->top = pdel->next;
  52. free(pdel);
  53. pdel = NULL;
  54. my_stack->count--;
  55. return 0;
  56. }
  57. //判断栈是否为空 返回 1 空 返回 0 非空
  58. int is_empty(stack_t *my_stack){
  59. if(NULL == my_stack){
  60. printf("入参为NULL 请检查\n");
  61. return -1;
  62. }
  63. return my_stack->top == NULL ? 1 : 0;
  64. }
  65. //清空栈
  66. int clean_stack(stack_t *my_stack){
  67. if(NULL == my_stack){
  68. printf("入参为NULL 请检查\n");
  69. return -1;
  70. }
  71. node_t *pdel = NULL;
  72. //循环头删 删除所有的 数据节点
  73. while(NULL != my_stack->top){
  74. pdel = my_stack->top;
  75. my_stack->top = pdel->next;
  76. free(pdel);
  77. }
  78. pdel = NULL;
  79. //个数清零
  80. my_stack->count = 0;
  81. return 0;
  82. }
  83. //销毁栈
  84. int destroy_stack(stack_t **my_stack){
  85. if(NULL == *my_stack || NULL == my_stack){
  86. printf("入参为NULL 请检查\n");
  87. return -1;
  88. }
  89. //先调用清空
  90. clean_stack(*my_stack);
  91. free(*my_stack);
  92. *my_stack = NULL;
  93. return 0;
  94. }
  95. //遍历栈
  96. int print_stack(stack_t *my_stack){
  97. if(NULL == my_stack){
  98. printf("入参为NULL 请检查\n");
  99. return -1;
  100. }
  101. node_t *ptemp = my_stack->top;
  102. while(NULL != ptemp){
  103. printf("%d ", ptemp->data);
  104. ptemp = ptemp->next;
  105. }
  106. printf("\n");
  107. return 0;
  108. }

main.c:

  1. #include "link_stack.h"
  2. int main(){
  3. stack_t *my_stack = NULL;
  4. //创建栈
  5. create_stack(&my_stack);
  6. //数据入栈
  7. push_stack(my_stack, 10);
  8. push_stack(my_stack, 20);
  9. push_stack(my_stack, 30);
  10. //遍历栈
  11. print_stack(my_stack);
  12. //数据出栈
  13. int num = 0;
  14. pop_stack(my_stack, &num);
  15. printf("num = %d\n", num);
  16. pop_stack(my_stack, &num);
  17. printf("num = %d\n", num);
  18. pop_stack(my_stack, &num);
  19. printf("num = %d\n", num);
  20. pop_stack(my_stack, &num);
  21. printf("num = %d\n", num);
  22. //再入栈两个元素
  23. push_stack(my_stack, 520);
  24. push_stack(my_stack, 1314);
  25. //遍历栈
  26. print_stack(my_stack);
  27. //清空栈
  28. clean_stack(my_stack);
  29. //遍历栈
  30. print_stack(my_stack);
  31. //销毁栈
  32. destroy_stack(&my_stack);
  33. printf("my_stack = %p\n", my_stack);
  34. return 0;

1.9队列

1.9.1 概念

队列是一种先进先出的结构。 FIFO(first in first out)

1.9.1.1队列的逻辑结构

线性结构

1.9.1.2队列的存储结构

顺序队列是基于顺序表配个两个下标,一个是队列头 front,一个是队列尾 rear。

顺序队列相当于对顺序表操作的一种约束:一端插入,另一端删除。

一般这种顺序队列我们不使用,因为 入队列时 rear++ 出队列时 front++

相当于每块空间指使用了一次,即使出队列了,空间也不会复用了,相当于一次性的

1.9.1.3 循环队列

这种结构的队列可以让空间复用起来。

每次插入完数据 不要执行 rear++ 而改成 rear = (reat+1)%N

每次删除完数据 不要执行 front++ 而改成 front = (front+1)%N

判断队列空 rear == front

判断队列满 (rear+1)%N == front (浪费了一个存储空间用来和判断空作区分,方便写代

1.9.1.4循序队列的操作:

创建队列

清空队列

销毁队列

入队列

判断队列是否为满

出队列

判断队列是否为空

1.9.1.5顺序队列的代码实现

头文件

  1. #ifndef __SEQ_QUEUE_H__
  2. #define __SEQ_QUEUE_H__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #define N 5
  6. typedef struct _Queue{
  7. int s[N];
  8. int front; //头
  9. int rear; //尾
  10. }queue_t;
  11. int create_queue(queue_t **p);
  12. int push_queue(queue_t *my_queue, int in_data);
  13. int is_full(queue_t *my_queue);
  14. int pop_queue(queue_t *my_queue, int *out_data);
  15. int is_empty(queue_t *my_queue);
  16. int clean_queue(queue_t *my_queue);
  17. int destroy_queue(queue_t **my_queue);
  18. int print_queue(queue_t *my_queue);
  19. #endif

函数

  1. #include "seq_queue.h"
  2. //创建队列
  3. int create_queue(queue_t **p){
  4. if(NULL == p){
  5. printf("入参为 NULL,请检查\n");
  6. return -1;
  7. }
  8. *p = (queue_t *)malloc(sizeof(queue_t));
  9. if(NULL == *p){
  10. printf("内存分配失败\n");
  11. exit(-1);
  12. }
  13. (*p)->front = 0;
  14. (*p)->rear = 0;
  15. return 0;
  16. }
  17. //入队列
  18. int push_queue(queue_t *my_queue, int in_data){
  19. if(NULL == my_queue){
  20. printf("入参为 NULL,请检查\n");
  21. return -1;
  22. }
  23. if(is_full(my_queue)){
  24. printf("队列满 入队列失败\n");
  25. return -1;
  26. }
  27. //执行入队列操作
  28. printf("入之前:front = %d rear = %d\n", my_queue->front, my_queue->rear);
  29. my_queue->s[my_queue->rear] = in_data;
  30. my_queue->rear = (my_queue->rear+1)%N;
  31. printf("入之后:front = %d rear = %d\n", my_queue->front, my_queue->rear);
  32. return 0;
  33. }
  34. //判断队列满 返回1满 0 未满
  35. int is_full(queue_t *my_queue){
  36. if(NULL == my_queue){
  37. printf("入参为 NULL,请检查\n");
  38. return -1;
  39. }
  40. return (my_queue->rear+1)%N == my_queue->front ? 1 : 0;
  41. }
  42. //出队列
  43. int pop_queue(queue_t *my_queue, int *out_data){
  44. if(NULL == my_queue || NULL == out_data){
  45. printf("入参为 NULL,请检查\n");
  46. return -1;
  47. }
  48. if(is_empty(my_queue)){
  49. printf("队列空 出队列失败\n");
  50. return -1;
  51. }
  52. //执行出队列操作
  53. *out_data = my_queue->s[my_queue->front];
  54. my_queue->front = (my_queue->front+1)%N;
  55. return 0;
  56. }
  57. //判断队列空 返回1空 0 非空
  58. int is_empty(queue_t *my_queue){
  59. if(NULL == my_queue){
  60. printf("入参为 NULL,请检查\n");
  61. return -1;
  62. }
  63. return my_queue->front == my_queue->rear ? 1 : 0;
  64. }
  65. //清空队列
  66. int clean_queue(queue_t *my_queue){
  67. if(NULL == my_queue){
  68. printf("入参为 NULL,请检查\n");
  69. return -1;
  70. }
  71. my_queue->front = 0;
  72. my_queue->rear = 0;
  73. return 0;
  74. }
  75. //销毁队列
  76. int destroy_queue(queue_t **my_queue){
  77. if(NULL == my_queue || NULL == *my_queue){
  78. printf("入参为 NULL,请检查\n");
  79. return -1;
  80. }
  81. free(*my_queue);
  82. *my_queue = NULL;
  83. return 0;
  84. }
  85. //遍历队列
  86. int print_queue(queue_t *my_queue){
  87. if(NULL == my_queue){
  88. printf("入参为 NULL,请检查\n");
  89. return -1;
  90. }
  91. int i = 0;
  92. for(i = my_queue->front; (i%N)!=my_queue->rear ;i++){
  93. printf("%d ", my_queue->s[i%N]);
  94. }
  95. printf("\n");
  96. return 0;
  97. }

main.c

  1. #include "seq_queue.h"
  2. int main(){
  3. queue_t *my_queue = NULL;
  4. //创建队列
  5. create_queue(&my_queue);
  6. //数据入队列
  7. push_queue(my_queue, 10);
  8. push_queue(my_queue, 20);
  9. push_queue(my_queue, 30);
  10. push_queue(my_queue, 40);
  11. //push_queue(my_queue, 50);//队列满
  12. //遍历队列
  13. print_queue(my_queue);
  14. //数据出队列
  15. int num = 0;
  16. pop_queue(my_queue, &num);
  17. printf("num = %d\n", num);
  18. pop_queue(my_queue, &num);
  19. printf("num = %d\n", num);
  20. pop_queue(my_queue, &num);
  21. printf("num = %d\n", num);
  22. pop_queue(my_queue, &num);
  23. printf("num = %d\n", num);
  24. //pop_queue(my_queue, &num);//失败 队列空
  25. //printf("num = %d\n", num);
  26. //遍历队列
  27. print_queue(my_queue);
  28. //再入队列几个数据
  29. push_queue(my_queue, 50);
  30. push_queue(my_queue, 60);
  31. push_queue(my_queue, 70);
  32. push_queue(my_queue, 80);
  33. //遍历队列
  34. print_queue(my_queue);
  35.     //清空队列
  36.     clean_queue(my_queue);
  37.     //遍历队列
  38.     print_queue(my_queue);
  39.     //销毁队列
  40.     destroy_queue(&my_queue);
  41.     printf("my_queue = %p\n", my_queue);
  42. return 0;
  43. }

1.10链式队列

1.10.1概念:

链式队列是基于链表实现的。

链式队列相当于对链表操作的一种约束:一端插入,另一端删除。

一般使用 尾插头删法

1.10.2链式队列的操作:

创建队列

清空队列

销毁队列

入队列

出队列

判断队列是否为空

遍历队列--学习阶段看现象用的

1.10.3代码实现

头文件

  1. #ifndef __LINK_QUEUE_H__
  2. #define __LINK_QUEUE_H__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. typedef struct _Node{
  6. int data;
  7. struct _Node *next;
  8. }node_t;
  9. typedef struct _Queue{
  10. node_t *front;
  11. node_t *rear;
  12. }queue_t;
  13. int create_queue(queue_t **p);
  14. int push_queue(queue_t *my_queue, int in_data);
  15. int print_queue(queue_t *my_queue);
  16. int pop_queue(queue_t *my_queue, int *out_data);
  17. int is_empty(queue_t *my_queue);
  18. int clean_queue(queue_t *my_queue);
  19. int destroy_queue(queue_t **my_queue);
  20. #endif

函数

  1. #include "link_queue.h"
  2. //创建队列
  3. int create_queue(queue_t **p){
  4. if(NULL == p){
  5. printf("入参为NULL, 请检查\n");
  6. return -1;
  7. }
  8. *p = (queue_t *)malloc(sizeof(queue_t));
  9. if(NULL == *p){
  10. printf("内存分配失败\n");
  11. exit(-1);
  12. }
  13. (*p)->front = NULL;
  14. (*p)->rear = NULL;
  15. return 0;
  16. }
  17. //数据入队列
  18. int push_queue(queue_t *my_queue, int in_data){
  19. if(NULL == my_queue){
  20. printf("入参为NULL, 请检查\n");
  21. return -1;
  22. }
  23. //先创建数据节点
  24. node_t *pnew = (node_t *)malloc(sizeof(node_t));
  25. if(NULL == pnew){
  26. printf("内存分配失败\n");
  27. exit(-1);
  28. }
  29. pnew->data = in_data;
  30. pnew->next = NULL;
  31. //执行插入操作
  32. if(my_queue->front==NULL && my_queue->rear==NULL){
  33. //说明是第一个数据节点
  34. my_queue->front = pnew;
  35. my_queue->rear = pnew;
  36. }else{
  37. //说明不是第一个数据节点
  38. my_queue->rear->next = pnew;
  39. my_queue->rear = pnew;
  40. }
  41. return 0;
  42. }
  43. //遍历队列
  44. int print_queue(queue_t *my_queue){
  45. if(NULL == my_queue){
  46. printf("入参为NULL, 请检查\n");
  47. return -1;
  48. }
  49. node_t *ptemp = my_queue->front;
  50. while(NULL != ptemp){
  51. printf("%d ", ptemp->data);
  52. ptemp = ptemp->next;
  53. }
  54. printf("\n");
  55. return 0;
  56. }
  57. //数据出队列
  58. int pop_queue(queue_t *my_queue, int *out_data){
  59. if(NULL == my_queue || NULL == out_data){
  60. printf("入参为NULL, 请检查\n");
  61. return -1;
  62. }
  63. if(is_empty(my_queue)){
  64. printf("队列已经空了 出队列失败\n");
  65. return -1;
  66. }
  67. //执行出队列的操作
  68. *out_data = my_queue->front->data;
  69. node_t *pdel = my_queue->front;
  70. my_queue->front = pdel->next;
  71. free(pdel);
  72. pdel = NULL;
  73. //如果出队列之后没有节点了 rear的指向也要更新
  74. if(NULL == my_queue->front){
  75. my_queue->rear = NULL;
  76. }
  77. return 0;
  78. }
  79. //判断队列是否为空 返回1空 0非空
  80. int is_empty(queue_t *my_queue){
  81. if(NULL == my_queue){
  82. printf("入参为NULL, 请检查\n");
  83. return -1;
  84. }
  85. return my_queue->front==NULL?1:0;
  86. }
  87. //清空队列
  88. int clean_queue(queue_t *my_queue){
  89. if(NULL == my_queue){
  90. printf("入参为NULL, 请检查\n");
  91. return -1;
  92. }
  93. //头删法删除所有节点
  94. node_t *pdel = NULL;
  95. while(NULL != my_queue->front){
  96. pdel = my_queue->front;
  97. my_queue->front = pdel->next;
  98. free(pdel);
  99. }
  100. pdel = NULL;
  101. my_queue->rear = NULL;
  102. return 0;
  103. }
  104. //销毁队列
  105. int destroy_queue(queue_t **my_queue){
  106. if(NULL == my_queue || NULL == *my_queue){
  107. printf("入参为NULL, 请检查\n");
  108. return -1;
  109. }
  110. //先调用清空
  111. clean_queue(*my_queue);
  112. //再销毁
  113. free(*my_queue);
  114. *my_queue = NULL;
  115. return 0;
  116. }

main.c

  1. #include "link_queue.h"
  2. int main(){
  3. queue_t *my_queue = NULL;
  4. //创建队列
  5. create_queue(&my_queue);
  6. //数据入队列
  7. push_queue(my_queue, 10);
  8. push_queue(my_queue, 20);
  9. //printf("front = %p rear = %p\n", my_queue->front, my_queue->rear);
  10. //遍历队列
  11. print_queue(my_queue);
  12. //数据出队列
  13. int num = 0;
  14. pop_queue(my_queue, &num);
  15. printf("num = %d\n", num);
  16. //printf("front = %p rear = %p\n", my_queue->front, my_queue->rear);
  17. pop_queue(my_queue, &num);
  18. printf("num = %d\n", num);
  19. //pop_queue(my_queue, &num);//失败 队列已经空了
  20. //printf("num = %d\n", num);
  21. //printf("front = %p rear = %p\n", my_queue->front, my_queue->rear);
  22. //再入队列几个元素
  23. push_queue(my_queue, 10);
  24. push_queue(my_queue, 20);
  25. push_queue(my_queue, 30);
  26. push_queue(my_queue, 40);
  27. //遍历队列
  28. print_queue(my_queue);
  29. //清空队列
  30. clean_queue(my_queue);
  31. //遍历队列
  32. print_queue(my_queue);
  33. //销毁队列
  34. destroy_queue(&my_queue);
  35. printf("my_queue = %p\n", my_queue);
  36. return 0;
  37. }

1.11递归

1.11.1概念

递归就是在函数内部再次调用函数自身的逻辑。

注意,每个递归都要有一个出口,否则函数无限次的调用下去,会把内存资源耗尽,

会出现堆栈溢出,导致段错误。

注意,递归本质就是函数调用,不是C语言程序结构的一种。

C语言程序结构只有三种:顺序结构、分支结构(选择结构)、循环结构。

1.11.2例子

1~n求和

  1. #include <stdio.h>
  2. //1~n求和
  3. int func(int n){
  4. if(n==1){
  5. return 1;//递归的出口
  6. }
  7. return n+func(n-1);
  8. }
  9. int main(int argc, const char *argv[])
  10. {
  11. int ret = 0;
  12. ret = func(5);
  13. printf("sum = %d\n", ret);
  14. return 0;
  15. }

青蛙跳台阶,共有10阶台阶,青蛙每次可以选择跳一阶或者两阶,

问:青蛙跳上这10个台阶共有多少种跳法

  1. #include <stdio.h>
  2. //计算跳上第n阶 有几种跳法
  3. //返回值:就是跳法的多少
  4. int func(int n){
  5. //递归的出口
  6. if(1 == n){
  7. return 1;
  8. }
  9. if(2 == n){
  10. return 2;
  11. }
  12. return func(n-1) + func(n-2);
  13. }
  14. int main(int argc, const char *argv[])
  15. {
  16. int ret = 0;
  17. ret = func(10);
  18. printf("ret = %d\n", ret);//89
  19. return 0;
  20. }

1.12.1快速排序

1.12.1.1概念

快速排序是冒泡排序的优化,也是一种基于交换的排序方式。

时间复杂度 O(nlogn)。

1.12.1.2基本思想

分而治之

通过一趟排序,先将数据分成两部分,其中一部分的数据,

都比另一部分的数据大(小),(每部分数据内部不要求有序)

然后,对于两部分数据分别进行上述的排序操作,把没部分数据再分成两部分,

依次类推,直到整个数据有序。

1.12.1.3代码实现

  1. #include <stdio.h>
  2. //一趟排序
  3. int sort(int *s, int low, int high){
  4. //选取一个比较的基准,选待排序列中的那个都可以
  5. //一般情况下,为了方便写代码,我们都选下标最小的那个
  6. int flag = s[low];
  7. while(low<high){
  8. while(s[high] > flag && low < high){//如果是降序,将此处的s[high] > flag 改成 s[high] < flag
  9. high--;
  10. }
  11. if(low<high){
  12. s[low] = s[high];
  13. low++;
  14. }
  15. while(s[low] < flag && low < high){//如果是降序,将此处的s[low] < flag 改成 s[low] > flag
  16. low++;
  17. }
  18. if(low<high){
  19. s[high] = s[low];
  20. high--;
  21. }
  22. }
  23. //记得把 flag 塞回去
  24. s[low] = flag;
  25. return low;//return high也可以,因为此时 low和high是相等的
  26. }
  27. //快速排序
  28. int quick_sort(int *s, int low, int high){
  29. //递归的出口
  30. if(low<high){
  31. int ret = sort(s, low, high);
  32. quick_sort(s, 0, ret-1);
  33. quick_sort(s, ret+1, high);
  34. }
  35. }
  36. int print_arr(int *p){
  37. int i = 0;
  38. for(i = 0; i < 10; i++){
  39. printf("%d ", p[i]);
  40. }
  41. printf("\n");
  42. }
  43. int main(int argc, const char *argv[])
  44. {
  45. int s[10] = {6, 4, 9, 3, 7, 5, 8, 2, 1, 10};
  46. //排序前
  47. print_arr(s);
  48. //执行快速排序
  49. quick_sort(s, 0, 9);
  50. printf("---------------------------\n");
  51. //排序后
  52. print_arr(s);
  53. return 0;
  54. }

1.14树的前中后续遍历

1.14.1概念

前序遍历:根左右,先遍历根节点,然后遍历左子树,最后遍历右子树。

一般在构建树的过程中会用到前序遍历,因为现有根,才有左右子树。

中序遍历:左根右,先遍历左子树,然后遍历根节点,最后遍历右子树。

一般遍历时使用中序,对于有序的树,使用中序遍历能得到有序的序列。

后序遍历:左右根,先遍历左子树,然后遍历右子树,最后遍历根节点。

释放树中所有节点时,一般使用后续,因为如果把根节点释放了,就没法访问左右子树了。

1.14.2图例

前序:ABCDEFGHI

中序:BDCAEHGIF

后序:DCBHIGFEA

1.14.3代码实现

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //树的节点类型
  4. typedef struct _Node{
  5. int data;//数据域
  6. struct _Node *lchild;//指向左孩子的指针
  7. struct _Node *rchild;//指向右孩子的指针
  8. }node_t;
  9. //创建一棵树
  10. int create_tree(node_t **root){
  11. if(NULL == root){
  12. printf("入参为NULL, 请检查\n");
  13. return -1;
  14. }
  15. char value = 0;
  16. scanf("%c", &value);
  17. getchar();//清理垃圾字符
  18. //递归的出口
  19. if('#' == value){
  20. return 0;
  21. }
  22. //分配空间
  23. *root = (node_t *)malloc(sizeof(node_t));
  24. if(NULL == *root){
  25. printf("内存分配失败\n");
  26. exit(-1);
  27. }
  28. (*root)->data = value;
  29. (*root)->lchild = NULL;
  30. (*root)->rchild = NULL;
  31. //创建左子树
  32. create_tree(&((*root)->lchild));
  33. //创建右子树
  34. create_tree(&((*root)->rchild));
  35. return 0;
  36. }
  37. //前序遍历
  38. int qianxu(node_t *root){
  39. if(NULL == root){//递归的出口
  40. return -1;
  41. }
  42. printf("%c", root->data);
  43. qianxu(root->lchild);
  44. qianxu(root->rchild);
  45. return 0;
  46. }
  47. //中序遍历
  48. int zhongxu(node_t *root){
  49. if(NULL == root){//递归的出口
  50. return -1;
  51. }
  52. zhongxu(root->lchild);
  53. printf("%c", root->data);
  54. zhongxu(root->rchild);
  55. return 0;
  56. }
  57. //后序遍历
  58. int houxu(node_t *root){
  59. if(NULL == root){//递归的出口
  60. return -1;
  61. }
  62. houxu(root->lchild);
  63. houxu(root->rchild);
  64. printf("%c", root->data);
  65. return 0;
  66. }
  67. //销毁树
  68. int destroy_tree(node_t **root){
  69.     if(root == NULL || *root == NULL){
  70.         return -1;
  71.     }
  72.     destroy_tree(&((*root)->lchild));
  73.     destroy_tree(&((*root)->rchild));
  74.     free(*root);
  75.     *root = NULL;
  76. return 0;
  77. }
  78. int main(){
  79. node_t *root = NULL;
  80. create_tree(&root);
  81. //前序
  82. printf("前序遍历\n");
  83. qianxu(root);
  84. printf("\n");
  85. //中序
  86. printf("中序遍历\n");
  87. zhongxu(root);
  88. printf("\n");
  89. //后序
  90. printf("后序遍历\n");
  91. houxu(root);
  92. printf("\n");
  93. //销毁树
  94.     destroy_tree(&root);
  95. return 0;
  96. }

1.14.4树的练习

练习1:

已知一棵树前序和中序的遍历结果:

前序: A B C E H F I J D G K

中序: A H E C I F J B D K G

请画出这个树的结构。

解题思路:

根据前序,能确定根,

然后根据中序,确定哪些节点是根的左子树,哪些是根的右子树

然后再根据前序确定子树的根

在根据中序确定子树的子树。。。依次类推。。

 1.15哈希

练习:

从终端输入任意一个只包含小写字母的字符串

输出每个字符出现的次数。

如:输入:aabbccdfe

  1. #include <stdio.h>
  2. int get_index(char value){
  3. return value-'a';
  4. }
  5. int main(){
  6. char s[128] = {0};
  7. printf("请输入字符串(小写):");
  8. scanf("%s", s);
  9. //用来保存字母出现次数的数组
  10. //count[0] --> a出现的次数
  11. //count[1] --> b出现的次数
  12. //...
  13. //count[25] --> z出现的次数
  14. unsigned int count[26] = {0};
  15. //遍历字符串,统计次数
  16. int i = 0;
  17. int index = 0;
  18. while(s[i] != '\0'){
  19. index = get_index(s[i]);
  20. count[index]++;
  21. i++;
  22. }
  23. //输出次数
  24. for(i = 0; i < 26; i++){
  25. printf("%c : %d\n", i+'a', count[i]);
  26. }
  27. return 0;
  28. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/831643
推荐阅读
相关标签
  

闽ICP备14008679号