ruby_collections 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/lib/ruby_collections/linked_list.rb +9 -6
- data/lib/ruby_collections/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7398054d1b760aabc43207861265e55fe82b4f92
|
4
|
+
data.tar.gz: 691f42570967b022e3f558f8e4713c0b036810c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d23fc58a7e892a2b551745cc9a11c1a0071548a022aeb10b66b9691d9b7615a7cb7f240bc56c0f1c0ac9a4332b05b3d85fcade2acf75b331dc53ab249b5b65d
|
7
|
+
data.tar.gz: 2c8bb1e27007067a9e83ae8950900d9a431261c81b612cfa709cbf80d28e3194196afacf4724aa0c9293ac50c82dc96a4a34985905efdfcba93ebbeee7711fc2
|
data/README.md
CHANGED
@@ -70,7 +70,7 @@ list = RubyCollections::LinkedList.new
|
|
70
70
|
|
71
71
|
list.size # => 0
|
72
72
|
|
73
|
-
list.
|
73
|
+
list.header # => ""
|
74
74
|
|
75
75
|
list.isEmpty? # => true
|
76
76
|
|
@@ -84,10 +84,18 @@ list.add(3,1) # => 3 (number of elements in list)
|
|
84
84
|
|
85
85
|
list.to_s # => "[2, 3, 1]"
|
86
86
|
|
87
|
-
list.remove(2) # => removes element at index 2
|
87
|
+
list.remove(2) # => 2 (removes element at index 2)
|
88
88
|
|
89
89
|
list.to_s # => "[2, 3]"
|
90
90
|
|
91
|
+
list.get(1) # => 3 (returns element at index 1)
|
92
|
+
|
93
|
+
list.get(1).setNext(12) # => adds a new node after index 1 with value 12. list is now [2, 3, 12]
|
94
|
+
|
95
|
+
list.get(1).data = 5 # => changes the value at index 1. list is now [2, 5, 12]
|
96
|
+
|
97
|
+
list.get(1).getNext # => 12
|
98
|
+
|
91
99
|
```
|
92
100
|
|
93
101
|
## Development
|
@@ -7,15 +7,18 @@ module RubyCollections
|
|
7
7
|
@top = nil
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def empty?
|
11
11
|
size.zero?
|
12
12
|
end
|
13
13
|
|
14
|
+
def header
|
15
|
+
@top ? @top.to_s : nil
|
16
|
+
end
|
17
|
+
|
14
18
|
def add(data, index = nil)
|
15
19
|
return nil if index and index >= size
|
16
20
|
if index
|
17
|
-
|
18
|
-
get(index-1).setNext(new_node)
|
21
|
+
get(index-1).setNext(data)
|
19
22
|
else
|
20
23
|
node = Node.new(data, top)
|
21
24
|
@top = node
|
@@ -37,6 +40,7 @@ module RubyCollections
|
|
37
40
|
end
|
38
41
|
|
39
42
|
def to_s
|
43
|
+
return "" if empty?
|
40
44
|
data = []
|
41
45
|
data << (node = top).data
|
42
46
|
(size-1).times {data << (node = node.getNext).data}
|
@@ -55,8 +59,8 @@ module RubyCollections
|
|
55
59
|
ObjectSpace._id2ref(@next)
|
56
60
|
end
|
57
61
|
|
58
|
-
def setNext(
|
59
|
-
|
62
|
+
def setNext(data)
|
63
|
+
node = Node.new(data, nil)
|
60
64
|
next_node_id = instance_variable_get(:@next)
|
61
65
|
@next = node.object_id
|
62
66
|
node.instance_variable_set(:@next, next_node_id)
|
@@ -69,5 +73,4 @@ module RubyCollections
|
|
69
73
|
end
|
70
74
|
|
71
75
|
end
|
72
|
-
|
73
76
|
end
|