Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6026662

Browse files
committed
学习 链表、队列、栈以及哈希表
1 parent dde65b2 commit 6026662

File tree

10 files changed

+102
-62
lines changed

10 files changed

+102
-62
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage
55
.DS_Store
66

77
temp
8+
tempCodeRunnerFile.js

examples/fibonacci/.circleci/config.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const assert = require('assert')
2-
const fastfib = require('../lib/index')
1+
const assert = require('assert');
2+
const fastfib = require('../lib/index');
33

4-
assert.equal(fastfib(0), 0)
5-
assert.equal(fastfib(1), 1)
6-
assert.equal(fastfib(2), 1)
7-
assert.equal(fastfib(3), 2)
8-
assert.equal(fastfib(4), 3)
9-
assert.equal(fastfib(5), 5)
10-
assert.equal(fastfib(6), 8)
11-
assert.equal(fastfib(7), 13)
12-
assert.equal(fastfib(8), 21)
13-
assert.equal(fastfib(9), 34)
14-
assert.equal(fastfib(10), 55)
15-
assert.equal(fastfib(11), 89)
16-
assert.equal(fastfib(12), 144)
4+
assert.equal(fastfib(0), 0);
5+
assert.equal(fastfib(1), 1);
6+
assert.equal(fastfib(2), 1);
7+
assert.equal(fastfib(3), 2);
8+
assert.equal(fastfib(4), 3);
9+
assert.equal(fastfib(5), 5);
10+
assert.equal(fastfib(6), 8);
11+
assert.equal(fastfib(7), 13);
12+
assert.equal(fastfib(8), 21);
13+
assert.equal(fastfib(9), 34);
14+
assert.equal(fastfib(10), 55);
15+
assert.equal(fastfib(11), 89);
16+
assert.equal(fastfib(12), 144);
1717

18-
console.log('Test passed')
18+
console.log('Test passed');

