ruby_collections 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -1
- data/lib/ruby_collections/collections.rb +2 -1
- data/lib/ruby_collections/linked_list.rb +73 -0
- data/lib/ruby_collections/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 409e6c00234df5a85788da2274f54e15b8eb8d43
|
4
|
+
data.tar.gz: 82b5595664dd34d7e9dc835c5c14360a60cb9734
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7379fdab9b3e7d305ef34ccf19b55f137c7f9bc953caa6eae409222657ff5dcb23c9e279295cb88f6c2c91cff98ff4f60369ecb0991ba9c029a946ebbcc44041
|
7
|
+
data.tar.gz: 552ac15b5b92f7f852858e71c256fd3e2676a7f45cb1ce0c2792007de842080e5eb88a82f976e9dd97bb1846b7728560c4939d99c4db3372529ad0f1021e3797
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
Supported Data Structures: Max Heap, Min Heap
|
23
|
+
Supported Data Structures: Max Heap, Min Heap, Stack, LinkedList
|
24
24
|
|
25
25
|
### RubyCollections::MaxHeap
|
26
26
|
|
@@ -62,6 +62,34 @@ stack.top # => 1
|
|
62
62
|
|
63
63
|
```
|
64
64
|
|
65
|
+
### RubyCollections::LinkedList
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
|
69
|
+
list = RubyCollections::LinkedList.new
|
70
|
+
|
71
|
+
list.size # => 0
|
72
|
+
|
73
|
+
list.top # => nil
|
74
|
+
|
75
|
+
list.isEmpty? # => true
|
76
|
+
|
77
|
+
list.add(1) # => 1 (number of elements in list)
|
78
|
+
|
79
|
+
list.add(2) # => 2 (number of elements in list)
|
80
|
+
|
81
|
+
list.to_s # => "[2, 1]"
|
82
|
+
|
83
|
+
list.add(3,1) # => 3 (number of elements in list)
|
84
|
+
|
85
|
+
list.to_s # => "[2, 3, 1]"
|
86
|
+
|
87
|
+
list.remove(2) # => removes element at index 2
|
88
|
+
|
89
|
+
list.to_s # => "[2, 3]"
|
90
|
+
|
91
|
+
```
|
92
|
+
|
65
93
|
## Development
|
66
94
|
|
67
95
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module RubyCollections
|
2
|
+
class LinkedList
|
3
|
+
attr_accessor :size, :top
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@size = 0
|
7
|
+
@top = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def isEmpty?
|
11
|
+
size.zero?
|
12
|
+
end
|
13
|
+
|
14
|
+
def add(data, index = nil)
|
15
|
+
return nil if index and index >= size
|
16
|
+
if index
|
17
|
+
new_node = Node.new data, nil
|
18
|
+
get(index-1).setNext(new_node)
|
19
|
+
else
|
20
|
+
node = Node.new(data, top)
|
21
|
+
@top = node
|
22
|
+
end
|
23
|
+
@size += 1
|
24
|
+
end
|
25
|
+
|
26
|
+
def get(index)
|
27
|
+
node = top
|
28
|
+
index.times {node = node.getNext}
|
29
|
+
return node
|
30
|
+
end
|
31
|
+
|
32
|
+
def remove(index)
|
33
|
+
node = get(index-1)
|
34
|
+
to_be_removed = node.getNext
|
35
|
+
node.setNext(to_be_removed.getNext)
|
36
|
+
@size -= 1
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_s
|
40
|
+
data = []
|
41
|
+
data << (node = top).data
|
42
|
+
(size-1).times {data << (node = node.getNext).data}
|
43
|
+
return data.to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
class Node
|
47
|
+
attr_accessor :data
|
48
|
+
|
49
|
+
def initialize(data, next_node)
|
50
|
+
@data = data
|
51
|
+
@next = next_node.object_id
|
52
|
+
end
|
53
|
+
|
54
|
+
def getNext
|
55
|
+
ObjectSpace._id2ref(@next)
|
56
|
+
end
|
57
|
+
|
58
|
+
def setNext(node)
|
59
|
+
return if node.nil?
|
60
|
+
next_node_id = instance_variable_get(:@next)
|
61
|
+
@next = node.object_id
|
62
|
+
node.instance_variable_set(:@next, next_node_id)
|
63
|
+
return node
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_s
|
67
|
+
"#{data}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_collections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vikash Vikram
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- bin/setup
|
59
59
|
- lib/ruby_collections.rb
|
60
60
|
- lib/ruby_collections/collections.rb
|
61
|
+
- lib/ruby_collections/linked_list.rb
|
61
62
|
- lib/ruby_collections/max_heap.rb
|
62
63
|
- lib/ruby_collections/min_heap.rb
|
63
64
|
- lib/ruby_collections/stack.rb
|