Chinaunix首页 | 论坛 | 博客
  • 博客访问: 358865
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-18 11:40:58


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>

  4. #define return_val_if_fail(expr, val) if (!(expr)) return (val)

  5. typedef void* mypointer;
  6. typedef int (*myCompareFunc)(mypointer a, mypointer b);
  7. typedef void (*myfunc)(mypointer a, mypointer b);
  8. typedef struct _SList SList;

  9. struct _SList
  10. {
  11.         mypointer data;
  12.         SList *next;
  13. };

  14. SList*
  15. myAlloc()
  16. {
  17.         SList *psl = (SList*)malloc(sizeof(SList));
  18.         if (NULL == psl) {
  19.                 printf("out of memory");
  20.                 return NULL;
  21.         }
  22.         psl->data = NULL;
  23.         psl->next = NULL;

  24.         return (psl);
  25. }

  26. void
  27. myFree(SList *psl)
  28. {
  29.         assert(NULL != psl);
  30.         free(psl);
  31.         psl = NULL;
  32. }

  33. SList*
  34. slistLast(SList *list)
  35. {
  36.         if (list) {
  37.                 while (list->next)
  38.                         list = list->next;
  39.         }

  40.         return (list);
  41. }

  42. SList*
  43. slistAppend(SList *list, mypointer data)
  44. {
  45.         SList *new_list;
  46.         SList *last;

  47.         new_list = myAlloc();
  48.         new_list->data = data;
  49.         new_list->next = NULL;

  50.         if (list) {
  51.                 last = slistLast(list);
  52.                 last->next = new_list;

  53.                 return (list);
  54.         } else {
  55.                 return (new_list);
  56.         }
  57. }

  58. SList*
  59. slistPrepend(SList *list, mypointer data)
  60. {
  61.         SList *new_list;

  62.         new_list = myAlloc();
  63.         new_list->data = data;
  64.         new_list->next = list;

  65.         return (new_list);
  66. }

  67. SList*
  68. slistInsert(SList *list, mypointer data, int pos)
  69. {
  70.         SList *pre_list;
  71.         SList *tmp_list;
  72.         SList *new_list;

  73.         if (pos < 0)
  74.                 return slistAppend(list, data);
  75.         else if (pos == 0)
  76.                 return slistPrepend(list, data);

  77.         new_list = myAlloc();
  78.         new_list->data = data;

  79.         if (!list) {
  80.                 new_list->next = NULL;
  81.                 return (new_list);
  82.         }

  83.         pre_list = NULL;
  84.         tmp_list = list;

  85.         while ((pos-- > 0) && tmp_list) {
  86.                 pre_list = tmp_list;
  87.                 tmp_list = tmp_list->next;
  88.         }

  89.         if (pre_list) {
  90.                 new_list->next = pre_list->next;
  91.                 pre_list->next = new_list;
  92.         } else {
  93.                 new_list->next = list;
  94.                 list = new_list;
  95.         }

  96.         return (list);
  97. }

  98. SList*
  99. slistInsertBefore(SList *list, SList *sibling, mypointer data)
  100. {
  101.         if (!list) {
  102.                 list = myAlloc();
  103.                 list->data = data;
  104.                 list->next = NULL;
  105.                 return (list);
  106.         } else {
  107.                 SList *node, *last = NULL;
  108.                 for (node = list; node; last = node, node = node->next)
  109.                         if (node == sibling)
  110.                                 break;
  111.                 if (!last) {
  112.                         node = myAlloc();
  113.                         node->data = data;
  114.                         node->next = list;

  115.                         return (node);
  116.                 } else {
  117.                         node = myAlloc();
  118.                         node->data = data;
  119.                         node->next = last->next;
  120.                         last->next = node;

  121.                         return (list);
  122.                 }
  123.         }
  124. }

  125. SList*
  126. slistConcat(SList *list1, SList *list2)
  127. {
  128.         if (list2) {
  129.                 if (list1)
  130.                         slistLast(list1)->next = list2;
  131.                 else
  132.                         list1 = list2;
  133.         }

  134.         return (list1);
  135. }

  136. SList*
  137. slistRemove(SList *list, mypointer data)
  138. {
  139.         SList *tmp, *prev = NULL;

  140.         tmp = list;
  141.         while (tmp) {
  142.                 if (tmp->data == data) {
  143.                         if (prev)
  144.                                 prev->next = tmp->next;
  145.                         else
  146.                                 list = tmp->next;

  147.                         myFree(tmp);
  148.                         break;
  149.                 }
  150.                 prev = tmp;
  151.                 tmp = tmp->next;
  152.         }

  153.         return (list);
  154. }

  155. SList*
  156. slistRemoveAll(SList *list, mypointer data)
  157. {
  158.         SList *tmp, *prev = NULL;

  159.         tmp = list;
  160.         while (tmp) {
  161.                 if (tmp->data == data) {
  162.                         SList *next = tmp->next;

  163.                         if (prev)
  164.                                 prev->next = next;
  165.                         else
  166.                                 list = next;

  167.                         myFree(tmp);
  168.                         tmp = next;
  169.                 } else {
  170.                         prev = tmp;
  171.                         tmp = prev->next;
  172.                 }
  173.         }

  174.         return (list);
  175. }

  176. SList*
  177. slistRemoveLink(SList *list, SList *link)
  178. {
  179.         SList *tmp, *prev = NULL;

  180.         tmp = list;
  181.         while (tmp) {
  182.                 if (tmp == link) {
  183.                         if (prev)
  184.                                 prev->next = tmp->next;
  185.                         if (list == tmp)
  186.                                 list = list->next;

  187.                         tmp->next = NULL;
  188.                         break;
  189.                 }

  190.                 prev = tmp;
  191.                 tmp = tmp->next;
  192.         }

  193.         return (list);
  194. }

  195. SList*
  196. slistDeleteLink(SList *list, SList *link)
  197. {
  198.         SList *tmp, *prev = NULL;

  199.         tmp = list;
  200.         while (tmp) {
  201.                 if (tmp == link) {
  202.                         if (prev)
  203.                                 prev->next = tmp->next;
  204.                         if (list == tmp)
  205.                                 list = list->next;

  206.                         tmp->next = NULL;
  207.                         myFree(tmp);
  208.                         break;
  209.                 }

  210.                 prev = tmp;
  211.                 tmp = tmp->next;
  212.         }

  213.         return (list);
  214. }

  215. SList*
  216. slistCopy(SList *list)
  217. {
  218.         SList *new_list = NULL;

  219.         if (list) {
  220.                 SList *last;

  221.                 new_list = myAlloc();
  222.                 new_list->data = list->data;
  223.                 last = new_list;
  224.                 list = list->next;

  225.                 while (list) {
  226.                         last->next = myAlloc();
  227.                         last = last->next;
  228.                         last->data = list->data;
  229.                         list = list->next;
  230.                 }

  231.                 last->next = NULL;
  232.         }

  233.         return (new_list);
  234. }

  235. SList*
  236. slistReverse(SList *list)
  237. {
  238.         SList *prev = NULL;

  239.         while (list) {
  240.                 SList *next = list->next;
  241.                 list->next = prev;
  242.                 prev = list;
  243.                 list = next;
  244.         }

  245.         return (prev);
  246. }

  247. SList*
  248. slistNTH(SList *list, unsigned int n)
  249. {
  250.         while (n-- > 0 && list)
  251.                 list = list->next;

  252.         return (list);
  253. }

  254. mypointer
  255. slistNTHdata(SList *list, unsigned int n)
  256. {
  257.         while (n-- > 0 && list)
  258.                 list = list->next;

  259.         return list ? list->data : NULL;
  260. }

  261. SList*
  262. slistFind(SList *list, mypointer data)
  263. {
  264.         while (list) {
  265.                 if (list->data == data)
  266.                         break;
  267.                 list = list->next;
  268.         }

  269.         return (list);
  270. }

  271. SList*
  272. slistFindCustom(SList *list, mypointer data, myCompareFunc func)
  273. {
  274.         return_val_if_fail(func != NULL, list);

  275.         while (list) {
  276.                 if (! func(list->data, data))
  277.                         return (list);
  278.                 list = list->next;
  279.         }

  280.         return (NULL);
  281. }

  282. int
  283. slistPosition(SList *list, SList *link)
  284. {
  285.         int i;

  286.         i = 0;
  287.         while (list) {
  288.                 if (list == link)
  289.                         return i;
  290.                 i++;
  291.                 list = list->next;
  292.         }

  293.         return (-1);
  294. }

  295. int
  296. slistIndex(SList *list, mypointer data)
  297. {
  298.         int i;

  299.         i = 0;
  300.         while (list) {
  301.                 if (list->data == data)
  302.                         return i;
  303.                 i++;
  304.                 list = list->next;
  305.         }

  306.         return (-1);
  307. }

  308. int
  309. slistLength(SList *list)
  310. {
  311.         int len;

  312.         len = 0;
  313.         while (list) {
  314.                 len++;
  315.                 list = list->next;
  316.         }

  317.         return (len);
  318. }

  319. void
  320. slistForeach(SList *list, myfunc func, mypointer user_data)
  321. {
  322.         while (list) {
  323.                 SList *next = list->next;
  324.                 (*func) (list->data, user_data);
  325.                 list = next;
  326.         }
  327. }

  328. void
  329. print(int *a, int *b)
  330. {
  331.         printf("%d,", *a);
  332. }

  333. void
  334. myFreeAll(SList *list)
  335. {
  336.         SList *next;
  337.         while (list) {
  338.                 next = list->next;
  339.                 myFree(list);
  340.                 list = next;
  341.         }
  342. }

  343. int
  344. main(void)
  345. {
  346.         int i;
  347.         int key = 100;
  348.         int a[5] = {1,3,5,7,9};
  349.         int b[5] = {2,4,6,8,0};
  350.         SList *psl = NULL;
  351.         SList *psl2 = NULL;
  352.         SList *psl3 = NULL;
  353.         SList *psl4 = NULL;
  354.         SList *tmp = NULL;

  355.         printf("a: 1,3,5,7,9\n");
  356.         printf("b: 2,4,6,8,0\n");

  357.         for (i = 0; i < 5; i++)
  358.                 psl = slistAppend(psl, &a[i]);

  359.         printf("Append: a\n");
  360.         slistForeach(psl, (myfunc)print, NULL);
  361.         printf("\n");

  362.         for (i = 0; i < 5; i++)
  363.                 psl2 = slistPrepend(psl2, &b[i]);

  364.         printf("Prepend: b\n");
  365.         slistForeach(psl2, (myfunc)print, NULL);
  366.         printf("\n");

  367.         psl3 = slistConcat(psl, psl2);

  368.         printf("Concat a and b:\n");
  369.         slistForeach(psl3, (myfunc)print, NULL);
  370.         printf("\n");

  371.         psl3 = slistInsert(psl3, &key, 3);

  372.         printf("Insert %d at 3:\n", key);
  373.         slistForeach(psl3, (myfunc)print, NULL);
  374.         printf("\n");

  375.         tmp = psl3->next;
  376.         psl3 = slistInsertBefore(psl3, tmp, &key);

  377.         printf("Insert %d before %d(index):\n", key, 1);
  378.         slistForeach(psl3, (myfunc)print, NULL);
  379.         printf("\n");

  380.         psl3 = slistRemove(psl3, &b[4]);

  381.         printf("Remove %d:\n", b[4]);
  382.         slistForeach(psl3, (myfunc)print, NULL);
  383.         printf("\n");

  384.         psl3 = slistRemoveAll(psl3, &key);

  385.         printf("Remove all %d:\n", key);
  386.         slistForeach(psl3, (myfunc)print, NULL);
  387.         printf("\n");

  388.         psl4 = slistCopy(psl3);
  389.         printf("Copy all :\n", key);
  390.         slistForeach(psl4, (myfunc)print, NULL);
  391.         printf("\n");

  392.         psl4 = slistReverse(psl4);
  393.         printf("Reverse:\n", key);
  394.         slistForeach(psl4, (myfunc)print, NULL);
  395.         printf("\n");

  396.         printf("NTHdata: %d(index) is %d\n", 2, *(int *)slistNTHdata(psl4, 2));

  397.         printf("Index: %d is %d(index)\n", key, slistIndex(psl4, &key));

  398.         printf("Length: %d\n", slistLength(psl4));

  399.         myFreeAll(psl);
  400.         myFreeAll(psl2);
  401.         myFreeAll(psl3);
  402.         myFreeAll(psl4);
  403.         return (0);
  404. }
Result:
  1. a: 1,3,5,7,9
  2. b: 2,4,6,8,0
  3. Append: a
  4. 1,3,5,7,9,
  5. Prepend: b
  6. 0,8,6,4,2,
  7. Concat a and b:
  8. 1,3,5,7,9,0,8,6,4,2,
  9. Insert 100 at 3:
  10. 1,3,5,100,7,9,0,8,6,4,2,
  11. Insert 100 before 1(index):
  12. 1,100,3,5,100,7,9,0,8,6,4,2,
  13. Remove 0:
  14. 1,100,3,5,100,7,9,8,6,4,2,
  15. Remove all 100:
  16. 1,3,5,7,9,8,6,4,2,
  17. Copy all :
  18. 1,3,5,7,9,8,6,4,2,
  19. Reverse:
  20. 2,4,6,8,9,7,5,3,1,
  21. NTHdata: 2(index) is 6
  22. Index: 100 is -1(index)
  23. Length: 9


阅读(1433) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~