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 ...
随机推荐
- Linux下tomcat使用
http://tomcat.apache.org/download-70.cgi这里下载 放到Linux目录下,解压开, 默认port:8080能够直接使用 经常使用启动命令catalina.sh ...
- PHP开发安全之近墨者浅谈(转)
==过滤输入/输出转义 过滤是Web应用安全的基础.它是你验证数据合法性的过程.通过在输入时确认对所有的数据进行过滤,你可以避免被污染(未过滤)数据在你的程序中被误信及误用.大多数流行的PHP应用的漏 ...
- 使用Tcl脚本分配FPGA管脚
自己主动生成Tcl文件 Project -> Generate Tcl File for Project... 弹出例如以下对话框.设置脚本路径. 编辑引脚 使用set_location_ass ...
- Git和Github的应用与命令方法总结
title: Git和Github的应用与命令方法总结 date: 2016-07-11 14:03:09 tags: git/github [本文摘抄自微信公众平台:AndroidDeveloper ...
- Android Studio学习随笔-模拟耗时操作(sleep)
在这里我申明一点,因为我是挂着VPN去YOUTOBE看的尚学堂的高明鑫老师讲的Android基础学习视频,有些东西他没有讲,而我也没办法,只能等两个星期后学校请老师来的时候进行询问,当然我也会将一些问 ...
- phonegap 2.8.1 toast
目录结构如下: 以上三个用红色框勾出的地方是需要修改的文件夹. 首先:添加java代码. 在src目录下新建一个包裹:org.apache.cordova 在该包裹下新建类:ToastPlugin.j ...
- 【转】Objective-C中一种消息处理方法performSelector: withObject:
原文 : http://www.cnblogs.com/buro79xxd/archive/2012/04/10/2440074.html Objective-C中调用函数的方法是“消息传递”,这 ...
- PHP PDO获取结果集
一.介绍PDO获取结果集,不得不介绍一下PDO是如果执行SQL语句,一般情况下分三种, 1.query()方法 query()方法通常用于返回执行查询后的结果集.语法是这样的:PDOStatement ...
- CSU 1616: Heaps(区间DP)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1616 1616: Heaps Time Limit: 2 Sec Memory Lim ...
- 安装 vsftp
1.yum安装 vsftp yum list vsftpd yum install vsftpd 2.配置 vsftp 将root注释掉 vi /etc/vsftpd/ftpusers 将root注释 ...