list.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "list.h"
  2. #include "string.h"
  3. #include <stdint.h>
  4. #include<string.h>
  5. #include <stdlib.h>
  6. #include<stdbool.h>
  7. #include "esp_heap_caps.h"
  8. #if 1
  9. Node *newNode(int x,int cmd,char *data,int len)
  10. #else
  11. Node *newNode(int x)
  12. #endif
  13. {
  14. #if 0
  15. Node *pnt = (Node *) malloc (sizeof(Node)); /* allocates physical memory for the node */
  16. #else
  17. Node *pnt = (Node *) heap_caps_malloc(sizeof(Node),MALLOC_CAP_RTCRAM); /* allocates physical memory for the node */
  18. #endif
  19. pnt -> n = x;
  20. /* inserts the information received as input in the field(s) in the list */
  21. /* entering additional information if necessary...
  22. * pnt -> b = y;
  23. * pnt -> c = z;
  24. * ... and so on
  25. */
  26. pnt -> cmd = cmd;
  27. #if 0
  28. pnt -> data = malloc(len);
  29. #else
  30. pnt -> data = heap_caps_malloc(len,MALLOC_CAP_RTCRAM);
  31. #endif
  32. #include "string.h"
  33. memcpy(pnt -> data,data,len);
  34. pnt -> len = len;
  35. pnt -> next = NULL; /* initialize the pointer */
  36. /* an insert function will take care of properly setting the next variable */
  37. return pnt;
  38. }
  39. //cmd,data,len
  40. Node *preInsert(Node *top, int x,int cmd,char *data,int len) {
  41. Node *tmp = newNode(x,cmd,data,len); /* create a node with input information */
  42. tmp -> next = top; /* top corresponds to the first element of the list BEFORE this operation,
  43. * which provides to put "tmp" at the top of the list,
  44. * thus becoming the new "top" */
  45. return tmp; /* returns the new first element of the list */
  46. }
  47. Node *orderInsert(Node *top, int x,int cmd,char *data,int len) {
  48. if (top == NULL || top -> n > x) {
  49. /* if top == NULL is true then the element will be positioned at the bottom of the list,
  50. * if top -> n > x then the element will be positioned in the middle of the list.
  51. * OBSERVATION: the first condition is fundamental to the function of the function
  52. * while the second one can be modeled according to the needs, in this case the condition
  53. * top -> n > x imposes an increasing order of the elements of the list, but it could well
  54. * be top -> n < x if you wanted to insert elements in descending order. */
  55. Node *tmp = newNode(x,cmd,data,len); /* creates a node with the information entered as input */
  56. tmp -> next = top; /* points to the next node in the list */
  57. top = tmp; /* inserts the node in the list */
  58. return top;
  59. }
  60. else
  61. top -> next = orderInsert(top -> next, x,cmd,data,len); /* "scrolls" the list, examining the next node */
  62. return top;
  63. }
  64. Node *postInsert(Node *top, int x,int cmd,char *data,int len) {
  65. if (top == NULL)
  66. return newNode(x,cmd,data,len); /* inserts the item at the bottom of the list */
  67. else
  68. top -> next = postInsert(top -> next, x,cmd,data,len); /* if it is not at the bottom, move on */
  69. return top;
  70. }
  71. Node *findNode(Node *top, int k) {
  72. if (top == NULL || top -> n == k)
  73. return top; /* returns the found node (NULL if it was not found) */
  74. else
  75. return findNode(top -> next, k); /* scrolls the list to search for the item */
  76. }
  77. Node *deleteNode(Node *top, int k) {
  78. Node *tmp; /* temporary node */
  79. if (top != NULL) {
  80. if (top -> n == k) { /* if the element was found, it is deleted */
  81. top -> n = 0;
  82. top -> cmd= 0;
  83. memset(top ->data,0x00,top -> len);
  84. top -> len = 0;
  85. // 释放一个已分配的msgid_num
  86. deallocateMsgIdNum(top->n);
  87. tmp = top -> next; /* set the temporary node to the next element (in order not to "lose" the list) */
  88. heap_caps_free(top->data);//释放data
  89. heap_caps_free(top); /* frees the physical memory associated with the node to be deleted */
  90. top = tmp; /* set the pointer to the next node */
  91. }
  92. else
  93. top -> next = deleteNode(top -> next, k); /* scrolls the list in order to search the item to be deleted */
  94. }
  95. return top;
  96. }
  97. Node *deleteNode_head(Node *top) {
  98. Node *tmp; /* temporary node */
  99. if (top != NULL) {
  100. if (1)
  101. { /* if the element was found, it is deleted */
  102. // 释放一个已分配的msgid_num
  103. deallocateMsgIdNum(top->n);
  104. tmp = top -> next; /* set the temporary node to the next element (in order not to "lose" the list) */
  105. //free(top->data);//释放data
  106. free(top); /* frees the physical memory associated with the node to be deleted */
  107. top = tmp; /* set the pointer to the next node */
  108. }
  109. else
  110. top -> next = deleteNode_head(top -> next); /* scrolls the list in order to search the item to be deleted */
  111. }
  112. return top;
  113. }
  114. Node *deleteList(Node *top) {
  115. if (top != NULL) { /* if not reached end of the list... */
  116. deleteList(top -> next); /* ...move on */
  117. //free(top->data);
  118. free(top); /* delete the node */
  119. return NULL;
  120. }
  121. //else
  122. return NULL;
  123. }
  124. // 函数用于复制链表
  125. Node* copyList( Node* originalList) {
  126. if (originalList == NULL) {
  127. return NULL;
  128. }
  129. Node* current = originalList; // 用于遍历原链表
  130. Node* newList = NULL; // 新链表的头节点
  131. Node* tail = NULL; // 新链表的尾节点
  132. // 遍历原链表
  133. while (current != NULL) {
  134. // 创建新节点
  135. Node* newNode = (Node*)malloc(sizeof(Node));
  136. newNode->n = current->n;
  137. newNode->cmd = current->cmd;
  138. newNode->len = current->len;
  139. newNode->data = malloc(current->len);
  140. memcpy(newNode->data,current->data,current->len);
  141. newNode->next = NULL;
  142. // 如果新链表为空,设置新链表的头节点
  143. if (newList == NULL) {
  144. newList = newNode;
  145. tail = newNode;
  146. } else {
  147. // 否则,将新节点连接到新链表的尾部
  148. tail->next = newNode;
  149. tail = newNode;
  150. }
  151. current = current->next;
  152. }
  153. return newList; // 返回新链表的头节点
  154. }
  155. void printList(Node *top) {
  156. if (top != NULL) {
  157. printf("n=%d ", top -> n);
  158. printf(" data=%s", top -> data);
  159. printf(" len=%d\r\n", top -> len);
  160. printList(top -> next);
  161. }
  162. else
  163. printf("NULL\n");
  164. }
  165. void MergeSort(Node **top) {
  166. Node *tmp = *top, *a, *b;
  167. if (tmp != NULL && tmp -> next != NULL) {
  168. Split(tmp, &a, &b); /* (divide) split head into "a" and "b" sublists */
  169. /* (conquer) sort the sublists */
  170. MergeSort(&a);
  171. MergeSort(&b);
  172. *top = Merge(a, b); /* (combine) merge the two sorted lists together */
  173. }
  174. }
  175. Node* Merge(Node *top1, Node *top2) {
  176. if (top1 == NULL)
  177. return top2;
  178. else
  179. if (top2 == NULL)
  180. return top1;
  181. Node* pnt = NULL;
  182. /* pick either top1 or top2, and merge them */
  183. if (top1 -> n <= top2 -> n) {
  184. pnt = top1;
  185. pnt -> next = Merge(top1 -> next, top2);
  186. }
  187. else {
  188. pnt = top2;
  189. pnt -> next = Merge(top1, top2 -> next);
  190. }
  191. return pnt;
  192. }
  193. void Split(Node *top, Node **front, Node **back) {
  194. Node* fast = top -> next;
  195. Node* slow = top;
  196. /* fast pointer advances two nodes, slow pointer advances one node */
  197. while (fast != NULL) {
  198. fast = fast -> next; /* "fast" moves on first time */
  199. if (fast != NULL) {
  200. slow = slow -> next; /* "slow" moves on first time */
  201. fast = fast -> next; /* "fast" moves on second time */
  202. }
  203. }
  204. /* "slow" is before the middle in the list, so split it in two at that point */
  205. *front = top;
  206. *back = slow -> next;
  207. slow -> next = NULL; /* end of the input list */
  208. }
  209. int countNodes(Node *top) {
  210. if (top == NULL)
  211. return 0;
  212. else
  213. return 1 + countNodes(top -> next);
  214. }
  215. int countClockNodes_byCMD(Node *top,char type_cmd)
  216. {
  217. if (top == NULL)
  218. return 0;
  219. else
  220. {
  221. if(top->cmd == type_cmd)
  222. return 1 + countClockNodes_byCMD(top->next,type_cmd);
  223. else
  224. return countClockNodes_byCMD(top->next,type_cmd);
  225. }
  226. }
  227. bool msgidNumAllocated[MAX_MSG_ID_NUM] = {false};
  228. // 分配一个可用的msgid_num
  229. int allocateMsgIdNum() {
  230. for (int i = 1; i <= MAX_MSG_ID_NUM; i++) {
  231. if (!msgidNumAllocated[i - 1]) {
  232. msgidNumAllocated[i - 1] = true; // 标记为已分配
  233. return i;
  234. }
  235. }
  236. return -1; // 没有可用的msgid_num
  237. }
  238. // 释放一个已分配的msgid_num
  239. void deallocateMsgIdNum(int idNum) {
  240. if (idNum >= 1 && idNum <= MAX_MSG_ID_NUM) {
  241. msgidNumAllocated[idNum - 1] = false; // 标记为未分配
  242. }
  243. }
  244. void wait_Send_Data_Insert(int msgid,uint8_t *data,int len)
  245. {
  246. }
  247. void wait_Send_Data_Delete(int msgid)
  248. {
  249. }