给链表添加东西再打印出来,好像是这个意思
我有一个用装单词的wordpool,可以向里面装单词(区分大小写)。
如果里面已经有这个词了,就增加计数器数值,没有就把词加进去
如果要加的词太长了,就忽略超过长度的部分。
如果wordpool满了,就静默返回,不做修改。
想写add和print函数。
print的格式是,单词加次数。
- C/C++ code
apple 1banana 2orange 1
- C/C++ code
#define MAX_WORDS 200#define MAX_SIZE 10typedef struct _wordPool { int counter; char word[MAX_SIZE]; } wordPool;static WordPool wordTable[MAX_WORDS];void add(const char *wp){}void print() {}int main (void) {}[解决办法]
- C/C++ code
#include <stdio.h>#include <string.h>#define MAX_WORDS 200#define MAX_SIZE 10typedef struct _wordPool { int counter; char word[MAX_SIZE]; } WordPool;static WordPool wordTable[MAX_WORDS];static unsigned wordcount;//统计wordTable中单词数量int findword(const char *wt){ int i,find=0; for(i=0;i!=wordcount;++i) { if(!strcmp(wordTable[i].word,wt)) { ++find; break; } } if(find) return i; else return -1;}void add(const char *wp){ int pos; if(wordcount>=MAX_WORDS) { puts("wordTable is full"); return; } if((pos=findword(wp))>=0) ++wordTable[pos].counter; else { strcpy(wordTable[wordcount].word,wp); wordTable[wordcount].counter=1; ++wordcount; }}void print() { int i; for(i=0;i!=wordcount;++i) printf("%s : %d\n",wordTable[i].word,wordTable[i].counter);}int main (void) { add("hello"); add("world"); add("hello"); print(); getchar(); return 0;}