data structure: static linked list / hash linking / an array based linked list

 

本来是对 CollisionCheckStack 数据结构的探究。

  • Push/pop operation.
  • Duplicate check. When an object that's already in the stack is pushed, this class will tell you so.

push/pop operation


使用一个数组,在数组尾端(tail)进行操作即可

 

 

Duplicate check

寻找一个元素,有如下几种策略

  • loop all the element to find
  • sort the elements, using binaray search
    • B-tree
  • build index, use the index to find
    • use hashmap

CollisionCheckStack 使用hashmap来快速定位。我们知道hashmap最简单的实现方式是chain , 而chain最简单的实现方式是Linked list

 

Kohsuke Kawaguchi 采用的是array based linked list或者wiki中所说的hash linking

 

The link fields need not be physically part of the nodes. If the data records are stored in an array and referenced by their indices, the link field may be stored in a separate array with the same indices as the data records.

 

wiki 中在相关数据结构中提到

 

A hash table may use linked lists to store the chains of items that hash to the same position in the hash table.

A heap shares some of the ordering properties of a linked list, but is almost always implemented using an array. Instead of references from node to node, the next and previous data indexes are calculated using the current data's index.

 

使用数组来实现linked list, (A linked list could be implemented in an array )

 

 

 

上面的Linked list采用三元组表示为 Data (index) (next node index)

 

                David (0) (4) -> Ioshua (4)  (7)->  Leah (7) (2) -> Miriam (2) (6) -> Robert (6) (-1)

 

其要点

  • 数组中的element有个index属性,得到index就得到element
  • next[i] 即当前节点(第i个节点)的下一个元素在数组中的index

 

把array based linked list 运用到hash map中就是

 

 

一共使用了三个数组

  • Object[] data, 如前,用来实现stack
  • int[] initialHash, initialHash[i]用来存放hash值为i的头节点在data数组中的位置
  • int[] next, next[i]用来存放与data[i]有相同hash值的下一元素在data数组中的位置

Suppose Find(data) return the index of data in the data array

 

Find(d6)的过程如下

      hash(d6) = 1

      initHash(1) = 6

      data[6] == d6, return 6

 

Find(d4)的过程如下

     hash(d4) = 1

     initHash(1) = 6

     data[6] != d4

     next[6] = 4

     data[4] == d4, return 4

 

Find(d3)的过程如下

    hahs(d3) = 1

    initHash(1) = 6

    data[6] != d3

    next[6] = 4

    data[4] != d3

    next[4] = 3

    data[3] == d3, return 3

 

Find(e) 函数如下:

 

insert(e)函数如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FireCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值