list.zip
- /* huangmingwei code*/
- #include <stdio.h>
- #include <string.h>
- #include <memory.h>
- #include <stdlib.h>
- #define ERROR -1
- #define OK 0
- typedef struct MyListNode_s
- {
- struct MyListNode_s *next;
- char *pWord;
- int num;
- }MyListNode_t;
- typedef struct MyList_s
- {
- MyListNode_t head;
- }MyList_t;
- #define LIST_INIT(pList) \
- do { \
- (pList)->head.next = NULL;\
- (pList)->head.num = 0;\
- (pList)->head.pWord = NULL;\
- }while (0)
- char g_arry[2048][20];
- int CountNumber;
-
- /* 加到头部 */
- void ListAdd(MyListNode_t *pNewNode, MyList_t *pList)
- {
- if (NULL == pNewNode || NULL == pList)
- {
- return;
- }
- pNewNode->next = pList->head.next;
- pList->head.next = pNewNode;
- }
- int AddWordToList(char *pWord, MyList_t *pList)
- {
- MyListNode_t *pNode;
- if (NULL == pWord || NULL == pList)
- {
- return ERROR;
- }
- /*已存在,加计数*/
- pNode = pList->head.next;
- while (NULL != pNode)
- {
- if (0 == strcmp(pNode->pWord, pWord))
- {
- pNode->num++;
- return OK;
- }
- pNode = pNode->next;
- }
- /* 不存在,重新申请 */
- pNode = (MyListNode_t *)malloc(sizeof(MyListNode_t));
- if (NULL == pNode)
- {
- return ERROR;
- }
- memset(pNode, 0, sizeof(MyListNode_t));
- pNode->pWord = (char *)malloc(strlen(pWord) + 1);
- if (NULL == pNode->pWord)
- {
- free(pNode);
- return ERROR;
- }
- memcpy(pNode->pWord, pWord, strlen(pWord));
- pNode->pWord[strlen(pWord)] = 0;
- pNode->num = 1;
- ListAdd(pNode, pList);
- printf("add %s\n", pWord);
-
- return OK;
- }
- void DestroyList(MyList_t *pList)
- {
- MyListNode_t *pNode = NULL;
- MyListNode_t *pNextNode = NULL;
-
- if (NULL == pList)
- {
- return ;
- }
- pNode = pList->head.next;
- pList->head.next = NULL;
- while (NULL != pNode)
- {
- pNextNode = pNode->next;
- free(pNode->pWord);
- free(pNode);
- pNode = pNextNode;
- }
- }
- int GetMinNumFromList(MyList_t *pList)
- {
- MyListNode_t *pNode = NULL;
- int MinNum = 65535;
- if (NULL == pList)
- {
- return MinNum;
- }
-
- pNode = pList->head.next;
- while (NULL != pNode)
- {
- if (MinNum > pNode->num)
- {
- MinNum = pNode->num;
- }
- pNode = pNode->next;
- }
- return MinNum;
- }
- int NeedDeleteOrNot(int MinNum, char *pWord, MyList_t *pList)
- {
- MyListNode_t *pNode;
- if (NULL == pWord || NULL == pList)
- {
- return ERROR;
- }
- pNode = pList->head.next;
- while (NULL != pNode)
- {
- if (0 == memcmp(pNode->pWord, pWord, strlen(pWord)) && pNode->num == MinNum)
- {
- return OK;
- }
- pNode = pNode->next;
- }
- return ERROR;
- }
- int OpGetFilterString(int MinNum, MyList_t *pList, char *pInputString, char *pOutputString)
- {
- int i;
-
- if (MinNum == 0 || MinNum == ~0 || NULL == pOutputString || NULL == pList)
- {
- return ERROR;
- }
- i = 0;
- for (i = 0; i <= CountNumber; i++)
- {
- if (OK != NeedDeleteOrNot(MinNum, g_arry[i], pList))
- {
- strcat(pOutputString, g_arry[i]);
- if (i < CountNumber)
- {
- strcat(pOutputString, " ");
- }
- }
- }
- return OK;
- }
- int GetFilterString(char *pInputString, char **ppOutputString)
- {
- int length = 0;
- MyList_t MyList;
- char *pStrPtr = NULL;
- int MinNum;
- int i, j;
-
- if (NULL == pInputString || NULL == ppOutputString)
- {
- return ERROR;
- }
- length = strlen(pInputString);
- *ppOutputString = (char *)malloc(length + 1);
- if (NULL == *ppOutputString)
- {
- return ERROR;
- }
- LIST_INIT(&MyList);
- i = 0;
-
- pStrPtr = strtok(pInputString, " ");
-
- while (pStrPtr)
- {
- sprintf(g_arry[i++], "%s", pStrPtr);
- pStrPtr = strtok(NULL, " ");
- //AddWordToList(buffer, &MyList);
- }
- for (j = 0; j < i; j++)
- {
- AddWordToList(g_arry[j], &MyList);
- }
-
- CountNumber = i - 1;
-
- MinNum = GetMinNumFromList(&MyList);
- printf("MinNum: %d\n", MinNum);
- OpGetFilterString(MinNum, &MyList, pInputString, *ppOutputString);
- DestroyList(&MyList);
- return OK;
- }
- int main()
- {
- char InputString[2048];
- char *pResultString = NULL;
-
- CountNumber = 0;
-
- printf("Please input a sentence:\n");
- gets(InputString);
- GetFilterString(InputString, &pResultString);
- printf("Result:\n%s\n", pResultString);
- free(pResultString);
- return 0;
- }
23:00开始写,凌晨12点多了,写完了,调了调,测了一下ok了。哎,可惜昨晚公司办公电脑的编译环境不行,公司这个搞得测试俺是得鸭蛋了。。。 苍天啊,大地啊!!
阅读(2299) | 评论(0) | 转发(0) |