linked_list_sourav 0.0.14 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/Gemfile.lock +32 -0
- data/README.md +34 -0
- data/Rakefile +2 -0
- data/lib/linked_list_sourav.rb +2 -2
- data/linked_list.gemspec +15 -0
- data/spec/linked_list_spec.rb +219 -0
- data/spec/spec_helper.rb +10 -0
- metadata +28 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5db64298ddfe099ff10ef887815c85e5442b07b
|
4
|
+
data.tar.gz: 2529b989a7fceded76e6de1b69dc3a996cba1181
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f995c5a6dac0cce0bdb345aca9c81516a3a09b46e51a4bfbeb74f53690e50f646114244bb0d181849fb256f6f5c1a8c2128795ab34fba26c73da315792961e74
|
7
|
+
data.tar.gz: db979ffea9a6ff5ae85657e1c83fd3858719d31a0d6d72d9c3b044e7b251a8a4a04d90d34ba6a5aa63096a8888fbd31b93ec0c87bfec576abe886c12b73e08c2
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
linked_list_sourav (0.0.14)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
rspec (3.4.0)
|
11
|
+
rspec-core (~> 3.4.0)
|
12
|
+
rspec-expectations (~> 3.4.0)
|
13
|
+
rspec-mocks (~> 3.4.0)
|
14
|
+
rspec-core (3.4.4)
|
15
|
+
rspec-support (~> 3.4.0)
|
16
|
+
rspec-expectations (3.4.0)
|
17
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
18
|
+
rspec-support (~> 3.4.0)
|
19
|
+
rspec-mocks (3.4.1)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.4.0)
|
22
|
+
rspec-support (3.4.1)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
linked_list_sourav!
|
29
|
+
rspec
|
30
|
+
|
31
|
+
BUNDLED WITH
|
32
|
+
1.11.2
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Linked List
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/linked_list_sourav.svg)](http://badge.fury.io/rb/linked_list_sourav)
|
3
|
+
This is linked list implementation both singly and doubly in ruby. Its main purpose is demonstration.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require "linked_list_sourav"
|
9
|
+
list = LinkedList::Singly.new(1) # intializes a new list
|
10
|
+
list.head # returns head node
|
11
|
+
list.add(2) # adds a new node
|
12
|
+
3.upto(20) { |data| list.add(data)}
|
13
|
+
list.find(10) # finds and return the node that contains data as 10
|
14
|
+
list.delete(6) # removes the node that contain 6 as data
|
15
|
+
list.add_a([21, 22, 23, 24]) #adds array members directly
|
16
|
+
list = LinkedList::Singly.new([1, 2, 3, 4]) # intialize with an array
|
17
|
+
list.parse # prints all the data from the list
|
18
|
+
list.to_a # prints an array of all data
|
19
|
+
list.to_s #prints an string of all data
|
20
|
+
list.no_of_nodes # returns no of nodes present in the linked list
|
21
|
+
|
22
|
+
### The same for Doubly Linked list ###
|
23
|
+
|
24
|
+
list = LinkedList::Doubly.new(1) # intializes a new list
|
25
|
+
list.head # returns head node
|
26
|
+
list.add(2) # adds a new node
|
27
|
+
3.upto(20) { |data| list.add(data)}
|
28
|
+
list.find(10) # finds and return the node that contains data as 10
|
29
|
+
list.delete(6) # removes the node that contain 6 as data
|
30
|
+
list = LinkedList::Doubly.new([1, 2, 3, 4]) # intialize with an array
|
31
|
+
list.parse # prints all the data from the list
|
32
|
+
list.to_a # prints an array of all data
|
33
|
+
list.to_s #prints an string of all data
|
34
|
+
```
|
data/Rakefile
ADDED
data/lib/linked_list_sourav.rb
CHANGED
@@ -35,13 +35,13 @@ class LinkedList
|
|
35
35
|
|
36
36
|
class Singly
|
37
37
|
def initialize(data = nil) # constructor
|
38
|
+
@count = 1
|
38
39
|
if data.class == Array
|
39
40
|
@head = Node.new(data[0])
|
40
41
|
data.each.with_index { |datum, index| self.add(datum) if index > 0}
|
41
42
|
else
|
42
43
|
@head = Node.new(data)
|
43
44
|
end
|
44
|
-
@count = 1
|
45
45
|
self
|
46
46
|
end
|
47
47
|
|
@@ -142,13 +142,13 @@ class LinkedList
|
|
142
142
|
|
143
143
|
class Doubly < Singly
|
144
144
|
def initialize(data) # intializes
|
145
|
+
@count = 1
|
145
146
|
if data.respond_to? :each
|
146
147
|
@head = DoublyNode.new(data[0])
|
147
148
|
data.each.with_index { |datum, index| self.add(datum) if index > 0}
|
148
149
|
else
|
149
150
|
@head = DoublyNode.new(data)
|
150
151
|
end
|
151
|
-
@count = 1
|
152
152
|
self
|
153
153
|
end
|
154
154
|
|
data/linked_list.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'linked_list_sourav'
|
3
|
+
s.version = '0.1.1'
|
4
|
+
s.date = '2016-05-17'
|
5
|
+
s.required_ruby_version = '>= 2.1.5'
|
6
|
+
s.summary = "Linked list library for ruby"
|
7
|
+
s.description = "A simple linked list gem"
|
8
|
+
s.authors = ["Sourav Moitra"]
|
9
|
+
s.email = 'sourav.moitr@gmail.con'
|
10
|
+
s.homepage = 'https://github.com/xw19/linked_list'
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.files = `git ls-files -z`.split("\x0")
|
13
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
14
|
+
s.add_development_dependency('rspec', '~> 3')
|
15
|
+
end
|
@@ -0,0 +1,219 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe LinkedList::Node do
|
4
|
+
describe "init" do
|
5
|
+
let(:node2) { described_class.new(2) }
|
6
|
+
let(:node) { described_class.new(1, node2) }
|
7
|
+
|
8
|
+
it "assigns data" do
|
9
|
+
expect(node.data).to eq 1
|
10
|
+
end
|
11
|
+
|
12
|
+
it "assigns forward" do
|
13
|
+
expect(node.forward).to eq node2
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#to_s" do
|
19
|
+
context "when single node exit" do
|
20
|
+
it "returns string with data" do
|
21
|
+
node = described_class.new 1
|
22
|
+
expect(node.to_s).to eq "Data: 1 "
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when next node exist" do
|
27
|
+
it "returns string with forward data when exist" do
|
28
|
+
node = described_class.new(1, described_class.new(2))
|
29
|
+
expect(node.to_s).to eq "Data: 1 Points to: 2"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe LinkedList::DoublyNode do
|
36
|
+
let(:node1) { described_class.new(1) }
|
37
|
+
let(:node3) { described_class.new(3) }
|
38
|
+
let(:node) { described_class.new(2, node1, node3) }
|
39
|
+
|
40
|
+
describe "init" do
|
41
|
+
it "assigns node data" do
|
42
|
+
expect(node.data).to eq 2
|
43
|
+
end
|
44
|
+
|
45
|
+
it "assigns node forward" do
|
46
|
+
expect(node.backward).to eq node1
|
47
|
+
end
|
48
|
+
|
49
|
+
it "assigns node backward" do
|
50
|
+
expect(node.forward).to eq node3
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#to_s" do
|
55
|
+
context "when single node" do
|
56
|
+
it "returns a string with data" do
|
57
|
+
node = described_class.new "single node"
|
58
|
+
expect(node.to_s).to eq "Data: single node "
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when backward nodes exist" do
|
63
|
+
it "returns a string with forward data when exist" do
|
64
|
+
node = described_class.new(2, node1)
|
65
|
+
expect(node.to_s).to eq "Data: 2 Pointed by: 1"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when backward & forward nodes exist" do
|
70
|
+
it "returns a string with all pointer reference" do
|
71
|
+
expect(node.to_s).to eq "Data: 2 Points to: 3 Pointed by: 1"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe LinkedList::Singly do
|
78
|
+
describe "init" do
|
79
|
+
context "when single node" do
|
80
|
+
it do
|
81
|
+
singly = described_class.new(1)
|
82
|
+
expect(singly.head.data).to eq 1
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "when multiple nodes" do
|
87
|
+
it do
|
88
|
+
arr = [1, 2, 3]
|
89
|
+
singly = described_class.new(arr)
|
90
|
+
expect(singly.head.data).to eq 1
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
let(:singly) { described_class.new([1, 2, 3])}
|
96
|
+
|
97
|
+
describe "#head" do
|
98
|
+
it "returns instance of LinkedList::Node" do
|
99
|
+
expect(singly.head).to be_instance_of LinkedList::Node
|
100
|
+
end
|
101
|
+
|
102
|
+
it "returns data" do
|
103
|
+
expect(singly.head.data).to eq 1
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "#parse" do
|
108
|
+
specify { expect { singly.parse }.to output("1\n2\n3\n").to_stdout }
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "#find" do
|
112
|
+
it do
|
113
|
+
node = singly.find(1)
|
114
|
+
expect(node.data).to eq 1
|
115
|
+
expect(node.forward.data).to eq 2
|
116
|
+
end
|
117
|
+
|
118
|
+
it do
|
119
|
+
node = singly.find(2)
|
120
|
+
expect(node.data).to eq 2
|
121
|
+
expect(node.forward.data).to eq 3
|
122
|
+
end
|
123
|
+
|
124
|
+
it do
|
125
|
+
node = singly.find(3)
|
126
|
+
expect(node.data).to eq 3
|
127
|
+
expect(node.forward).to be nil
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "#no_of_nodes" do
|
132
|
+
it do
|
133
|
+
expect(singly.no_of_nodes).to eq 3
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "#add" do
|
138
|
+
it "add no_of_nodes by 1" do
|
139
|
+
expect{ singly.add(4) }.to change { singly.no_of_nodes }.by(1)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe "#add_a" do
|
144
|
+
it "accepts array and add no_of_nodes by 3" do
|
145
|
+
expect{ singly.add_a([4, 5, 6]) }.to change { singly.no_of_nodes }.by(3)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "#edit" do
|
150
|
+
it do
|
151
|
+
expect(singly.find(3).data).to_not be_nil
|
152
|
+
singly.edit(3, 9)
|
153
|
+
expect(singly.find(3)).to be_nil
|
154
|
+
expect(singly.find(9).data).to eq 9
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe "#to_a" do
|
159
|
+
it "returns array" do
|
160
|
+
expect(singly.to_a).to eq([1, 2, 3])
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe "#to_s" do
|
165
|
+
it do
|
166
|
+
expect(singly.to_s).to eq [1, 2, 3].join(", ")
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "#delete" do
|
171
|
+
it "remove single node" do
|
172
|
+
expect { singly.delete(1) }.to change { singly.no_of_nodes }.by(-1)
|
173
|
+
end
|
174
|
+
|
175
|
+
it "remove any node that match data" do
|
176
|
+
expect(singly.find(2)).to_not be_nil
|
177
|
+
singly.delete(2)
|
178
|
+
expect(singly.find(2)).to be_nil
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe LinkedList::Doubly do
|
184
|
+
describe "init" do
|
185
|
+
context "when single node" do
|
186
|
+
it do
|
187
|
+
singly = described_class.new(1)
|
188
|
+
expect(singly.head.data).to eq 1
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
context "when multiple nodes" do
|
193
|
+
it do
|
194
|
+
arr = [1, 2, 3]
|
195
|
+
singly = described_class.new(arr)
|
196
|
+
expect(singly.head.data).to eq 1
|
197
|
+
expect(singly.head.forward.data).to eq 2
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
let(:doubly) { described_class.new([1, 2 ,3]) }
|
203
|
+
|
204
|
+
describe "#add" do
|
205
|
+
it "adds no of nodes" do
|
206
|
+
expect(doubly.no_of_nodes).to eq 3
|
207
|
+
expect { doubly.add(4) }.to change { doubly.no_of_nodes }.by(1)
|
208
|
+
expect(doubly.no_of_nodes).to eq 4
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "#delete" do
|
213
|
+
it "removes a node" do
|
214
|
+
expect(doubly.find(2)).to_not be_nil
|
215
|
+
doubly.delete(2)
|
216
|
+
expect(doubly.find(2)).to be_nil
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,22 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linked_list_sourav
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sourav Moitra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2016-05-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
13
27
|
description: A simple linked list gem
|
14
28
|
email: sourav.moitr@gmail.con
|
15
29
|
executables: []
|
16
30
|
extensions: []
|
17
31
|
extra_rdoc_files: []
|
18
32
|
files:
|
33
|
+
- Gemfile
|
34
|
+
- Gemfile.lock
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
19
37
|
- lib/linked_list_sourav.rb
|
38
|
+
- linked_list.gemspec
|
39
|
+
- spec/linked_list_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
20
41
|
homepage: https://github.com/xw19/linked_list
|
21
42
|
licenses:
|
22
43
|
- MIT
|
@@ -37,8 +58,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
58
|
version: '0'
|
38
59
|
requirements: []
|
39
60
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
61
|
+
rubygems_version: 2.5.1
|
41
62
|
signing_key:
|
42
63
|
specification_version: 4
|
43
64
|
summary: Linked list library for ruby
|
44
|
-
test_files:
|
65
|
+
test_files:
|
66
|
+
- spec/linked_list_spec.rb
|
67
|
+
- spec/spec_helper.rb
|