C语言实现staque结构 [复制链接]
发表于 2022-10-22 15:12:28 | 显示全部楼层 |阅读模式
C语言实现staque结构

 
1. 使用说明

staque结构以单链表方式实现,结合了stack与queue结构:pop_front+push_front使用方式为stack;pop_front+push_back使用方式是queue。
首尾插入和顶部弹出是运行效率最高的,此外还实现了任意位置的插入、移除和访问功能
带有返回值的函数:返回值如果是void*类型,则NULL代表执行失败;如果是int类型,则0为成功,-1为失败。
 
2. 代码实现

staque.h
  1. 1 #ifndef __STAQUE_H
  2. 2 #define __STAQUE_H
  3. 3
  4. 4 typedef
  5. 5 struct unidirectional_node
  6. 6 {
  7. 7     void* p_data;
  8. 8     struct unidirectional_node* p_next;
  9. 9 }STRUCT_UD_NODE,
  10. 10 * P_STRUCT_UD_NODE;
  11. 11
  12. 12 typedef
  13. 13 struct staque
  14. 14 {
  15. 15     unsigned int count;
  16. 16     P_STRUCT_UD_NODE p_first;
  17. 17     P_STRUCT_UD_NODE p_last;
  18. 18 }STRUCT_STAQUE,
  19. 19 * P_STRUCT_STAQUE;
  20. 20
  21. 21 P_STRUCT_STAQUE
  22. 22 f_staque_construct(void);
  23. 23
  24. 24 void
  25. 25 f_staque_push_back(P_STRUCT_STAQUE const p_stq, void* const p_data);
  26. 26
  27. 27 void
  28. 28 f_staque_push_front(P_STRUCT_STAQUE const p_stq, void* const p_data);
  29. 29
  30. 30 void*
  31. 31 f_staque_pop_front(P_STRUCT_STAQUE const p_stq);
  32. 32
  33. 33 int
  34. 34 f_staque_data_insert(P_STRUCT_STAQUE const p_stq, void* const p_data, const unsigned int index);
  35. 35
  36. 36 void*
  37. 37 f_staque_data_remove(P_STRUCT_STAQUE const p_stq, const unsigned int index);
  38. 38
  39. 39 void*
  40. 40 f_staque_data_access(P_STRUCT_STAQUE const p_stq, const unsigned int index);
  41. 41
  42. 42 int
  43. 43 f_staque_data_index(P_STRUCT_STAQUE const p_stq, void* const p_data, unsigned int* const p_index);
  44. 44
  45. 45 void
  46. 46 f_staque_destroy(P_STRUCT_STAQUE const p_stq);
  47. 47
  48. 48 #endif // !__STAQUE_H
