linked-list 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +9 -0
- data/lib/linked-list.rb +3 -0
- data/lib/linked-list/conversions.rb +42 -0
- data/lib/linked-list/list.rb +176 -0
- data/lib/linked-list/node.rb +19 -0
- data/lib/linked-list/version.rb +5 -0
- data/linked-list.gemspec +24 -0
- data/test/conversions_test.rb +32 -0
- data/test/list_test.rb +203 -0
- data/test/node_test.rb +33 -0
- data/test/test_helper.rb +15 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9a0280fdef3349b667cc6b932c6873c6b2e23c25
|
4
|
+
data.tar.gz: 57e230c91ee076d78d42e549dd562e3b6cdebffd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 30ba2f45bca37a529dd265f41b523dcdf69649d4b66ce6baa1dd650815c5139e7cea5b942de9d7dcfd3f9b69ed11ff4feca8cf46be89c3dab3b5baae9f37ed7d
|
7
|
+
data.tar.gz: 556a3fa2ec3a3497159a3b4a73bbdaab88719a9c01fc63f41c84b71c773675e0369c10ae0581524d06b9999c8612667fb1c96b67c99327cc4055dfba3906e6c0
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Yury Velikanau
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/spectator/linked-list.png)](https://codeclimate.com/github/spectator/linked-list)
|
2
|
+
[![Build Status](https://secure.travis-ci.org/spectator/linked-list.png?branch=master)](http://travis-ci.org/spectator/linked-list)
|
3
|
+
|
4
|
+
# LinkedList
|
5
|
+
|
6
|
+
Ruby implementation of Linked List, following some Ruby idioms.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'linked-list'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
```shell
|
19
|
+
$ bundle
|
20
|
+
```
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
```shell
|
25
|
+
$ gem install linked-list
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
object = Object.new # could be anything
|
32
|
+
list = LinkedList::List.new
|
33
|
+
|
34
|
+
list.push(object)
|
35
|
+
list << object # same as `push`
|
36
|
+
|
37
|
+
list.unshift(object)
|
38
|
+
|
39
|
+
list.pop
|
40
|
+
list.shift
|
41
|
+
|
42
|
+
list.reverse
|
43
|
+
list.reverse!
|
44
|
+
|
45
|
+
list.each # Enumerator object
|
46
|
+
list.each { |e| puts e }
|
47
|
+
|
48
|
+
list.first # head of the list
|
49
|
+
list.last # tail of the list
|
50
|
+
|
51
|
+
list.to_a
|
52
|
+
```
|
53
|
+
|
54
|
+
Please see `LinkedList::List` and `LinkedList::Node` for details.
|
55
|
+
|
56
|
+
## TODO
|
57
|
+
|
58
|
+
* Insert / delete in the middle
|
59
|
+
* More readable `inspect`
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/linked-list.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module LinkedList
|
2
|
+
module Conversions
|
3
|
+
module_function
|
4
|
+
|
5
|
+
# +Node()+ tries to convert its argument to +Node+ object by first calling
|
6
|
+
# +#to_node+, if that is not availabe then it will instantiate a new +Node+
|
7
|
+
# object.
|
8
|
+
#
|
9
|
+
# == Returns:
|
10
|
+
# New +Node+ object.
|
11
|
+
#
|
12
|
+
def Node(arg)
|
13
|
+
case arg
|
14
|
+
when ->(_arg) { _arg.respond_to?(:to_node) }
|
15
|
+
arg.to_node
|
16
|
+
else
|
17
|
+
Node.new(arg)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# +List()+ tries to conver its argument to +List+ object by first calling
|
22
|
+
# +#to_list+, if that is not availabe and its argument is an array (or can
|
23
|
+
# be convertd into array with +#to_ary+) then it will instantiate a new
|
24
|
+
# +List+ object making nodes from array elements. If none above applies,
|
25
|
+
# then a new +List+ will be instantiated with one node holding argument
|
26
|
+
# value.
|
27
|
+
#
|
28
|
+
# == Returns:
|
29
|
+
# New +List+ object.
|
30
|
+
#
|
31
|
+
def List(arg)
|
32
|
+
case arg
|
33
|
+
when ->(_arg) { _arg.respond_to?(:to_list) }
|
34
|
+
arg.to_list
|
35
|
+
when ->(_arg) { _arg.respond_to?(:to_ary) }
|
36
|
+
arg.to_ary.each_with_object(List.new) { |n, l| l.push(Node(n)) }
|
37
|
+
else
|
38
|
+
List.new.push(Node(arg))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
module LinkedList
|
2
|
+
class List
|
3
|
+
include Conversions
|
4
|
+
|
5
|
+
attr_reader :length
|
6
|
+
alias_method :size, :length
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@head = nil
|
10
|
+
@tail = nil
|
11
|
+
@length = 0
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns the first element of the list or nil.
|
15
|
+
#
|
16
|
+
def first
|
17
|
+
@head && @head.data
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns the last element of the list or nil.
|
21
|
+
#
|
22
|
+
def last
|
23
|
+
@tail && @tail.data
|
24
|
+
end
|
25
|
+
|
26
|
+
# Pushes new nodes to the end of the list.
|
27
|
+
#
|
28
|
+
# == Parameters:
|
29
|
+
# node:: Any object, including +Node+ objects.
|
30
|
+
#
|
31
|
+
# == Returns:
|
32
|
+
# self of +List+ object.
|
33
|
+
#
|
34
|
+
def push(node)
|
35
|
+
node = Node(node)
|
36
|
+
@head ||= node
|
37
|
+
|
38
|
+
@tail.next = node if @tail
|
39
|
+
@tail = node
|
40
|
+
|
41
|
+
@length += 1
|
42
|
+
self
|
43
|
+
end
|
44
|
+
alias_method :<<, :push
|
45
|
+
|
46
|
+
# Pushes new nodes on top of the list.
|
47
|
+
#
|
48
|
+
# == Parameters:
|
49
|
+
# node:: Any object, including +Node+ objects.
|
50
|
+
#
|
51
|
+
# == Returns:
|
52
|
+
# self of +List+ object.
|
53
|
+
#
|
54
|
+
def unshift(node)
|
55
|
+
node = Node(node)
|
56
|
+
@tail ||= node
|
57
|
+
|
58
|
+
node.next = @head
|
59
|
+
@head = node
|
60
|
+
|
61
|
+
@length += 1
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
# Removes node from the end of the list.
|
66
|
+
#
|
67
|
+
# == Returns:
|
68
|
+
# Data stored in the node or nil.
|
69
|
+
#
|
70
|
+
def pop
|
71
|
+
return nil unless @head
|
72
|
+
|
73
|
+
node = @head
|
74
|
+
next_to_tail = nil
|
75
|
+
while(node = node.next)
|
76
|
+
next_to_tail = node unless node.next
|
77
|
+
end
|
78
|
+
@head = nil unless next_to_tail
|
79
|
+
|
80
|
+
tail = @tail
|
81
|
+
@tail = next_to_tail
|
82
|
+
|
83
|
+
@length -= 1
|
84
|
+
tail.data
|
85
|
+
end
|
86
|
+
|
87
|
+
# Removes node from the beginning of the list.
|
88
|
+
#
|
89
|
+
# == Returns:
|
90
|
+
# Data stored in the node or nil.
|
91
|
+
#
|
92
|
+
def shift
|
93
|
+
return nil unless @head
|
94
|
+
|
95
|
+
head = __shift
|
96
|
+
@tail = nil unless @head
|
97
|
+
|
98
|
+
@length -= 1
|
99
|
+
head.data
|
100
|
+
end
|
101
|
+
|
102
|
+
# Reverse list of nodes and returns new instance of the list.
|
103
|
+
#
|
104
|
+
# == Returns:
|
105
|
+
# New +List+ in reverse order.
|
106
|
+
#
|
107
|
+
def reverse
|
108
|
+
List(to_a).reverse!
|
109
|
+
end
|
110
|
+
|
111
|
+
# Reverses list of nodes in place.
|
112
|
+
#
|
113
|
+
# == Returns:
|
114
|
+
# self in reverse order.
|
115
|
+
#
|
116
|
+
def reverse!
|
117
|
+
return self unless @head
|
118
|
+
|
119
|
+
prev_node = __shift
|
120
|
+
prev_node.next = nil
|
121
|
+
@tail = prev_node
|
122
|
+
|
123
|
+
while(@head)
|
124
|
+
curr_node = __shift
|
125
|
+
curr_node.next = prev_node
|
126
|
+
prev_node = curr_node
|
127
|
+
end
|
128
|
+
@head = prev_node
|
129
|
+
self
|
130
|
+
end
|
131
|
+
|
132
|
+
# Iterates over nodes from top to bottom passing node data to the block if
|
133
|
+
# given. If no block given, returns +Enumerator+.
|
134
|
+
#
|
135
|
+
# == Returns:
|
136
|
+
# +Enumerator+ or yields data to the block stored in every node on the
|
137
|
+
# list.
|
138
|
+
#
|
139
|
+
def each
|
140
|
+
return to_enum(__callee__) unless block_given?
|
141
|
+
__each { |e| yield(e.data) }
|
142
|
+
end
|
143
|
+
|
144
|
+
# Converts list to array.
|
145
|
+
#
|
146
|
+
def to_a
|
147
|
+
each.to_a
|
148
|
+
end
|
149
|
+
alias_method :to_ary, :to_a
|
150
|
+
|
151
|
+
# Conversion function, see +Conversions.List+.
|
152
|
+
#
|
153
|
+
# == Returns:
|
154
|
+
# self
|
155
|
+
#
|
156
|
+
def to_list
|
157
|
+
self
|
158
|
+
end
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
def __shift
|
163
|
+
head = @head
|
164
|
+
@head = @head.next
|
165
|
+
head
|
166
|
+
end
|
167
|
+
|
168
|
+
def __each
|
169
|
+
next_node = @head
|
170
|
+
while(next_node)
|
171
|
+
yield next_node
|
172
|
+
next_node = next_node.next
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module LinkedList
|
2
|
+
class Node
|
3
|
+
attr_accessor :data, :next
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@data = data
|
7
|
+
@next = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
# Conversion function, see +Conversions.Node+.
|
11
|
+
#
|
12
|
+
# == Returns:
|
13
|
+
# self
|
14
|
+
#
|
15
|
+
def to_node
|
16
|
+
self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/linked-list.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'linked-list/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'linked-list'
|
8
|
+
spec.version = Linked::List::VERSION
|
9
|
+
spec.authors = ['Yury Velikanau']
|
10
|
+
spec.email = ['yury.velikanau@gmail.com']
|
11
|
+
spec.description = %q{Ruby implementation of Linked List, following some Ruby idioms.}
|
12
|
+
spec.summary = %q{Ruby implementation of Linked List, following some Ruby idioms.}
|
13
|
+
spec.homepage = 'https://github.com/spectator/linked-list'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_development_dependency 'bundler', '>= 1.3', '<= 2.0'
|
23
|
+
spec.add_development_dependency 'minitest', '>= 5.0', '<= 6.0'
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe LinkedList::Conversions do
|
4
|
+
describe 'Node()' do
|
5
|
+
it 'returns self if node is given' do
|
6
|
+
node = create_node('foo')
|
7
|
+
assert_equal LinkedList::Conversions.Node(node), node
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'returns new node' do
|
11
|
+
assert_instance_of LinkedList::Node,
|
12
|
+
LinkedList::Conversions.Node('foo')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'List()' do
|
17
|
+
it 'returns self if list is given' do
|
18
|
+
list = create_list
|
19
|
+
assert_equal LinkedList::Conversions.List(list), list
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns list with nodes made from array' do
|
23
|
+
list = LinkedList::Conversions.List([1, 2])
|
24
|
+
assert_equal [1, 2], [list.first, list.last]
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'returns new list with one node' do
|
28
|
+
list = LinkedList::Conversions.List('foo')
|
29
|
+
assert_equal 'foo', list.first
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/test/list_test.rb
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe LinkedList::List do
|
4
|
+
let(:list) { create_list }
|
5
|
+
let(:node_1) { create_node('foo') }
|
6
|
+
let(:node_2) { create_node('bar') }
|
7
|
+
|
8
|
+
describe 'instantiation' do
|
9
|
+
it 'assigns first to nil' do
|
10
|
+
assert_nil list.first
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'assigns last to nil' do
|
14
|
+
assert_nil list.last
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'has zero length' do
|
18
|
+
assert_equal 0, list.length
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#push' do
|
23
|
+
it 'last pushed node can be accessed with #last' do
|
24
|
+
list.push(node_1)
|
25
|
+
assert_equal node_1.data, list.last
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'last pushed node data can be accessed with #first' do
|
29
|
+
list.push(node_1)
|
30
|
+
assert_equal node_1.data, list.first
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'maintains first pushed node as first' do
|
34
|
+
list.push(node_1)
|
35
|
+
list.push(node_2)
|
36
|
+
assert_equal node_1.data, list.first
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'sets reference to the next node' do
|
40
|
+
list.push(node_1)
|
41
|
+
list.push(node_2)
|
42
|
+
assert_equal node_1.next, node_2
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'increases list length by 1' do
|
46
|
+
list.push(node_1)
|
47
|
+
assert_equal 1, list.length
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'returns self' do
|
51
|
+
assert_equal list.push(node_1), list
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#unshift' do
|
56
|
+
it 'last pushed node can be accessed with #last' do
|
57
|
+
list.unshift(node_1)
|
58
|
+
assert_equal node_1.data, list.last
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'last pushed node can be accessed with #first' do
|
62
|
+
list.unshift(node_1)
|
63
|
+
assert_equal node_1.data, list.first
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'maintains first pushed node as last' do
|
67
|
+
list.unshift(node_1)
|
68
|
+
list.unshift(node_2)
|
69
|
+
assert_equal node_1.data, list.last
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'sets reference to the next node' do
|
73
|
+
list.unshift(node_1)
|
74
|
+
list.unshift(node_2)
|
75
|
+
assert_equal node_2.next, node_1
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'increases list length by 1' do
|
79
|
+
list.unshift(node_1)
|
80
|
+
assert_equal 1, list.length
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'returns self' do
|
84
|
+
assert_equal list.unshift(node_1), list
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe '#pop' do
|
89
|
+
it 'returns nil if list is empty' do
|
90
|
+
assert_nil list.pop
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'returns node from the end of the list' do
|
94
|
+
list.push(node_1)
|
95
|
+
list.push(node_2)
|
96
|
+
assert_equal node_2.data, list.pop
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'sets #last to nil when all nodes are removed' do
|
100
|
+
list.push(node_1)
|
101
|
+
list.pop
|
102
|
+
assert_nil list.last
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'sets #first to nil when all nodes are removed' do
|
106
|
+
list.push(node_1)
|
107
|
+
list.pop
|
108
|
+
assert_nil list.first
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'reduces list length by 1' do
|
112
|
+
list.push(node_1)
|
113
|
+
list.pop
|
114
|
+
assert_equal 0, list.length
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe '#shift' do
|
119
|
+
it 'returns nil if list is empty' do
|
120
|
+
assert_nil list.shift
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'returns node from the top of the list' do
|
124
|
+
list.push(node_1)
|
125
|
+
list.push(node_2)
|
126
|
+
assert_equal node_1.data, list.shift
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'sets #last to nil when all nodes are removed' do
|
130
|
+
list.push(node_1)
|
131
|
+
list.shift
|
132
|
+
assert_nil list.last
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'sets #first to nil when all nodes are removed' do
|
136
|
+
list.push(node_1)
|
137
|
+
list.shift
|
138
|
+
assert_nil list.first
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'reduces list length by 1' do
|
142
|
+
list.push(node_1)
|
143
|
+
list.shift
|
144
|
+
assert_equal 0, list.length
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe '#reverse' do
|
149
|
+
it 'returns new empty list when receiver list is empty' do
|
150
|
+
refute_equal list, list.reverse
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'returns new list in reverse order' do
|
154
|
+
list.push(node_1)
|
155
|
+
refute_equal list.reverse, list.reverse!
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'reverses order of nodes' do
|
159
|
+
list.push(node_1)
|
160
|
+
list.push(node_2)
|
161
|
+
new_list = list.reverse
|
162
|
+
assert_equal %w(bar foo), [new_list.first, new_list.last]
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe '#reverse!' do
|
167
|
+
it 'returns self when list is empty' do
|
168
|
+
assert_equal list, list.reverse!
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'reverses order of nodes' do
|
172
|
+
list.push(node_1)
|
173
|
+
list.push(node_2)
|
174
|
+
list.reverse!
|
175
|
+
assert_equal [node_2.data, node_1.data], [list.first, list.last]
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'returns same object' do
|
179
|
+
list.push(node_1)
|
180
|
+
assert_equal list, list.reverse!
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe '#each' do
|
185
|
+
it 'returns enumerator if no block given' do
|
186
|
+
assert_instance_of Enumerator, list.each
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'pass each node data to the block' do
|
190
|
+
list.push(node_1)
|
191
|
+
list.push(node_2)
|
192
|
+
nodes = []
|
193
|
+
list.each { |e| nodes << e }
|
194
|
+
assert_equal %w(foo bar), nodes
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe 'conversion' do
|
199
|
+
it '#to_list returns self' do
|
200
|
+
assert_equal list, list.to_list
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
data/test/node_test.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe LinkedList::Node do
|
4
|
+
let(:node) { create_node('foo') }
|
5
|
+
|
6
|
+
describe 'instantiation' do
|
7
|
+
it 'assigns data' do
|
8
|
+
assert_equal 'foo', node.data
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'assigns nil to next' do
|
12
|
+
assert_nil node.next
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'accessors' do
|
17
|
+
it '#data' do
|
18
|
+
node.data = 'bar'
|
19
|
+
assert_equal 'bar', node.data
|
20
|
+
end
|
21
|
+
|
22
|
+
it '#next' do
|
23
|
+
node.next = 'bar'
|
24
|
+
assert_equal 'bar', node.next
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'conversion' do
|
29
|
+
it '#to_node returns self' do
|
30
|
+
assert_equal node, node.to_node
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'linked-list'
|
2
|
+
|
3
|
+
gem 'minitest'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'minitest/pride'
|
6
|
+
|
7
|
+
class Minitest::Spec
|
8
|
+
def create_node(*args)
|
9
|
+
LinkedList::Node.new(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_list(*args)
|
13
|
+
LinkedList::List.new(*args)
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linked-list
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yury Velikanau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
- - <=
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '2.0'
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '1.3'
|
44
|
+
- - <=
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: minitest
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '5.0'
|
54
|
+
- - <=
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '6.0'
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '5.0'
|
64
|
+
- - <=
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '6.0'
|
67
|
+
description: Ruby implementation of Linked List, following some Ruby idioms.
|
68
|
+
email:
|
69
|
+
- yury.velikanau@gmail.com
|
70
|
+
executables: []
|
71
|
+
extensions: []
|
72
|
+
extra_rdoc_files: []
|
73
|
+
files:
|
74
|
+
- .gitignore
|
75
|
+
- .travis.yml
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- lib/linked-list.rb
|
81
|
+
- lib/linked-list/conversions.rb
|
82
|
+
- lib/linked-list/list.rb
|
83
|
+
- lib/linked-list/node.rb
|
84
|
+
- lib/linked-list/version.rb
|
85
|
+
- linked-list.gemspec
|
86
|
+
- test/conversions_test.rb
|
87
|
+
- test/list_test.rb
|
88
|
+
- test/node_test.rb
|
89
|
+
- test/test_helper.rb
|
90
|
+
homepage: https://github.com/spectator/linked-list
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.0.14
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Ruby implementation of Linked List, following some Ruby idioms.
|
114
|
+
test_files:
|
115
|
+
- test/conversions_test.rb
|
116
|
+
- test/list_test.rb
|
117
|
+
- test/node_test.rb
|
118
|
+
- test/test_helper.rb
|
119
|
+
has_rdoc:
|