2014-04-23 6 views
0

알파벳순으로 단어를 추가 할 addWord 부분을 반환 할 수 있습니까? 시도했지만 작동하지 않았습니다. C 프로그래밍에서 매우 새로 운 것입니다. 사실, 일주일이 지난 지금부터 작업 중입니다. 그러나 나는 문제가 될 수 없다.C에서 알파벳순으로 노드 추가

typedef struct NODE { 
char *str;    
int count;    
struct NODE *pNext;  
} NODE; 
void addWord(char *word) 
    { 
     NODE *pCounter = NULL; 
     NODE *pLast = NULL; 

     if(pStart == NULL) // pstart is globally defined pStart=NULL; 
     { 
     pStart = createWordCounter(word); 
     return; 
     } 

     /* If the word is in the list, increment its count */ 
     pCounter = pStart; 
     while(pCounter != NULL) 
     { 
     if(strcmp(word, pCounter->str) == 0) 
     { 
      ++pCounter->count; 

      return; 
     } 
     pLast = pCounter;    
     pCounter = pCounter->pNext; 
     } 

     /* Word is not in the list, add it */ 
     pLast->pNext = createWord(word); 
    } 

    NODE* createWord(char *word) 
    { 
     NODE *pCounter = NULL; 
     pCounter = (NODE*)malloc(sizeof(NODE)); 
     pCounter->str = (char*)malloc(strlen(word)+1); 
     strcpy(pCounter->str, word); 
     pCounter->count = 1; 
     pCounter->pNext = NULL; 
     return pCounter; 
    } 

나는이 부분을 시도하지만 result.Here

void addWord(char *word) 
{ 
    NODE *pCounter = NULL; 
    NODE *pLast = NULL; 
    NODE *pNew = NULL; 

    if(pStart == NULL) 
    { 
    pStart = createWordCounter(word); 
    return; 
    } 

    /* If the word is in the list, increment its count */ 
    pCounter = pStart; 
    while(pCounter != NULL) 
    { 
    if(strcmp(word, pCounter->str) == 0) 
    { 
     ++pCounter->count; 

     return; 
    } 
    pLast = pCounter;    
    pCounter = pCounter->pNext; 
    } 
    while(pCounter != NULL){ 
       if(strcmp(word,pCounter->str)<0){ 
                pNew =createWordCounter(word); 
                pLast->pNext = pNew ; 
                pNew->pNext = pCounter; 
                pCounter = pLast->pNext; 
                } 
       else{ 
         pNew=createWordCounter(word); 
         pNew->pNext=pCounter; 
         pLast->pNext=pNew; 
         } 
         } 
+0

죄송합니다 우리는 당신을 위해 여기없는 시도 숙제. 특정 문제가 발생하면 도움을 드릴 수 있습니다. – rmi

+0

특정 문제는 addWord 부분입니다. 알파벳으로 반환 할 수 없습니다. psudocode를 도울 수 있다고 생각하십니까? – user3546593

+0

'작동하지 않음'이란 무엇을 의미합니까? 알파벳 순서로 추가하지만 알파벳 순서로 추가하지 않습니까? – rmi

답변

0

를 반환하지 않았습니다 업데이트 :

while(pCounter != NULL) 
{ 
    if(strcmp(word, pCounter->str) == 0) 
    { 
     ++pCounter->count; 
     return; 
    } 

    if(strcmp(word,pCounter->str)<0) 
    { 
     if (pCounter == pStart) 
     { 
      // TODO: insert at the begining 
      return; 
     } 
     else 
     { 
      pNew = createWordCounter(word); 
      pLast->pNext = pNew; 
      pNew->pNext = pCounter; 
      pCounter = pLast->pNext; 
      return; 
     } 
    } 

    pLast = pCounter;    
    pCounter = pCounter->pNext; 
} 

/* Word is not in the list, add it */ 
if (pLast == NULL) 
{ 
    // TODO: insert at the begining 
} 
else 
    pLast->pNext = createWord(word); 
+0

이미 완료했습니다 – user3546593

+0

루프 중에 입력되었지만 종료되지 않았습니다. – user3546593

+0

단어가 목록의 앞에 추가되면 pLast는 NULL입니다. 이 시나리오를 특별히 처리해야합니다. – rmi