Table Lookup
做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的更多相关文章
- 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 ...
- Page (computer memory) Memory segmentation Page table 虚拟地址到物理地址的转换
A page, memory page, or virtual page is a fixed-length contiguous block of virtual memory, described ...
- [MySQL Reference Manual]15. 其他存储引擎
15. 其他存储引擎 15. 其他存储引擎 15.1 设置存储引擎 15.2 MyISAM存储引擎 15.2.1 MyISAM启动选项 15.2.2 Key的空间要求 15.2.3 MyISAM表存储 ...
- Linux操作系统主机名(hostname)简介
http://www.jb51.net/LINUXjishu/10938.html 摘要:本文是关于Linux操作系统主机名(hostname)的文档,对主要配置文件/etc/hosts进行简要的说明 ...
- linux hosts文件详+mac主机名被莫名其妙修改
1.名词解析 主机名: 无论是在局域网还是在INTERNET上,每台主机都有一个IP地址,用来区分当前是那一台机器(其实底层是使用机器的物理地址),也就是说IP地址就是一个主机的门牌号,唯一的标示这一 ...
- BitHacks
备份文件时看到的.我以前居然下过这东西. 2016-12-4 12:05:52更新 纯文本格式真棒.假如使用word写的我能拷过来格式还不乱?? Markdown真好. Bit Hacks By Se ...
- Interpolation in MATLAB
Mathematics One-Dimensional Interpolation There are two kinds of one-dimensional interpolation i ...
- linux修改主机名称
http://blog.csdn.net/qq_20480611/article/details/51017033 ========================================== ...
- 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 ...
随机推荐
- XML的四种解析方式
本文描述了构建良好的XML需要遵循的规则.作者详细介绍了构建XML需要考虑的元素,如何命名约定.正确的标记嵌套.属性规则.声明和实体,以及DTD和schema的验证,十分便于新手开始学习了解XML. ...
- Android上传文件到服务器(转)
Android中实现上传文件,其实是很简单的,和在java里面是一样的,基本上都是熟悉操作输出流和输入流!还有一个特别重要的就是需要配置content-type的一些参数!如果这些都弄好了,上传就很简 ...
- 传感器 Sensor 加速度【示例】
简介 坐标系 x轴:从左到右 y轴:从下到上 z轴:从内到外 这个坐标系与Android 2D API中的不同,传感器中的返回值都以此坐标系为准. SENSOR_TYPE_ACCELEROMETER ...
- noip 2010 关押罪犯 (二分图染色 并茶几)
/* 二分图染色版本 两个监狱对应二部图的两部分 在给定的怨气值里二分 对于每一个Ci 进行染色判断是否合法 染色的时候 如果这条边的ci > Ci 这两个人就带分开 即染成不同的颜色 如果染色 ...
- 弹出层easydialog-v2.0
地址:http://www.lanrentuku.com/down/js/qita-862/ easydialog.css ;;; } .easyDialog_wrapper{ width:320px ...
- 七.生成n位随机字符串
--1.借助newid() go --创建视图(因为在函数中无法直接使用newid()) create view vnewid as select newid() N'MacoId'; go --创建 ...
- 一个tomcat部署俩个java web项目
2.发布的时候可以发布成war包,用项目名称右键export,选择项目名称,还有发布的路径,即tomcat下的路径,参考http://zhidao.baidu.com/link?url=imOu0Uu ...
- for update和for update nowait的区别和使用
首先,for update 和for update nowait 是对操作的数据行进行加锁,在事务提交前防止其他操作对数据的修改. for update 和for update nowait主要区别在 ...
- SqlServer CTE 递归查询 Oracle递归查询
在做数据库设计这块,很多时候表的数据模型就是典型的二叉树结构. 于是在查询数据的时候,就涉及到了数据的递归查询. 递归查询分为两种:1.从根节点查询自身以及所有的子节点:2.从子节点查询自身以及所有的 ...
- Navicat 选择语句
1.进入数据库后,点击Query 2.点击new query 3.左边提供界面的筛选条件,如果不清楚sql语句,可直接在上面操作 4.右边可自己编写sql语句 5.写完语句后,点击Run,在resul ...