复制代码
head 
staque.c
  1.   1 #include "staque.h"
  2.   2 #include <stdlib.h>
  3.   3
  4.   4 #pragma warning(disable:6011)
  5.   5
  6.   6 P_STRUCT_STAQUE f_staque_construct(void)
  7.   7 {
  8.   8     P_STRUCT_STAQUE p_ret_vec = (P_STRUCT_STAQUE)malloc(sizeof(STRUCT_STAQUE));
  9.   9     p_ret_vec->count = 0;
  10. 10     p_ret_vec->p_first = NULL;
  11. 11     p_ret_vec->p_last = NULL;
  12. 12     return p_ret_vec;
  13. 13 }
  14. 14
  15. 15 void innf_add_first_node(P_STRUCT_STAQUE const p_stq, void* const p_data)
  16. 16 {
  17. 17     p_stq->p_first = (P_STRUCT_UD_NODE)malloc(sizeof(STRUCT_UD_NODE));
  18. 18     p_stq->p_first->p_data = p_data;
  19. 19     p_stq->p_first->p_next = NULL;
  20. 20     p_stq->p_last = p_stq->p_first;
  21. 21     p_stq->count = 1;
  22. 22 }
  23. 23
  24. 24 void* innf_remove_last_node(P_STRUCT_STAQUE const p_stq)
  25. 25 {
  26. 26     void* p_ret_data = p_stq->p_first->p_data;
  27. 27     free(p_stq->p_first);
  28. 28     p_stq->p_first = NULL;
  29. 29     p_stq->p_last = NULL;
  30. 30     p_stq->count = 0;
  31. 31     return p_ret_data;
  32. 32 }
  33. 33
  34. 34 void f_staque_push_back(P_STRUCT_STAQUE const p_stq, void* const p_data)
  35. 35 {
  36. 36     switch (p_stq->count)
  37. 37     {
  38. 38     case 0:
  39. 39         innf_add_first_node(p_stq, p_data);
  40. 40         return;
  41. 41     }
  42. 42     P_STRUCT_UD_NODE p_node = (P_STRUCT_UD_NODE)malloc(sizeof(STRUCT_UD_NODE));
  43. 43     p_node->p_data = p_data;
  44. 44     p_node->p_next = NULL;
  45. 45     p_stq->p_last->p_next = p_node;
  46. 46     p_stq->p_last = p_node;
  47. 47     p_stq->count++;
  48. 48 }
  49. 49
  50. 50 void f_staque_push_front(P_STRUCT_STAQUE const p_stq, void* const p_data)
  51. 51 {
  52. 52     switch (p_stq->count) {
  53. 53     case 0:
  54. 54         innf_add_first_node(p_stq, p_data);
  55. 55         return;
  56. 56     }
  57. 57     P_STRUCT_UD_NODE p_node = (P_STRUCT_UD_NODE)malloc(sizeof(STRUCT_UD_NODE));
  58. 58     p_node->p_data = p_data;
  59. 59     p_node->p_next = p_stq->p_first;
  60. 60     p_stq->p_first = p_node;
  61. 61     p_stq->count++;
  62. 62 }
  63. 63
  64. 64 void* f_staque_pop_front(P_STRUCT_STAQUE const p_stq)
  65. 65 {
  66. 66     switch (p_stq->count) {
  67. 67     case 0:
  68. 68         return NULL;
  69. 69     case 1:
  70. 70         return innf_remove_last_node(p_stq);
  71. 71     }
  72. 72     void* p_ret_data = p_stq->p_first->p_data;
  73. 73     P_STRUCT_UD_NODE p_node = p_stq->p_first->p_next;
  74. 74     free(p_stq->p_first);
  75. 75     p_stq->p_first = p_node;
  76. 76     p_stq->count--;
  77. 77     return p_ret_data;
  78. 78 }
  79. 79
  80. 80 int f_staque_data_insert(P_STRUCT_STAQUE const p_stq, void* const p_data, const unsigned int index)
  81. 81 {
  82. 82     switch (index) {
  83. 83     case 0:
  84. 84         f_staque_push_front(p_stq, p_data);
  85. 85         return 0;
  86. 86     }
  87. 87     if (index == p_stq->count) {
  88. 88         f_staque_push_back(p_stq, p_data);
  89. 89         return 0;
  90. 90     }
  91. 91     else if (index > p_stq->count) {
  92. 92         return -1;
  93. 93     }
  94. 94     P_STRUCT_UD_NODE p_node = p_stq->p_first;
  95. 95     for (unsigned int i = 0 ; i < index - 1; i++) {
  96. 96         p_node = p_node->p_next;
  97. 97     }
  98. 98     P_STRUCT_UD_NODE p_node_next = p_node->p_next;
  99. 99     p_node->p_next = (P_STRUCT_UD_NODE)malloc(sizeof(STRUCT_UD_NODE));
  100. 100     p_node->p_next->p_data = p_data;
  101. 101     p_node->p_next->p_next = p_node_next;
  102. 102     p_stq->count++;
  103. 103     return 0;
  104. 104 }
  105. 105
  106. 106 void* f_staque_data_remove(P_STRUCT_STAQUE const p_stq, const unsigned int index)
  107. 107 {
  108. 108     switch (p_stq->count) {
  109. 109     case 0:
  110. 110         return NULL;
  111. 111     }
  112. 112     switch (index) {
  113. 113     case 0:
  114. 114         return f_staque_pop_front(p_stq);
  115. 115     }
  116. 116     if (index >= p_stq->count) {
  117. 117         return NULL;
  118. 118     }
  119. 119     P_STRUCT_UD_NODE p_node = p_stq->p_first;
  120. 120     for (unsigned int i = 0; i < index - 1; i++) {
  121. 121         p_node = p_node->p_next;
  122. 122     }
  123. 123     P_STRUCT_UD_NODE p_node_next = p_node->p_next->p_next;
  124. 124     void* p_ret_data = p_node->p_next->p_data;
  125. 125     free(p_node->p_next);
  126. 126     p_node->p_next = p_node_next;
  127. 127     p_stq->count--;
  128. 128     return p_ret_data;
  129. 129 }
  130. 130
  131. 131 void* f_staque_data_access(P_STRUCT_STAQUE const p_stq, const unsigned int index)
  132. 132 {
  133. 133     switch (p_stq->count) {
  134. 134     case 0:
  135. 135         return NULL;
  136. 136     }
  137. 137     if (index >= p_stq->count) {
  138. 138         return NULL;
  139. 139     }
  140. 140     P_STRUCT_UD_NODE p_node = p_stq->p_first;
  141. 141     for (unsigned int i = 0; i < index; i++) {
  142. 142         p_node = p_node->p_next;
  143. 143     }
  144. 144     return p_node->p_data;
  145. 145 }
  146. 146
  147. 147 int f_staque_data_index(P_STRUCT_STAQUE const p_stq, void* const p_data, unsigned int* const p_index)
  148. 148 {
  149. 149     P_STRUCT_UD_NODE p_node = p_stq->p_first;
  150. 150     for (unsigned int i = 0; i < p_stq->count; i++) {
  151. 151         if (p_node->p_data == p_data) {
  152. 152             *p_index = i;
  153. 153             return 0;
  154. 154         }
  155. 155         p_node = p_node->p_next;
  156. 156     }
  157. 157     return -1;
  158. 158 }
  159. 159
  160. 160 void f_staque_destroy(P_STRUCT_STAQUE const p_stq)
  161. 161 {
  162. 162     if (p_stq == NULL) {
  163. 163         return;
  164. 164     }
  165. 165     while (p_stq->count != 0) {
  166. 166         f_staque_pop_front(p_stq);
  167. 167     }
  168. 168     free(p_stq);
  169. 169 }
