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 ...
随机推荐
- POJ 1113 Wall 求凸包的两种方法
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31199 Accepted: 10521 Descriptio ...
- verilog之task用法实例
该用法的代码源自夏宇闻老师的教材. 源代码: module traffic_lights; reg clock, red, amber, green; , off = , red_tics = , a ...
- 关于html水平垂直居中的一些总结吧
html水平垂直居中 最近遇到很多居中的问题,就花点时间总结了一下放在这里,以后找也方便,0.0~~ 1.居中文本 <div class="wrap"> 我在中间…… ...
- Effective java-枚举和注解思维导图
- datazen 备份还原
DataZen备份: DataZen还原 第一步:用管理员进入dos,输入 net stop datazen 第二步:切换到datazen Core Service的安装目录cd c:\Progr ...
- Mac OS X 10.9 Mavericks 修改root密码
Mac10.9忘记密码后有两种方式可以进去: 代码如下 复制代码 1.sudo passwd 重新输入密码即可,此方法修改了root的密码 代码如下 复制代码 2.sudo bash 输入当前用户 ...
- leetcode修炼之路——350. Intersection of Two Arrays II
题目如下: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2 ...
- 抓取锁的sql语句-第一次修改
CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); CUR_LOCK SYS_REFCURSOR; TYPE TP_LOCK ...
- Hibernate 环境搭建
Hibernate 工作流程 1.创建工程并导包 2.在src根目录下创建配置文件:hibernate.cfg.xml(也可以创建在src其他文件夹下,但是在后面的配置中,需要指明路径) <?x ...
- 巧用Session Manager还原Firefox丢失会话
今天Firefox Crash之后,我的会话全部丢失了.按照以往来说,Firefox在重新启动之后或者Crash之后会有一个会话还原的页面.但今天确实没有.后来我进行Google查阅,试了很多种办法. ...