给链表添加东西再打印出来,好像是这个意思

filipesi 2010-04-26 10:01:30
我有一个用装单词的wordpool,可以向里面装单词(区分大小写)。
如果里面已经有这个词了,就增加计数器数值,没有就把词加进去
如果要加的词太长了,就忽略超过长度的部分。
如果wordpool满了,就静默返回,不做修改。
想写add和print函数。
print的格式是,单词加次数。

apple 1
banana 2
orange 1


#define MAX_WORDS 200
#define MAX_SIZE 10

typedef struct _wordPool {
int counter;
char word[MAX_SIZE];
} wordPool;

static WordPool wordTable[MAX_WORDS];

void add(const char *wp){
}

void print() {
}

int main (void) {
}
...全文
92 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
selooloo 2010-04-26
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <string.h>
#define MAX_WORDS 200
#define MAX_SIZE 10

typedef 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;
}
  • 打赏
  • 举报
回复
用STL吧,map搞定、
filipesi 2010-04-26
  • 打赏
  • 举报
回复
还是数组?
buptzwp 2010-04-26
  • 打赏
  • 举报
回复
这个是链表嘛?

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