Search
 
SCRIPT & CODE EXAMPLE
 

C

c language dictionary implemet

struct nlist { /* table entry: */
    struct nlist *next; /* next entry in chain */
    char *name; /* defined name */
    char *defn; /* replacement text */
};

#define HASHSIZE 101
static struct nlist *hashtab[HASHSIZE]; /* pointer table */

/* hash: form hash value for string s */
unsigned hash(char *s)
{
    unsigned hashval;
    for (hashval = 0; *s != ''; s++)
      hashval = *s + 31 * hashval;
    return hashval % HASHSIZE;
}

/* lookup: look for s in hashtab */
struct nlist *lookup(char *s)
{
    struct nlist *np;
    for (np = hashtab[hash(s)]; np != NULL; np = np->next)
        if (strcmp(s, np->name) == 0)
          return np; /* found */
    return NULL; /* not found */
}

char *strdup(char *);
/* install: put (name, defn) in hashtab */
struct nlist *install(char *name, char *defn)
{
    struct nlist *np;
    unsigned hashval;
    if ((np = lookup(name)) == NULL) { /* not found */
        np = (struct nlist *) malloc(sizeof(*np));
        if (np == NULL || (np->name = strdup(name)) == NULL)
          return NULL;
        hashval = hash(name);
        np->next = hashtab[hashval];
        hashtab[hashval] = np;
    } else /* already there */
        free((void *) np->defn); /*free previous defn */
    if ((np->defn = strdup(defn)) == NULL)
       return NULL;
    return np;
}

char *strdup(char *s) /* make a duplicate of s */
{
    char *p;
    p = (char *) malloc(strlen(s)+1); /* +1 for ’’ */
    if (p != NULL)
       strcpy(p, s);
    return p;
}
Comment

PREVIOUS NEXT
Code Example
C :: unia c 
C :: under 
C :: sscanf and sprintf in c 
C :: taking input and converting it to a string in c 
C :: ::template 
C :: how to devowel string in c program 
C :: os.listdir to array 
C :: Wait until an animation finishes - Selenium, Java 
C :: lazer codechef 
C :: user define 
C :: q2. wap in c to input 5 numbers in an array and display in reverse order. 
C :: passing an array of unspecified number of variables to a function 
C :: come fare un programma in c con cui interagire 
C :: c declare float 
C :: c if statement 
C :: c while loop 
C :: get configuration script window 7 
Dart :: TextStyle underline flutter 
Dart :: flutter format currency fcfa 
Dart :: flutter positioned center horizontally 
Dart :: flutter snackbar shape 
Dart :: how to stop screen rotation in flutter 
Dart :: flutter absorbpointer 
Dart :: how to change legend colour in SfCircularChart in flutter 
Dart :: dart string interpolation 
Dart :: flutter animated opacity 
Dart :: alertdialog flutter press outside to disappera 
Dart :: flutter text 
Dart :: how to put the Pi in dart 
Dart :: what is final and const verabile in flutter 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =