2015-02-08 291 views
0

我的一般目標是能夠輸入字符串並將其添加到列表中。我的主要問題是makenewnode。我非常有信心,主要和我的結構是堅實的,我對搜索的基本代碼有點自信,但具體看起來不太好。我的問題基本上是,main中的print語句有什麼問題,在make冗餘中使用makenewnode兩次,而makenewnode實際上是如何工作的。鏈接列表和輸入

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 

WC(字數)應該持有WRD單詞列表,在計數的單詞和未來的數是指通過列表來推進。

struct wc { 
    struct wc* next; 
    char* wrd; 
    int count; 
}; 

頭,就是要在列表

struct wc* head=NULL; 

makenewnode是相當不言自明的開始。它需要的char * S,分配內存,增加了計數(應該是在列表中的單詞數),並增加了字WRD(應該是單詞的列表)

void makenewnode(char* s){ 
    struct wc* newnode; 
    char* newword; 

    newword=malloc(strlen(s)+1); 
    strcpy(newword,s); 
    newnode=malloc(sizeof(struct wc)); 
    newnode->wrd=newword; 
    newnode->count=1; 
    newnode->next=head; 
    head=newnode; 
} 

搜索應該取出輸入字符串並確定它是否已經在列表中。 while循環應該運行直到輸入字符串結束。它將wrd(已添加到列表中的單詞)與輸入進行比較,並且如果輸入已經在wrd中,它將添加到計數並將找到的值設置爲1(就像一個符號,1實際上並不代表什麼意思)。如果輸入不在wrd中,它將使用makenewnode爲輸入創建一個新節點。我覺得我的陳述很重要,第二個陳述是多餘的,但我不確定。

void search(char* linebuf){ 
    struct wc* node; 
    int found=0; 

    found=0; 
    node=head; 
    while(node!=NULL){ 
     if(strcmp(node->wrd, linebuf)==0){ 
      node->count++; 
      found=1; 
      break; 
     } 
     else{ 
      makenewnode(linebuf); 

     } 

    if(found==0){ 
     makenewnode(linebuf); 
    } 
    } 
} 

主要應該接受輸入字符串(100個字符),只是通過搜索運行它們(這通過makenewnode運行)。然後它應該打印單詞數(count)和單詞列表(wrd)k。

int main(int argc, char* argv[]){ 
    struct wc* node; 
    char linebuf[100]; 

    printf("Enter Words: "); 
    while(fgets(linebuf,100,stdin)!=0){ 
     printf("Input line: %s", linebuf); 
     printf("Enter Words: "); 
     search(linebuf); 
    } 
    /* 
    I'm pretty iffy on these print statements but the rest of main is fine (I think) 
    printf("%d", count); 
    printf("%s", wrd); 
    */ 
    return 0; 
} 
+4

你真的有問題嗎? – 2015-02-08 03:30:40

+0

如果它有一個實際的**問題,將是一個很好的帖子** – 2015-02-08 03:31:47

+0

對於iffy部分,使用'%s'而不是'%c'作爲後者期望的單個字符 – 2015-02-08 03:33:32

回答

0

搜索應該是:

void search(char* linebuf){ 
    struct wc* node; 
    int found; 

    node=head; 
    found=0; 
    while(node!=NULL){ 
     if(strcmp(node->wrd, linebuf)==0){ 
      node->count++; 
      found=1; 
      break; 
     }else{ 
     node = node->next; 
     } 
    } 
    if(found==0){ 
     makenewnode(linebuf); 
    } 
} 

主要應該是:

int main(int argc, char* argv[]){ 
    struct wc* node; 
    char linebuf[100]; 
    char* inp; 

    printf("Enter Words: "); 

    while((inp=fgets(linebuf,100,stdin))!=0){ 
     printf("Input line: %s", inp); 
     search(inp); 
    } 
    node=head; 
    while(node!=NULL){ 
     printf("\nWords: %s\nCount: %d\n", node->wrd,node->count); 
     node=node->next; 
    } 
    return 0; 
} 

代碼的其他部分都很好。

1

變化

void search(char* linebuf){ 
    struct wc* node; 
    int found=0; 

    //found=0; 
    node=head; 
    while(node!=NULL){ 
     if(strcmp(node->wrd, linebuf)==0){ 
      node->count++; 
      found=1; 
      break;//or return ; 
     } 
     node = node->next; 
    } 
    if(found==0){ 
     makenewnode(linebuf); 
    } 
}