Chinaunix首页 | 论坛 | 博客
  • 博客访问: 436873
  • 博文数量: 89
  • 博客积分: 2713
  • 博客等级: 少校
  • 技术积分: 938
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-18 21:19
个人简介

为了成为自由自在的人而奋斗!

文章分类

全部博文(89)

文章存档

2016年(5)

2015年(9)

2014年(2)

2013年(10)

2012年(1)

2011年(30)

2010年(32)

分类: C/C++

2012-04-27 01:06:16

 list.zip   

 

 


点击(此处)折叠或打开

  1. /* huangmingwei code*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <memory.h>
  5. #include <stdlib.h>

  6. #define ERROR -1
  7. #define OK 0


  8. typedef struct MyListNode_s
  9. {
  10.     struct MyListNode_s *next;
  11.     char *pWord;
  12.     int num;
  13. }MyListNode_t;

  14. typedef struct MyList_s
  15. {
  16.     MyListNode_t head;
  17. }MyList_t;

  18. #define LIST_INIT(pList) \
  19.     do { \
  20.         (pList)->head.next = NULL;\
  21.         (pList)->head.num = 0;\
  22.         (pList)->head.pWord = NULL;\
  23.     }while (0)

  24. char g_arry[2048][20];
  25. int CountNumber;

  26.     
  27. /* 加到头部 */
  28. void ListAdd(MyListNode_t *pNewNode, MyList_t *pList)
  29. {
  30.     if (NULL == pNewNode || NULL == pList)
  31.     {
  32.         return;
  33.     }

  34.     pNewNode->next = pList->head.next;
  35.     pList->head.next = pNewNode;
  36. }

  37. int AddWordToList(char *pWord, MyList_t *pList)
  38. {
  39.     MyListNode_t *pNode;

  40.     if (NULL == pWord || NULL == pList)
  41.     {
  42.         return ERROR;
  43.     }

  44.     /*已存在,加计数*/
  45.     pNode = pList->head.next;
  46.     while (NULL != pNode)
  47.     {
  48.         if (0 == strcmp(pNode->pWord, pWord))
  49.         {
  50.             pNode->num++;
  51.             return OK;
  52.         }
  53.         pNode = pNode->next;
  54.     }

  55.     /* 不存在,重新申请 */
  56.     pNode = (MyListNode_t *)malloc(sizeof(MyListNode_t));
  57.     if (NULL == pNode)
  58.     {
  59.         return ERROR;
  60.     }

  61.     memset(pNode, 0, sizeof(MyListNode_t));

  62.     pNode->pWord = (char *)malloc(strlen(pWord) + 1);
  63.     if (NULL == pNode->pWord)
  64.     {
  65.         free(pNode);
  66.         return ERROR;
  67.     }

  68.     memcpy(pNode->pWord, pWord, strlen(pWord));
  69.     pNode->pWord[strlen(pWord)] = 0;
  70.     pNode->num = 1;

  71.     ListAdd(pNode, pList);

  72.     printf("add %s\n", pWord);
  73.     
  74.     return OK;
  75. }

  76. void DestroyList(MyList_t *pList)
  77. {
  78.     MyListNode_t *pNode = NULL;
  79.     MyListNode_t *pNextNode = NULL;
  80.     
  81.     if (NULL == pList)
  82.     {
  83.         return ;
  84.     }

  85.     pNode = pList->head.next;
  86.     pList->head.next = NULL;
  87.     while (NULL != pNode)
  88.     {
  89.         pNextNode = pNode->next;

  90.         free(pNode->pWord);
  91.         free(pNode);

  92.         pNode = pNextNode;
  93.     }
  94. }


  95. int GetMinNumFromList(MyList_t *pList)
  96. {
  97.     MyListNode_t *pNode = NULL;
  98.     int MinNum = 65535;

  99.     if (NULL == pList)
  100.     {
  101.         return MinNum;
  102.     }
  103.     
  104.     pNode = pList->head.next;
  105.     while (NULL != pNode)
  106.     {
  107.         if (MinNum > pNode->num)
  108.         {
  109.             MinNum = pNode->num;
  110.         }
  111.         pNode = pNode->next;
  112.     }

  113.     return MinNum;
  114. }

  115. int NeedDeleteOrNot(int MinNum, char *pWord, MyList_t *pList)
  116. {
  117.     MyListNode_t *pNode;

  118.     if (NULL == pWord || NULL == pList)
  119.     {
  120.         return ERROR;
  121.     }

  122.     pNode = pList->head.next;
  123.     while (NULL != pNode)
  124.     {
  125.         if (0 == memcmp(pNode->pWord, pWord, strlen(pWord)) && pNode->num == MinNum)
  126.         {
  127.             return OK;
  128.         }
  129.         pNode = pNode->next;
  130.     }

  131.     return ERROR;
  132. }


  133. int OpGetFilterString(int MinNum, MyList_t *pList, char *pInputString, char *pOutputString)
  134. {
  135.     int i;
  136.     
  137.     if (MinNum == 0 || MinNum == ~0 || NULL == pOutputString || NULL == pList)
  138.     {
  139.         return ERROR;
  140.     }

  141.     i = 0;
  142.     for (i = 0; i <= CountNumber; i++)
  143.     {
  144.         if (OK != NeedDeleteOrNot(MinNum, g_arry[i], pList))
  145.         {
  146.             strcat(pOutputString, g_arry[i]);
  147.             if (i < CountNumber)
  148.             {
  149.                 strcat(pOutputString, " ");
  150.             }
  151.         }
  152.     }

  153.     return OK;
  154. }

  155. int GetFilterString(char *pInputString, char **ppOutputString)
  156. {
  157.     int length = 0;
  158.     MyList_t MyList;
  159.     char *pStrPtr = NULL;
  160.     int MinNum;
  161.     int i, j;
  162.     
  163.     if (NULL == pInputString || NULL == ppOutputString)
  164.     {
  165.         return ERROR;
  166.     }

  167.     length = strlen(pInputString);

  168.     *ppOutputString = (char *)malloc(length + 1);
  169.     if (NULL == *ppOutputString)
  170.     {
  171.         return ERROR;
  172.     }

  173.     LIST_INIT(&MyList);

  174.     i = 0;
  175.     
  176.     pStrPtr = strtok(pInputString, " ");
  177.     
  178.     while (pStrPtr)
  179.     {
  180.         sprintf(g_arry[i++], "%s", pStrPtr);
  181.         pStrPtr = strtok(NULL, " ");
  182.         //AddWordToList(buffer, &MyList);
  183.     }

  184.     for (j = 0; j < i; j++)
  185.     {
  186.         AddWordToList(g_arry[j], &MyList);
  187.     }
  188.     
  189.     CountNumber = i - 1;
  190.     
  191.     MinNum = GetMinNumFromList(&MyList);
  192.     printf("MinNum: %d\n", MinNum);
  193.     OpGetFilterString(MinNum, &MyList, pInputString, *ppOutputString);
  194.     DestroyList(&MyList);

  195.     return OK;
  196. }

  197. int main()
  198. {
  199.     char InputString[2048];
  200.     char *pResultString = NULL;

  201.     
  202.     CountNumber = 0;
  203.     
  204.     printf("Please input a sentence:\n");
  205.     gets(InputString);

  206.     GetFilterString(InputString, &pResultString);

  207.     printf("Result:\n%s\n", pResultString);
  208.     free(pResultString);

  209.     return 0;
  210. }

23:00开始写,凌晨12点多了,写完了,调了调,测了一下ok了。哎,可惜昨晚公司办公电脑的编译环境不行,公司这个搞得测试俺是得鸭蛋了。。。   苍天啊,大地啊!!
阅读(2270) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~