热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

k&r的表查找安装功能-Tablelookupinstallfunctionfromk&r

Section6.6ofK&Rdiscussesahashtableusingalinkedlist.K&R的6.6节讨论了使用链表的哈希表。Inshor

Section 6.6 of K&R discusses a hash table using a linked list.

K&R的6.6节讨论了使用链表的哈希表。

In short, a hash table is an array of pointers. The pointers point to a linked list. The linked list is a struct that looks like:

简而言之,哈希表是一个指针数组。指针指向链表。链表是一个结构,看起来像:

struct nlist {             /* table entry: */
    struct nlist *next;    /* next entry in chain */
    char *name;            /* defined name */
    char *defn;            /* replacement text */
};

The name is hashed, and this hash determines the index in the table. The chapter then shows code to add a name/defn pair to the table:

名称经过哈希处理,此哈希值确定表中的索引。然后,本章将显示向表中添加名称/ defn对的代码:

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 /* already there */
        free((void *) np->defn); /*free previous defn */
    if ((np->defn = strdup(defn)) == NULL)
        return NULL;
    return np;
}

Everything makes sense except for the following 2 lines:

除了以下两行之外,一切都有意义:

np->next = hashtab[hashval];
hashtab[hashval] = np;

When I try to understand this, I keep coming to the conclusion that the list now links back to itself and if you try to traverse the linked list it will be like a dog chasing its own tail. I would expect the code to set np->next to NULL.

当我试图理解这一点时,我不断得出结论,列表现在链接回自己,如果你试图遍历链表,它就像一条狗追逐自己的尾巴。我希望代码将np->设置为NULL。

What am I not understanding? How come this code works ?

我不明白的是什么?为什么这段代码有效?

4 个解决方案

#1


14  

It results in the new value being inserted at the beginning of the list.

它导致新值插入列表的开头。

So it goes from:

所以它来自:

hashtab[hashval]   -->  original_list

to:

hashtab[hashval]   -->  original_list
np->next           -->  original_list

to:

hashtab[hashval]  -->  *np
        np->next  -->  original_list

The golden rule is, if linked-list code doesn't make sense, always draw out a diagram!

黄金法则是,如果链表代码没有意义,总是绘制出一个图表!

#2


2  

hashtab[hashval] is a pointer, not a value. It is a pointer to the address in memory where the first element of that particular row in the pointer table (static struct nlist *hashtab[HASHSIZE]) resides. np and np->next are also pointers. So here is what happens in these two lines: First line: The pointer of the first element in that row of the table is copied into np->next. np->next now points at the address in memory where the first element of that row used to point. Second line: The pointer to the address in memory where the new *np resides is now copied into the pointer variable hastab[hashval]. hastab[hashval] now once again becomes the pointer to where the first element of that table row resides. So, just as Oli wrote, the new *np is inserted at the beginning of that particular row in the table.

hashtab [hashval]是一个指针,而不是一个值。它是指向内存中地址的指针,其中指针表(static struct nlist * hashtab [HASHSIZE])中该特定行的第一个元素所在。 np和np-> next也是指针。所以这是在这两行中发生的事情:第一行:表中该行的第一个元素的指针被复制到np-> next。 np-> next现在指向内存中用于指向该行的第一个元素的地址。第二行:指向新* np所在的内存中地址的指针现在被复制到指针变量hastab [hashval]中。 hastab [hashval]现在再次成为指向该表行的第一个元素所在的指针。所以,就像Oli写的那样,新的* np插入到表中特定行的开头。

#3


0  

There is already a node there. That node is null. By defining the nlist struct as static, it is automatically initiates all its element pointers to NULL.

那里已经有一个节点了。该节点为null。通过将nlist结构定义为静态,它会自动将其所有元素指针启动为NULL。

Correct me if I'm wrong.

如我错了请纠正我。

#4


0  

But there is no original list here. It is inserting a node where no key is found, right?

但这里没有原始清单。它正在插入一个没有找到密钥的节点,对吗?

if ((np = lookup(name)) == NULL) { /* not found */ 

推荐阅读
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 本文介绍了Java中Hashtable的clear()方法,该方法用于清除和移除指定Hashtable中的所有键。通过示例程序演示了clear()方法的使用。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • Java编程实现邻接矩阵表示稠密图的方法及实现类介绍
    本文介绍了Java编程如何实现邻接矩阵表示稠密图的方法,通过一个名为AMWGraph.java的类来构造邻接矩阵表示的图,并提供了插入结点、插入边、获取邻接结点等功能。通过使用二维数组来表示结点之间的关系,并通过元素的值来表示权值的大小,实现了稠密图的表示和操作。对于对稠密图的表示和操作感兴趣的读者可以参考本文。 ... [详细]
  • python中安装并使用redis相关的知识
    本文介绍了在python中安装并使用redis的相关知识,包括redis的数据缓存系统和支持的数据类型,以及在pycharm中安装redis模块和常用的字符串操作。 ... [详细]
  • 类Hashtable<K,V>所有已实现的接口:Serializable,Cloneable,Map<K,V>此类实现一个哈希表,该哈希表将键映 ... [详细]
  • hashmap线程不安全允许有null的键和值效率高一点、方法不是Synchronize的要提供外同步有containsvalue和containsKey方法HashMap是Java1 ... [详细]
  • 一、HashMap1.HashMap概述:HashMap是基于哈希表的Map接口的非同步实现。此实现提供所有可选的映射操作,并允许使用null值和null键。此类不保证映射的顺序,特别是 ... [详细]
  • HashMap和Hashtable的区别主要的区别有三点:线程安全性,同步(synchronization),以及速度。(两者都是无序排放)HashMap几乎可以等价于Hashtable,除了Hash ... [详细]
  • 1.利用Hashtable如何从链表中删除重复数据,最容易想到的方法就是遍历链表,把遍历的值存储到一个Hashtable中,在遍历过程中,若当前访问的值在Hashtable中已经 ... [详细]
  • HashMap及HashTable源码解析HashMap在java和Android经常使用到,之前学过数据结构,理解了它的原理,却很 ... [详细]
  • 本文介绍了一种图的存储和遍历方法——链式前向星法,该方法在存储带边权的图时时间效率比vector略高且节省空间。然而,链式前向星法存图的最大问题是对一个点的出边进行排序去重不容易,但在平行边无所谓的情况下选择这个方法是非常明智的。文章还提及了图中搜索树的父子关系一般不是很重要,同时给出了相应的代码示例。 ... [详细]
author-avatar
爱的甜蜜日记2010
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有