linkedlist 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/linkedlist/version.rb +1 -1
- data/lib/linkedlist.rb +38 -12
- data/lib/node.rb +1 -1
- data/linkedlist.gemspec +1 -1
- data/test/linkedlisttest.rb +17 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1e2b07a8e698f00705addaefb003c15864afe03
|
4
|
+
data.tar.gz: aa0cc4a59285f2627926bfb51e43043c0d16ddc6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88dc5f0824b74a6dfe147586f6fe4eb8db37596b3554ebc1e39bc5fdeaf8ec8fe4e69eaf2f9f83977b31e1c5622a08dc0bdc533f38086577313130eb2ac037fc
|
7
|
+
data.tar.gz: 495c3a63ba671e206f2c1267b2ff77d38543a117e400845574e5bf94b3d2cb7ce8e8d0a9d2f468b2dd7a352538e5d328e7067eb4711b10010fbda5b68d9d90e9
|
data/lib/linkedlist/version.rb
CHANGED
data/lib/linkedlist.rb
CHANGED
@@ -55,7 +55,7 @@ class LinkedList
|
|
55
55
|
#insert an item on end
|
56
56
|
def add(info)
|
57
57
|
_new = Node.new(info, previous: @tail)
|
58
|
-
if @head
|
58
|
+
if @head.nil?
|
59
59
|
@head = _new
|
60
60
|
@tail = @head
|
61
61
|
else
|
@@ -66,6 +66,25 @@ class LinkedList
|
|
66
66
|
self
|
67
67
|
end
|
68
68
|
|
69
|
+
#insert at a certain position
|
70
|
+
def insert_at(index, info)
|
71
|
+
curr = get(index)
|
72
|
+
if !curr.nil?
|
73
|
+
_new = Node.new(info, previous: curr.previous, _next: curr)
|
74
|
+
curr.previous.next = _new if !curr.previous.nil?
|
75
|
+
curr.previous = _new
|
76
|
+
@head = _new if @head == curr
|
77
|
+
@tail = _new if @tail == curr
|
78
|
+
@length += 1
|
79
|
+
else
|
80
|
+
for i in (@length...index)
|
81
|
+
self << nil
|
82
|
+
end
|
83
|
+
add(info)
|
84
|
+
end
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
69
88
|
#get data from it index starting at 0
|
70
89
|
def get(index)
|
71
90
|
dir = index > @length/2? -1: 1
|
@@ -76,7 +95,7 @@ class LinkedList
|
|
76
95
|
def remove(index)
|
77
96
|
dir = index > @length/2? -1: 1
|
78
97
|
curr = go(dir: dir, index: index)
|
79
|
-
if curr
|
98
|
+
if !curr.nil?
|
80
99
|
curr.previous.next = curr.next if !curr.previous.nil?
|
81
100
|
curr.next.previous = curr.previous if !curr.next.nil?
|
82
101
|
@tail = curr.previous if curr == @tail
|
@@ -84,7 +103,7 @@ class LinkedList
|
|
84
103
|
curr.previous, curr.next = nil, nil
|
85
104
|
@length -= 1
|
86
105
|
end
|
87
|
-
curr.info if curr
|
106
|
+
curr.info if !curr.nil?
|
88
107
|
end
|
89
108
|
|
90
109
|
#the first item of the list
|
@@ -100,7 +119,7 @@ class LinkedList
|
|
100
119
|
#go forward
|
101
120
|
def each
|
102
121
|
curr = @head
|
103
|
-
while curr
|
122
|
+
while !curr.nil?
|
104
123
|
yield(curr.info) if block_given?
|
105
124
|
curr = curr.next
|
106
125
|
end
|
@@ -109,7 +128,7 @@ class LinkedList
|
|
109
128
|
#go backwards
|
110
129
|
def reverse_each
|
111
130
|
curr = @tail
|
112
|
-
while curr
|
131
|
+
while !curr.nil?
|
113
132
|
yield(curr.info) if block_given?
|
114
133
|
curr = curr.previous
|
115
134
|
end
|
@@ -119,7 +138,7 @@ class LinkedList
|
|
119
138
|
def to_s
|
120
139
|
s = "["
|
121
140
|
curr = @head
|
122
|
-
while curr
|
141
|
+
while !curr.nil?
|
123
142
|
s << curr.to_s
|
124
143
|
curr = curr.next
|
125
144
|
end
|
@@ -141,19 +160,26 @@ class LinkedList
|
|
141
160
|
#remove all itens
|
142
161
|
def clear
|
143
162
|
@length = 0
|
144
|
-
@next = nil
|
145
|
-
@previous = nil
|
163
|
+
@head.next.previous = nil if !@head.next.nil?
|
164
|
+
@tail.previous.next = nil if !@head.previous.nil?
|
165
|
+
@head = nil
|
166
|
+
@tail = nil
|
146
167
|
self
|
147
168
|
end
|
148
169
|
|
149
170
|
def [](index)
|
150
171
|
i = get(index)
|
151
|
-
i.info if i
|
172
|
+
i.info if !i.nil?
|
152
173
|
end
|
153
174
|
|
154
|
-
def []=(index,
|
175
|
+
def []=(index, info)
|
155
176
|
i = get(index)
|
156
|
-
|
177
|
+
if !i.nil?
|
178
|
+
i.info = info
|
179
|
+
else
|
180
|
+
insert_at(index, info)
|
181
|
+
end
|
182
|
+
info
|
157
183
|
end
|
158
184
|
|
159
185
|
def <<(info)
|
@@ -165,7 +191,7 @@ class LinkedList
|
|
165
191
|
def go(dir: 1, index: nil)
|
166
192
|
curr, i = @head, 0 if dir == 1
|
167
193
|
curr, i = @tail, (@length-1) if dir == -1
|
168
|
-
while curr
|
194
|
+
while !curr.nil?
|
169
195
|
break if i == index
|
170
196
|
curr = curr.next if dir == 1
|
171
197
|
curr = curr.previous if dir == -1
|
data/lib/node.rb
CHANGED
data/linkedlist.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.summary = %q{An iterable doubly linked list}
|
12
12
|
spec.description = %q{An iterable doubly linked list which can be used as a stack, queue or array list}
|
13
13
|
spec.homepage = "https://github.com/javaboybr/linkedlist-ruby"
|
14
|
-
spec.metadata = {
|
14
|
+
spec.metadata = {"issue_tracker" => "https://github.com/javaboybr/linkedlist-ruby/issues"}
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0")
|
data/test/linkedlisttest.rb
CHANGED
@@ -42,4 +42,21 @@ class LinkedListTest < Test::Unit::TestCase
|
|
42
42
|
assert_equal list.remove(1), "string"
|
43
43
|
@log.info "Removed \"string\" List #{list}"
|
44
44
|
end
|
45
|
+
|
46
|
+
def test_insert_at
|
47
|
+
list = LinkedList.new << 1 << 2 << "string" << false << true << nil
|
48
|
+
assert_not_nil list.insert_at(0, -1)
|
49
|
+
@log.info "List #{list}"
|
50
|
+
assert_equal list[0], -1
|
51
|
+
assert_equal list[1], 1
|
52
|
+
assert_not_nil list.insert_at(3, -1)
|
53
|
+
assert_equal list[3], -1
|
54
|
+
assert_equal list[4], "string"
|
55
|
+
assert_not_nil list.insert_at(8, -1)
|
56
|
+
assert_nil list[7]
|
57
|
+
assert_equal list[8], -1
|
58
|
+
assert_not_nil list.insert_at(15, -1)
|
59
|
+
assert_equal list.length, 16
|
60
|
+
@log.info "List #{list}"
|
61
|
+
end
|
45
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linkedlist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thiago Gonzaga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|