Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-06-20 17:11:37
  4. * @LastEditTime: 2021-06-20 19:21:51
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \slist\slist.c
  8. */
  9. #include "slist.h"
  10. #include "stdlib.h"
  11. #include "stdio.h"
  12. pkt_node_t pkt_creat_slist(void)
  13. {
  14. pkt_node_t p;
  15. p = (pkt_node_t)malloc(sizeof(struct pkt_node));
  16. p->next = NULL;
  17. return p;
  18. }
  19. int pkt_get_slist_length(pkt_node_t l)
  20. {
  21. PKT_ASSERT(l);
  22. pkt_node_t p = l;
  23. int i = 0;
  24. while (p->next)
  25. {
  26. p = p->next;
  27. i++;
  28. printf("l[%d] = %d\n", i, p->data);
  29. }
  30. return i;
  31. }
  32. int pkt_insert_slist_head(pkt_node_t l, data_type data)
  33. {
  34. PKT_ASSERT(l);
  35. pkt_node_t p;
  36. p = (pkt_node_t)malloc(sizeof(struct pkt_node));
  37. if (p == NULL)
  38. {
  39. printf("no mem\n");
  40. return -1;
  41. }
  42. p->next = l->next;
  43. p->data = data;
  44. l->next = p;
  45. return 0;
  46. }
  47. int pkt_insert_slist_by_pos(pkt_node_t l, data_type data, int pos)
  48. {
  49. PKT_ASSERT(l);
  50. pkt_node_t p = l;
  51. int i = 0;
  52. if (pos < 0)
  53. {
  54. printf("error pos is < 0\n");
  55. return 0;
  56. }
  57. while (p && pos > i)
  58. {
  59. p = p->next;
  60. i++;
  61. }
  62. if (!p)
  63. {
  64. printf("input pos too big!\n");
  65. return -1;
  66. }
  67. pkt_node_t n = (pkt_node_t)malloc(sizeof(struct pkt_node));
  68. if (n == NULL)
  69. {
  70. printf("no mem\n");
  71. return -1;
  72. }
  73. n->data = data;
  74. n->next = p->next;
  75. p->next = n;
  76. return 0;
  77. }
  78. pkt_node_t pkt_find_node_by_pos(pkt_node_t l, int pos)
  79. {
  80. pkt_node_t p = l->next;
  81. int i = 0;
  82. if (pos < 0)
  83. {
  84. printf("error pos is < 0\n");
  85. return NULL;
  86. }
  87. while (p->next && pos > i)
  88. {
  89. p = p->next;
  90. i++;
  91. }
  92. if (pos == i)
  93. {
  94. return p;
  95. }
  96. printf("input pos is too big\n");
  97. return NULL;
  98. }
  99. pkt_node_t pkt_find_node_by_data(pkt_node_t l, data_type data)
  100. {
  101. pkt_node_t p = l->next;
  102. while (p && p->data != data)
  103. {
  104. p = p->next;
  105. }
  106. if (p == NULL)
  107. {
  108. printf("no find this data node\n");
  109. return NULL;
  110. }
  111. return p;
  112. }
  113. int pkt_del_node_by_pos(pkt_node_t l, int pos)
  114. {
  115. PKT_ASSERT(l);
  116. if (pos < 0)
  117. {
  118. printf("error pos is < 0\n");
  119. return -1;
  120. }
  121. pkt_node_t p;
  122. if (pos == 0)
  123. {
  124. p = l;
  125. }
  126. else
  127. {
  128. p = pkt_find_node_by_pos(l, pos - 1);
  129. }
  130. if (p == NULL && p->next) /* if [->next == NULL], is at list tail, did not del tail->next */
  131. {
  132. return -1;
  133. }
  134. pkt_node_t del = p->next;
  135. p->next = p->next->next;
  136. free(del);
  137. return 0;
  138. }
  139. /* delete node from list */
  140. int pkt_del_node(pkt_node_t l, pkt_node_t n)
  141. {
  142. pkt_node_t p = l;
  143. while (p && p->next != n)
  144. {
  145. p = p->next;
  146. }
  147. if (!p->next)
  148. {
  149. return -1;
  150. }
  151. p->next = p->next->next;
  152. free(n);
  153. return 0;
  154. }
  155. /* frist find the node by data, then delete node */
  156. int pkt_del_node_by_data(pkt_node_t l, data_type data)
  157. {
  158. PKT_ASSERT(l);
  159. pkt_node_t p;
  160. if ((p = pkt_find_node_by_data(l, data)) == NULL)
  161. {
  162. return -1;
  163. }
  164. return pkt_del_node(l, p);
  165. }
  166. int pkt_rever_slist(pkt_node_t l)
  167. {
  168. PKT_ASSERT(l);
  169. if (!l->next)
  170. {
  171. printf("this is a empty list\n");
  172. return -1;
  173. }
  174. #if 0
  175. pkt_node_t p1 = l->next;
  176. pkt_node_t p2 = l->next->next;
  177. p1->next = NULL;
  178. pkt_node_t tmp;
  179. while (p2)
  180. {
  181. tmp = p2->next;
  182. p2->next = p1;
  183. p1 = p2;
  184. p2 = tmp;
  185. }
  186. l->next = p1; /* head next point p1 */
  187. #else
  188. pkt_node_t p1 = l->next;
  189. pkt_node_t tmp;
  190. l->next = NULL; /* empty the list */
  191. while (p1)
  192. {
  193. tmp = p1; /* unattach a node tmp */
  194. p1 = p1->next;
  195. tmp->next = l->next; /* insert tmp node to head */
  196. l->next = tmp;
  197. }
  198. #endif
  199. return 0;
  200. }
  201. int pkt_insert_node_by_data_order(pkt_node_t l, data_type data)
  202. {
  203. PKT_ASSERT(l);
  204. pkt_node_t p = l;
  205. while (p->next && data < p->next->data)
  206. {
  207. p = p->next;
  208. }
  209. pkt_node_t n = (pkt_node_t)malloc(sizeof(struct pkt_node));
  210. if (n == NULL)
  211. {
  212. return -1;
  213. }
  214. n->data = data;
  215. n->next = p->next;
  216. p->next = n;
  217. return 0;
  218. }
  219. int pkt_list_by_data_order(pkt_node_t l)
  220. {
  221. PKT_ASSERT(l);
  222. if (!l->next || !l->next->next)
  223. {
  224. printf("this list node <= 1\n");
  225. return -1;
  226. }
  227. pkt_node_t p = l->next->next;
  228. pkt_node_t r = l;
  229. r->next->next = NULL;
  230. pkt_node_t q;
  231. while (p->next)
  232. {
  233. q = p;
  234. p = p->next;
  235. r = l;
  236. while (r->next && q->data > r->next->data) /* ascending order */
  237. {
  238. r = r->next;
  239. }
  240. q->next = r->next;
  241. r->next = q;
  242. }
  243. return 0;
  244. }
  245. int pkt_list_by_data_order_with_fun(pkt_node_t l, int (*cmp)(pkt_node_t l1, pkt_node_t l2))
  246. {
  247. PKT_ASSERT(l);
  248. if (!l->next || !l->next->next)
  249. {
  250. printf("this list node <= 1\n");
  251. return -1;
  252. }
  253. pkt_node_t p = l->next->next;
  254. pkt_node_t r = l;
  255. r->next->next = NULL;
  256. pkt_node_t q;
  257. while (p->next)
  258. {
  259. q = p;
  260. p = p->next;
  261. r = l;
  262. while (r->next && !cmp(r->next, q))
  263. {
  264. r = r->next;
  265. }
  266. q->next = r->next;
  267. r->next = q;
  268. }
  269. return 0;
  270. }
  271. int cmp(pkt_node_t l1, pkt_node_t l2)
  272. {
  273. if (l2->data > l1->data)
  274. {
  275. return 0;
  276. }
  277. return 1;
  278. }
  279. int main(int argc, char **argv)
  280. {
  281. pkt_node_t slist;
  282. slist = pkt_creat_slist();
  283. #if 1
  284. pkt_insert_slist_head(slist, 22);
  285. pkt_insert_slist_head(slist, 47);
  286. pkt_insert_slist_head(slist, 89);
  287. pkt_insert_slist_head(slist, 44);
  288. pkt_insert_slist_head(slist, 6568);
  289. pkt_insert_slist_head(slist, 77);
  290. #else
  291. pkt_insert_node_by_data_order(slist, 99);
  292. pkt_insert_node_by_data_order(slist, 1);
  293. pkt_insert_node_by_data_order(slist, 5);
  294. #endif
  295. pkt_list_by_data_order(slist);
  296. pkt_get_slist_length(slist);
  297. int pos;
  298. while (1)
  299. {
  300. scanf("%d", &pos);
  301. #if 0
  302. pkt_node_t node = pkt_find_node_by_pos(slist, pos);
  303. if (node)
  304. {
  305. printf("pos%d data: %d\n", pos, node->data);
  306. }
  307. #elif 0
  308. pkt_find_node_by_data(slist, pos);
  309. #elif 0
  310. pkt_insert_slist_by_pos(slist, 100, pos);
  311. pkt_get_slist_length(slist);
  312. #elif 0
  313. pkt_del_node_by_pos(slist, pos);
  314. pkt_get_slist_length(slist);
  315. #elif 0
  316. pkt_del_node_by_data(slist, pos);
  317. pkt_get_slist_length(slist);
  318. #elif 0
  319. printf("==========================================\n");
  320. pkt_rever_slist(slist);
  321. pkt_get_slist_length(slist);
  322. #elif 1
  323. pkt_insert_node_by_data_order(slist, pos);
  324. pkt_get_slist_length(slist);
  325. }
  326. #endif
  327. return 0;
  328. }