做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. 操作SharedPreferences的注意点

    如果使用SharedPreferences用于数据存取,大部分人喜欢使用如下代码: public void writeSharedprefs(int pos) { SharedPreferences  ...

  2. linux查看cpu温度

      分类: linux系统 一.安装  sudo apt-get install lm-sensors   二.查看 linux@cdyemail:~$ sensors k10temp-pci-00c ...

  3. Android实现网络多线程断点续传下载(转)

    本示例介绍在Android平台下通过HTTP协议实现断点续传下载. 我们编写的是Andorid的HTTP协议多线程断点下载应用程序.直接使用单线程下载HTTP文件对我们来说是一件非常简单的事.那么,多 ...

  4. IDL绘制黑体辐射曲线

    普朗克定律是热红外遥感中常常使用的三大定律之一,描述了黑体辐射能量的情况.绝对黑体的辐射光谱对于研究一切物体的辐射规律具有根本的意义.1900年普朗克引进量子概念,将辐射当做不连续的量子发射,成功地从 ...

  5. fluentd结合kibana、elasticsearch实时搜索分析hadoop集群日志<转>

    转自 http://blog.csdn.net/jiedushi/article/details/12003171 Fluentd是一个开源收集事件和日志系统,它目前提供150+扩展插件让你存储大数据 ...

  6. python实现登录函数,比较简单

    一个简单的python实现登录以及修改密码的函数 #密码错误3次,锁定登录: password_list = ['] def account_login(): Tries = 3 while Trie ...

  7. IOS 开发 【objective-c 基础1】

    案例:读取本地硬盘上程序根目录下words.txt文件内容,显示每行的字符数. // // main.m // hello // // Created by swack on 15/11/27. // ...

  8. codevs1404字符串匹配

    /* 无奈我改了那么久还是看的题解 首先跑一边kmp 几下ans[p]表示总共匹配到长度p的次数 这些不一定都是恰好到p 所以在处理一下 ans[p]通过处理变成 所有的匹配到长度p的次数 最后答案就 ...

  9. 滑动页面,顶部导航or顶部 固定在一个位置

    现在很多页面 特别是电商用的比较多 比如电商里面某个商品的详细页 往下拉页面 当滚轮到达一定位置的时候  导航栏即固定在顶部 其实他的原理很简单, 就是一开始设置导航为相对定位,然后计算出滚动条离顶部 ...

  10. Hive学习之六 《Hive进阶— —hive jdbc》 详解

    接Hive学习五 http://www.cnblogs.com/invban/p/5331159.html 一.配置环境变量 hive jdbc的开发,在开发环境中,配置Java环境变量 修改/etc ...