复制代码
code 
 
3. 代码测试

source.c
  1. 1 #include "staque.h"
  2. 2 #include <stdio.h>
  3. 3 #include <stdlib.h>
  4. 4
  5. 5 #pragma warning(disable:6011)
  6. 6
  7. 7 int main(int argc, char** argv)
  8. 8 {
  9. 9     P_STRUCT_STAQUE p_stq = f_staque_construct();
  10. 10     const unsigned int arr_size = 10;
  11. 11     int* arr = (int*)malloc(sizeof(int) * arr_size);
  12. 12     printf("arr: ");
  13. 13     for (unsigned int i = 0; i < arr_size; i++) {
  14. 14         *(arr + i) = i * 2;
  15. 15         printf("%d ", *(arr + i));
  16. 16     }
  17. 17     printf("\nstaque push back all\n");
  18. 18     for (unsigned int i = 0; i < arr_size; i++) {
  19. 19         f_staque_push_back(p_stq, arr + i);
  20. 20     }
  21. 21     printf("access staque data\n");
  22. 22     for (unsigned int i = 0; i < p_stq->count; i++) {
  23. 23         printf("%d ", *(int*)f_staque_data_access(p_stq, i));
  24. 24     }
  25. 25     printf("\nstaque push front all\n");
  26. 26     for (unsigned int i = 0; i < arr_size; i++) {
  27. 27         f_staque_push_front(p_stq, arr + i);
  28. 28     }
  29. 29     for (P_STRUCT_UD_NODE p_node = p_stq->p_first; p_node != NULL; p_node = p_node->p_next) {
  30. 30         printf("%d ", *(int*)p_node->p_data);
  31. 31     }
  32. 32     printf("\ninsert to half\n");
  33. 33     f_staque_data_insert(p_stq, &p_stq->count, p_stq->count / 2);
  34. 34     for (P_STRUCT_UD_NODE p_node = p_stq->p_first; p_node != NULL; p_node = p_node->p_next) {
  35. 35         printf("%d ", *(int*)p_node->p_data);
  36. 36     }
  37. 37     unsigned int index = 0;
  38. 38     f_staque_data_index(p_stq, &p_stq->count, &index);
  39. 39     printf("\nget index of that node: %d", index);
  40. 40     printf("\nremove half: %d\n", *(int*)f_staque_data_remove(p_stq, p_stq->count / 2));
  41. 41     for (P_STRUCT_UD_NODE p_node = p_stq->p_first; p_node != NULL; p_node = p_node->p_next) {
  42. 42         printf("%d ", *(int*)p_node->p_data);
  43. 43     }
  44. 44     printf("\nstaque pop front all\n");
  45. 45     while (p_stq->count != 0) {
  46. 46         printf("%d ", *(int*)f_staque_pop_front(p_stq));
  47. 47     }
  48. 48     printf("\n");
  49. 49     f_staque_destroy(p_stq);
  50. 50     free(arr);
  51. 51     return 0;
  52. 52 }
复制代码
test 
运行结果:
arr: 0 2 4 6 8 10 12 14 16 18
staque push back all
access staque data
0 2 4 6 8 10 12 14 16 18
staque push front all
18 16 14 12 10 8 6 4 2 0 0 2 4 6 8 10 12 14 16 18
insert to half
18 16 14 12 10 8 6 4 2 0 21 0 2 4 6 8 10 12 14 16 18
get index of that node: 10
remove half: 20
18 16 14 12 10 8 6 4 2 0 0 2 4 6 8 10 12 14 16 18
staque pop front all
18 16 14 12 10 8 6 4 2 0 0 2 4 6 8 10 12 14 16 18

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表