linked-list 0.0.12 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/lib/linked-list/list.rb +10 -40
- data/lib/linked-list/version.rb +1 -1
- data/test/list_test.rb +51 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11374e3455b233cc16781e3275650c2242fa34aa1631636ca7874eb6576d40af
|
4
|
+
data.tar.gz: 4d86d4d970ae13882d9599bd207abda51cc3472327346adf540e3ee5dc286003
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 148fb602013aaefce59532166566f1720988b1f419ad23106b990d9dc3efb4b25386947ba067dcab618ca5af41b959b230c6aa8e4a4be658ba07eebbc5dce95f
|
7
|
+
data.tar.gz: 66d06537f183dfc5174bed493862b906f83238a74ad1a57549fa8af813fd0076a964177dd290836167b68a5478d1ab101e43d9c64fe003aecdc1a23ecf0d69a6
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
# 0.0.13 / 2018-11-27
|
2
|
+
|
3
|
+
## Fixed
|
4
|
+
|
5
|
+
- Delete duplicate method definitions (nex3 in [#7](https://github.com/spectator/linked-list/pull/7))
|
6
|
+
|
7
|
+
## Added
|
8
|
+
|
9
|
+
- Allow `List#delete` to delete a `Node` in O(1) time (nex3 in [#6](https://github.com/spectator/linked-list/pull/6))
|
10
|
+
|
11
|
+
[Compare v0.0.12...v0.0.13](https://github.com/spectator/linked-list/compare/v0.0.12...v0.0.13)
|
12
|
+
|
1
13
|
# 0.0.12 / 2018-09-04
|
2
14
|
|
3
15
|
## Added
|
data/lib/linked-list/list.rb
CHANGED
@@ -130,10 +130,19 @@ module LinkedList
|
|
130
130
|
|
131
131
|
# Removes first matched node.data from the the list by passed block or value.
|
132
132
|
#
|
133
|
+
# If +val+ is a +Node+, removes that node from the list. Behavior is
|
134
|
+
# undefined if +val+ is a +Node+ that's not a member of this list.
|
135
|
+
#
|
133
136
|
# == Returns:
|
134
|
-
# Deleted node
|
137
|
+
# Deleted node's data
|
135
138
|
#
|
136
139
|
def delete(val = nil, &block)
|
140
|
+
if val.respond_to?(:to_node)
|
141
|
+
node = val.to_node
|
142
|
+
__unlink_node(node)
|
143
|
+
return node.data
|
144
|
+
end
|
145
|
+
|
137
146
|
each_node.find(&__to_matcher(val, &block)).tap do |node_to_delete|
|
138
147
|
return unless node_to_delete
|
139
148
|
__unlink_node(node_to_delete)
|
@@ -274,45 +283,6 @@ module LinkedList
|
|
274
283
|
self
|
275
284
|
end
|
276
285
|
|
277
|
-
# Inserts data after passed node.
|
278
|
-
#
|
279
|
-
# == Returns:
|
280
|
-
# Inserted node
|
281
|
-
#
|
282
|
-
def insert_after_node(data, node)
|
283
|
-
Node(data).tap do |new_node|
|
284
|
-
new_node.prev = node
|
285
|
-
new_node.next = node.next
|
286
|
-
if node.next
|
287
|
-
node.next.prev = new_node
|
288
|
-
else
|
289
|
-
@tail = new_node
|
290
|
-
end
|
291
|
-
node.next = new_node
|
292
|
-
@length += 1
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
|
297
|
-
# Inserts data before passed node.
|
298
|
-
#
|
299
|
-
# == Returns:
|
300
|
-
# Inserted node
|
301
|
-
#
|
302
|
-
def insert_before_node(data, node)
|
303
|
-
Node(data).tap do |new_node|
|
304
|
-
new_node.next = node
|
305
|
-
new_node.prev = node.prev
|
306
|
-
if node.prev
|
307
|
-
node.prev.next = new_node
|
308
|
-
else
|
309
|
-
@head = new_node
|
310
|
-
end
|
311
|
-
node.prev = new_node
|
312
|
-
@length += 1
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
316
286
|
private
|
317
287
|
|
318
288
|
def __unlink_node(node)
|
data/lib/linked-list/version.rb
CHANGED
data/test/list_test.rb
CHANGED
@@ -4,6 +4,7 @@ describe LinkedList::List do
|
|
4
4
|
let(:list) { create_list }
|
5
5
|
let(:node_1) { create_node('foo') }
|
6
6
|
let(:node_2) { create_node('bar') }
|
7
|
+
let(:node_3) { create_node('baz') }
|
7
8
|
|
8
9
|
describe 'instantiation' do
|
9
10
|
it 'assigns first to nil' do
|
@@ -476,6 +477,56 @@ describe LinkedList::List do
|
|
476
477
|
end
|
477
478
|
end
|
478
479
|
end
|
480
|
+
|
481
|
+
describe 'by node' do
|
482
|
+
it 'deletes first node' do
|
483
|
+
list.push(node_1)
|
484
|
+
list.push(node_2)
|
485
|
+
list.push(node_3)
|
486
|
+
list.delete(node_1)
|
487
|
+
assert_equal ['bar', 'baz'], list.to_a
|
488
|
+
end
|
489
|
+
|
490
|
+
it 'returns deleted value' do
|
491
|
+
list.push(node_1)
|
492
|
+
list.delete(node_1)
|
493
|
+
assert_equal node_1.data, list.delete(node_1)
|
494
|
+
end
|
495
|
+
|
496
|
+
it 'decreases length of list' do
|
497
|
+
list.push('foo')
|
498
|
+
list.push('bar')
|
499
|
+
list.push('baz')
|
500
|
+
list.delete('foo')
|
501
|
+
assert_equal 2, list.length
|
502
|
+
end
|
503
|
+
|
504
|
+
describe 'position edge cases' do
|
505
|
+
before do
|
506
|
+
list.push(node_1)
|
507
|
+
list.push(node_2)
|
508
|
+
list.push(node_3)
|
509
|
+
end
|
510
|
+
|
511
|
+
it 'deletes value from head' do
|
512
|
+
list.delete(node_1)
|
513
|
+
assert_equal ['bar', 'baz'], list.to_a
|
514
|
+
assert_equal 'bar', list.first
|
515
|
+
end
|
516
|
+
|
517
|
+
it 'deletes value from middle' do
|
518
|
+
list.delete(node_2)
|
519
|
+
assert_equal ['foo', 'baz'], list.to_a
|
520
|
+
end
|
521
|
+
|
522
|
+
|
523
|
+
it 'deletes value from tail' do
|
524
|
+
list.delete(node_3)
|
525
|
+
assert_equal ['foo', 'bar'], list.to_a
|
526
|
+
assert_equal 'bar', list.last
|
527
|
+
end
|
528
|
+
end
|
529
|
+
end
|
479
530
|
end
|
480
531
|
|
481
532
|
describe '#delete_all' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linked-list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Velikanau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|