2013年8月26日月曜日

開発環境

プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第6章(構造体)、6.6(テーブル参照)、演習6-5を解いてみる。

その他参考書籍

演習 6-5.

コード

sample.c

#include <string.h>

struct nlist {
    struct nlist *next;
    char *name;
    char *defn;
};

#define HASHSIZE 101

static struct nlist *hashtab[HASHSIZE];

void undef(char *s)
{
    int h;
    struct nlist *pre, *np;
    unsigned hash(char *);
    
    pre = NULL;
    h = hash(s);
    for (np = hashtab[h]; np != NULL; np = np->next) {
        if (strcmp(s, np->name) == 0) {
            break;
        }
        pre = np;
    }
    if (np != NULL) {
        if (pre == NULL) {
            hashtab[h] = np->next;
        } else {
            pre->next = np->next;
        }
        free((void *) np->name);
        free((void *) np->defn);
        free((void *) np);
    }
}

unsigned hash(char *s)
{
    unsigned hashval;
    
    for (hashval = 0; *s != '\0'; s++) {
        hashval = *s + 31 * hashval;
    }
    return hashval % HASHSIZE;
}

0 コメント:

コメントを投稿