-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
76 lines (69 loc) · 2.39 KB
/
test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
function LinkedListQueue() {
// 链表头节点,队列头部元素从此处开始访问
this.head = null;
// 链表尾节点,用于在队列尾部快速插入元素
this.tail = null;
// 队列长度,方便追踪队列元素数量
this.length = 0;
}
// 链表节点构造函数
function Node(data) {
this.data = data;
this.next = null;
}
LinkedListQueue.prototype.enqueue = function (data) {
if (data === 2) {
console.log(this.head.next === this.tail);
debugger
}
const newNode = new Node(data);
if (this.length === 0) {
// 若队列为空,新节点既是头节点也是尾节点
this.head = newNode;
this.tail = newNode;
} else {
/**
* 将新节点添加到尾节点之后,并更新尾节点
*
* 在链表实现的队列中,虽然没有直接对 `head` 进行显式的修改操作,
* 但当执行 `this.tail.next = newNode` 时,会间接影响到 `head` 的 `next` 指针。
* 原因是在第一次入队 `queue.enqueue(1)` 时,`head` 和 `tail` 都指向了这个新创建的包含数据 `1` 的节点。
* 当第二次执行 `queue.enqueue(12)` 时,`this.tail` 就是之前那个包含数据 `1` 的节点,
* 执行 `this.tail.next = newNode` 就将包含数据 `1` 的节点的 `next` 指针指向了新创建的包含数据 `12` 的节点,
* 所以看起来 `head` 的 `next` 就发生了改变,实际上是因为 `tail` 原本和 `head` 指向同一个初始节点,
* 对 `tail` 的后继节点的操作自然会反映在链表的结构中,进而影响到 `head` 所能访问到的后续节点。
*/
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
};
LinkedListQueue.prototype.dequeue = function () {
if (this.length === 0) {
return null;
}
const data = this.head.data;
this.head = this.head.next;
if (this.length === 1) {
// 若队列仅剩一个元素,出队后尾节点也需置空
this.tail = null;
}
this.length--;
return data;
};
const queue = new LinkedListQueue();
queue.enqueue(1);
queue.enqueue(12);
console.log(queue);
</script>
</html>