做OJ的时候,做过类似的,即hash。算法很简单,关键是书上写的和做OJ,是完全不同的风格。有很多值得学习的地方。

  

/*
* Table Lookup
* 详见<<C程序设计语言>>(英文版*第二版) P143
*
* This code is typical of what might be found in the symbl table
* management routines of a macro processor or a compiler.
* For example, consider the #define statment. When a line like
* #define IN 1
* is encountered, the name IN and the replacement text 1 are stored in a table.
* Later, when the name IN appears in a statement like
* state IN;
* it must be replaced by 1.
*
* There are two routines that manipulate the names and replacement texts.
* install(s, t) records the name s and the replacement text t in a table;
* s and t are just character strings. lookup(s) searches for s in the table,
* and return a pointer to a place where it was found,
* or NULL if it wasn't there.
*
* The algorithm is a hash search-the incoming name is converted into a small non-negative integer,
* which is then used to index into an array of pointers.
* An array element points to the begining of a linked list of blocks describing names that have that hash value.
*
* It is NULL if no names have hashed to that value.
*/ #include <stdio.h>
#include <string.h>
#include <stdlib.h> #define HASHSIZE 101 static struct nlist *hashtab[HASHSIZE];// pointer table struct nlist { //table entry:
struct nlist *next; // next entry in chain
char *name; // defined name
char *defn; // replacement text
}; char *_strdup(char *s) { //make a duplicate of a
char *p; p = (char *)malloc(sizeof(strlen(s))+);
if (p != NULL)
  strcpy(p, s);
return p;
} unsigned hash(char *s) { //hash: form hash value for string s
unsigned hashval; for (hashval = ; *s != '\0'; s++)
   hashval = *s + * hashval;
return hashval % HASHSIZE;
} struct nlist *lookup(char *s) { //lookup: look for s in hashtab
struct nlist *np; for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  if (strcmp(s, np->name) == )
   return np;
return NULL;
} // 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
   free((void *) np->defn); //free previous defn
if((np->defn = _strdup(defn)) == NULL)
  return NULL;
return np;
} int main(int argc, char *argv[]) {
struct nlist *np; install("tanhe", "haha");
install("ni", "haoma");
np = lookup("tanhe");
printf("%s %s\n", np->name, np->defn);
np = lookup("ni");
printf("%s %s\n", np->name, np->defn); return ;
}

Table Lookup的更多相关文章

  1. 13.1.17 CREATE TABLE Syntax

    13.1.17 CREATE TABLE Syntax 13.1.17.1 CREATE TABLE ... LIKE Syntax 13.1.17.2 CREATE TABLE ... SELECT ...

  2. Page (computer memory) Memory segmentation Page table 虚拟地址到物理地址的转换

    A page, memory page, or virtual page is a fixed-length contiguous block of virtual memory, described ...

  3. [MySQL Reference Manual]15. 其他存储引擎

    15. 其他存储引擎 15. 其他存储引擎 15.1 设置存储引擎 15.2 MyISAM存储引擎 15.2.1 MyISAM启动选项 15.2.2 Key的空间要求 15.2.3 MyISAM表存储 ...

  4. Linux操作系统主机名(hostname)简介

    http://www.jb51.net/LINUXjishu/10938.html 摘要:本文是关于Linux操作系统主机名(hostname)的文档,对主要配置文件/etc/hosts进行简要的说明 ...

  5. linux hosts文件详+mac主机名被莫名其妙修改

    1.名词解析 主机名: 无论是在局域网还是在INTERNET上,每台主机都有一个IP地址,用来区分当前是那一台机器(其实底层是使用机器的物理地址),也就是说IP地址就是一个主机的门牌号,唯一的标示这一 ...

  6. BitHacks

    备份文件时看到的.我以前居然下过这东西. 2016-12-4 12:05:52更新 纯文本格式真棒.假如使用word写的我能拷过来格式还不乱?? Markdown真好. Bit Hacks By Se ...

  7. Interpolation in MATLAB

    Mathematics     One-Dimensional Interpolation There are two kinds of one-dimensional interpolation i ...

  8. linux修改主机名称

    http://blog.csdn.net/qq_20480611/article/details/51017033 ========================================== ...

  9. MCMC and Bayesian Data Analysis(PPT在文件模块)

    How to generate a sample from $p(x)$? Let's first see how Matlab samples from a $p(x)$. In Matlab, t ...

随机推荐

  1. Java使用Protocol Buffers入门四步骤

    Protocol Buffers(简称protobuf)是谷歌的一项技术.用于将结构化的数据序列化.反序列化.经经常使用于网络传输. 这货实际上相似于XML生成和解析.但protobuf的效率高于XM ...

  2. [转] prerender-SPA程序的SEO优化策略

    随着web2.0的兴起,ajax的时代已经成为了事实,更如今 Knockout,backbone, angular,ember前端MDV(model driver view)框架强势而来,Single ...

  3. jsp页面禁用缓存

    问题:为什么禁用JSP页面缓存 就是为了得到实时信息 怎样禁用JSP页面缓存 1.在JSP页面设置 <meta http-equiv="pragma" content=&qu ...

  4. Java基础知识强化之IO流笔记19:FileOutputStream的三个write方法

    1. FileOutputStream的三个write方法:  void write(byte[] buffer)           Writes the entire contents of th ...

  5. POJ 1470 Closest Common Ancestors(LCA&RMQ)

    题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...

  6. Python之路,Day26-----暂无正在更新中

    Python之路,Day26-----暂无正在更新中

  7. js中的同步与异步

    同步:提交后等待服务器的响应,接收服务器返回的数据后再执行下面的代码    异步:与上面相反,提交后继续执行下面的代码,而在后台继续监听,服务器响应后有程序做相应处理,异步的操作好处是不必等待服务器而 ...

  8. memcache的安装及管理

    一.Memcache概述 Memcache(内存,缓存):是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个巨大的hash表.(key=value)(是用C语言开发的,并且需要libeven ...

  9. utils object doesn,t exists中毒后,就删除了.JS文件后台就出现了前面的英文。请问怎么解决

    明显是你删了js丢失了函数了...从备份里还原一份.js出来放进去,前台也不能删,如果遇到名字冲突,修改一下名字即可

  10. 【转】 iOS开发UI篇—控制器的View的创建

    最近对view的周期等还不是非常清楚,就找到顶哥的文章,非常不错,就搬运过来了. 原文: http://www.cnblogs.com/wendingding/p/3770760.html 一.6种创 ...