厦大C语言上机 1382 小明的生词本

1382.小明的生词本


时间限制: 1000 MS          内存限制: 65536 K
        
提交数: 2 (0 users)          通过数: 0 (0 users)


问题描述
    小明知道自己的单词量远远不及他人,于是他想编程给自己建一个生词本,每次遇到新的单词的时候就加入到自己的生词本中。不过由于有时记性不好,本来已经存在生词本中的单词又被小明当成生词加入到了生词本中,对于这种情况,并不需要再给这个单词在分配一块内存来存放数据,也就是说,保证生词本中的单词之间的相异性。再加入一系列单词后,小明想知道自己的生词本中有多少个单词。


输入格式
第一行为一个正整数N,0 < N <= 1000,N代表小明加入的单词总数
接下来N行,每一行都是由小写字母组成的单词,单词长度不超过10


输出格式
输出生词本中的生词总数M


样例输入
9
the
quick
brown
fox
jumps
over
the
lazy
dog


样例输出
8


来源

xmu

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

struct Node
{
    char *word;
    struct Node *next;
};

int insert(struct Node *vocabulary, char *word)
{
    struct Node *p, *q, *r;
    int cmp;

    p = vocabulary->next;
    q = vocabulary;
    while (p)
    {
        cmp = strcmp(p->word, word);
        if (cmp == 0)
            return 0;
        else if (cmp < 0)
        {
            q = p;
            p = p->next;
        }
        else
        {
            r = (struct Node *)malloc(sizeof(struct Node));
            r->word = (char *)malloc(sizeof(char) * 15);
            strcpy(r->word, word);
            q->next = r;
            r->next = p;
            return 1;
        }
    }
    r = (struct Node *)malloc(sizeof(struct Node));
    r->word = (char *)malloc(sizeof(char) * 15);
    strcpy(r->word, word);
    q->next = r;
    r->next = p;
    return 1;
}

int main()
{
    int n, count = 0, i;
    char word[15] = { 0 };
    struct Node *vocabulary;

    vocabulary = (struct Node *)malloc(sizeof(struct Node));
    vocabulary->next = NULL;

    scanf("%d", &n);
    for (i = 0; i < n; ++i)
    {
        scanf("%s", word);
        count += insert(vocabulary, word);
    }
    printf("%d\n", count);

    return 0;
}


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值