-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcircular-linked-list.js
140 lines (127 loc) · 2.88 KB
/
circular-linked-list.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* @desc 循环链表
*/
import LinkedList from './linked-list'
import { Node } from './models/linked-list-models'
export default class CircularLinkedList extends LinkedList {
constructor () {
super()
}
/**
* @desc 在链表最后添加元素
* @param {Any} element
*/
push (element) {
const node = new Node(element)
let current
if (this.head === null) { // 链表为空时
this.head = node
} else {
current = this.getElementAt(this.size() - 1)
current.next = node
}
node.next = this.head
this.count++
}
/**
* @desc 根据下标获取链表元素
* @param {Number} index
* @return {Node}
*/
// getElementAt (index) {}
/**
* @desc 在任意位置插入元素
* @param {Any} element 插入的元素
* @param {Number} index 期望插入的下标
* @return {Boolean} 插入成功返回true
*/
insert (element, index) {
this._checkIndex(index)
const node = new Node(element)
let current = this.head
if (index === 0) { // 插到头
if (this.head === null) {
this.head = node
node.next = this.head
} else {
node.next = current
current = this.getElementAt(this.size())
// update last element
this.head = node
current.next = this.head
}
} else { // 插到非头任意位置
const previous = this.getElementAt(index - 1)
node.next = previous.next
previous.next = node
}
this.count++
return true
}
/**
* @desc 根据下标移除某一项
* @param {Number} index
* @return {Any} 被移除项的值
*/
removeAt (index) {
this._checkIndex(index)
let current = this.head
if (index === 0) { // 移除头
if (this.size() === 1) {
this.head = null
} else {
const removed = this.head
current = this.getElementAt(this.size() - 1)
this.head = this.head.next
current.next = this.head
current = removed
}
} else {
const previous = this.getElementAt(index - 1)
current = previous.next
previous.next = current.next
}
this.count--
return current.element
}
/**
* @desc 根据值移除某一项
* @param {Any} element
*/
// remove (element) {}
/**
* @desc 获取某一项在链表中的下标
* @param {Any} element
* @return {Number}
*/
// indexOf (element) {}
/**
* @desc 判断是否为空
* @return {Boolean}
*/
// isEmpty () {}
/**
* @desc 获取链表长度
* @return {Number}
*/
// size () {}
/**
* @desc 获取链表头
* @return {Node}
*/
// getHead () {}
/**
* @desc 清空链表
*/
// clear () {}
/**
* @desc 打印链表
* @return {String}
*/
// toString () {}
/**
* @desc 检查下标是否溢出
* @param {Number} index
*/
// _checkIndex (index) {}
}