examples/hash/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# HASH 算法
2+
3+
常用 HASH 函数
4+
5+
- 直接取余法:f(x) = x mod maxM; maxM一般是不太接近 2^t 的一个质数。
6+
- 乘法取整法:f(x) = trunc((x/maxX)*maxlongit) mod maxM,主要用于实数。
7+
- 平方取中法:f(x) = (x*x div 1000 ) mod 1000000); 平方后取中间的,每位包含信息比较多。
8+
9+
散列函数能使对一个数据序列的访问过程更加迅速有效,通过散列函数,数据元素将被更快地定位。
10+
11+
常用hash算法
12+
13+
- MD5
14+
- MD4(RFC 1320)是 MIT 的Ronald L. Rivest在 1990 年设计的,MD 是 Message Digest(消息摘要) 的缩写。它适用在32位字长的处理器上用高速软件实现——它是基于 32位操作数的位操作来实现的。
15+
- MD5(RFC 1321)是 Rivest 于1991年对MD4的改进版本。它对输入仍以512位分组,其输出是4个32位字的级联,与 MD4 相同。MD5比MD4来得复杂,并且速度较之要慢一点,但更安全,在抗分析和抗差分方面表现更好。
16+
- SHA 家族
17+
- SHA1是由NIST NSA设计为同DSA一起使用的,它对长度小于2^64的输入,产生长度为160bit的散列值,因此抗穷举(brute-force)性更好。SHA-1 设计时基于和MD4相同原理,并且模仿了该算法。
18+
19+
HASH 主要用于信息安全领域中加密算法,它把一些不同长度的信息转化成杂乱的128位的编码里,叫做HASH值. 也可以说,hash就是找到一种数据内容和数据存放地址之间的映射关系。Hash算法在信息安全方面的应用主要体现在以下的3个方面:
20+
21+
- 文件校验
22+
- 我们比较熟悉的校验算法有奇偶校验和CRC校验,这2种校验并没有抗数据篡改的能力,它们一定程度上能检测并纠正数据传输中的信道误码,但却不能防止对数据的恶意破坏。
23+
- MD5 Hash算法的"数字指纹"特性,使它成为目前应用最广泛的一种文件完整性校验和(Checksum)算法,不少Unix系统有提供计算md5 checksum的命令。
24+
- 数字签名
25+
- Hash 算法也是现代密码体系中的一个重要组成部分。由于非对称算法的运算速度较慢,所以在数字签名协议中,单向散列函数扮演了一个重要的角色。对 Hash 值,又称"数字摘要"进行数字签名,在统计上可以认为与对文件本身进行数字签名是等效的。而且这样的协议还有其他的优点。
26+
- 鉴权协议
27+
- 鉴权协议又被称作"挑战--认证模式:在传输信道是可被侦听,但不可被篡改的情况下,这是一种简单而安全的方法。
28+
29+
参考资料:
30+
31+
- [Hash(散列函数)](https://baike.baidu.com/item/Hash/390310)
32+
- [MD5](https://baike.baidu.com/item/MD5)
33+
- [SHA家族](https://baike.baidu.com/item/SHA%E5%AE%B6%E6%97%8F/9849595)
34+
- [npm: md5](https://www.npmjs.com/package/md5)

examples/hash/src/md5.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
// https://github.com/pvorb/node-md5
3+
// import md5 from 'md5';
4+
5+
// message -- `String`, `Buffer`, `Array` or `Uint8Array`
6+
// returns `String`
7+
8+
// md5(message)

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ module.exports = {
1313
coverageDirectory: './coverage/',
1414

1515
// If the test path matches any of the patterns, it will be skipped.
16+
// https://jestjs.io/docs/zh-Hans/configuration#testpathignorepatterns-%E6%95%B0%E7%BB%84-string
1617
testPathIgnorePatterns: [
1718
'<rootDir>/node_modules/',
1819
'<rootDir>/examples/',
1920
'<rootDir>/temp/',
21+
// '<rootDir>/**/tempCodeRunnerFile.js',
2022
],
2123

2224
// If the file path matches any of the patterns, coverage information will be skipped.

src/data-structures/doubly-linked-list/README.zh-CN.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
两个节点链接允许在任一方向上遍历列表。
1010

11-
在双向链表中进行添加或者删除节点时,需做的链接更改要比单向链表复杂得多。这种操作在单向链表中更简单高效,因为不需要关注一个节点(除第一个和最后一个节点以外的节点)的两个链接,而只需要关注一个链接即可。
12-
13-
11+
在双向链表中进行添加或者删除节点时,需做的链接更改要比单向链表复杂得多。这种操作在单向链表中更简单高效,因为不需要关注一个节点(除第一个和最后一个节点以外的节点)的两个链接,而只需要关注一个链接即可。
1412

1513
## 基础操作的伪代码
1614

src/data-structures/hash-table/README.zh-CN.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,33 @@
33
在计算中, 一个 **哈希表(hash table 或hash map)** 是一种实现 *关联数组(associative array)*
44
的抽象数据类型, 该结构可以将 *键映射到值*
55

6-
哈希表使用 *哈希函数/散列函数* 来计算一个值在数组或桶(buckets)中或槽(slots)中对应的索引,可使用该索引找到所需的值。
6+
哈希表使用 *哈希函数/散列函数* 来计算一个值在数组或桶(buckets)中或槽(slots)中对应的索引可使用该索引找到所需的值。
77

8-
理想情况下,散列函数将为每个键分配给一个唯一的桶(bucket),但是大多数哈希表设计采用不完美的散列函数,这可能会导致"哈希冲突(hash collisions)",也就是散列函数为多个键(key)生成了相同的索引,这种碰撞必须
9-
以某种方式进行处理。
8+
## 伪代码
109

10+
### 计算 hash
11+
12+
为简单起见,我们将只使用密钥中所有字符的字符代码和计算散列值。但也可以使用更复杂的方法,如多项式字符串哈希来减少碰撞次数:
13+
14+
```text
15+
hash = charCodeAt(0) * PRIME^(n-1) + charCodeAt(1) * PRIME^(n-2) + ... + charCodeAt(n-1)
16+
```
17+
18+
其中 charCodeAt(i) 是键的第i个字符代码,n是键的长度,PRIME就是任何质数,比如31。
19+
20+
```text
21+
Hash(key)
22+
Pre: 输入任意长度 key
23+
Post: 通过散列算法变换成固定长度的散列值输出(消息摘要)
24+
hash ← Array.from(key).reduce(
25+
(hashAccumulator, keySymbol) => (hashAccumulator + keySymbol.charCodeAt(0)),
26+
0,
27+
);
28+
return hash % this.buckets.length;
29+
end Hash
30+
```
31+
32+
理想情况下,散列函数将为每个键分配给一个唯一的桶(bucket),但是大多数哈希表设计采用不完美的散列函数,这可能会导致"哈希冲突(hash collisions)",也就是散列函数为多个键(key)生成了相同的索引,这种碰撞必须以某种方式进行处理。
1133

1234
![Hash Table](./images/hash-table.jpeg)
1335

src/data-structures/linked-list/LinkedList.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ export default class LinkedList {
234234
return nodes;
235235
}
236236

237+
/**
238+
* @param {*[]} values - Array of values that need to be converted to linked list.
239+
* @return {LinkedList}
240+
*/
241+
fromArray(values) {
242+
values.forEach(value => this.append(value));
243+
244+
return this;
245+
}
246+
237247
/**
238248
* @param {function} [callback]
239249
* @return {string}

src/data-structures/linked-list/README.zh-CN.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
## 基本操作的伪代码
1616

17+
[伪代码格式](https://blog.csdn.net/ssisse/article/details/51501797)
18+
19+
- 赋值用箭头“←”
20+
1721
### 插入
1822

1923
```text
@@ -31,7 +35,7 @@ Add(value)
3135
end Add
3236
```
3337

34-
```
38+
```text
3539
Prepend(value)
3640
Pre: value is the value to add to the list
3741
Post: value has been placed at the head of the list

0 commit comments

Comments
 (0)