list.c 8.7 KB

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