linkedlist 0.0.6
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +8 -0
- data/lib/linkedlist/version.rb +3 -0
- data/lib/linkedlist.rb +178 -0
- data/lib/node.rb +17 -0
- data/linkedlist.gemspec +24 -0
- data/test/linkedlisttest.rb +45 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3b525c12d9753f530e2c319c8e2c899c38b1e089
|
4
|
+
data.tar.gz: 2ed63443c9df98670eafded4d0f190239c5dab6c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3af87974c55957c7e8082b44148beff135a3d46faa2e99183a0ee1ca3a6e1aebac5451db23288d51af2c2e129f352db45debff41129bc4428f4114077544cbd6
|
7
|
+
data.tar.gz: ebabce6efed40866108147fb9ed7da7948b52b56f20f75956678bd85c7ae0a8b2bb6e518084b2d8f11f0a8f736ae303ee9cfc2811f5e7c0816d160d6208e5874
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 javaboybr
|
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,43 @@
|
|
1
|
+
# LinkedList
|
2
|
+
|
3
|
+
Simple implementation of doubly linked list data type in ruby
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'linkedlist'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install linkedlist
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
list = LinkedList.new
|
25
|
+
#adding some data, it could be whatever you want
|
26
|
+
list << "a string" << 1 << nil << false << Time.new
|
27
|
+
#it can be a stack, adding at the top
|
28
|
+
list.push "top of the stack"
|
29
|
+
#removing from the top
|
30
|
+
list.pop
|
31
|
+
#it can be a queue, adding on end
|
32
|
+
list.enq "Last in"
|
33
|
+
#removing the first
|
34
|
+
list.deq
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it ( https://github.com/javaboybr/linkedlist-ruby/fork )
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/linkedlist.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require "linkedlist/version"
|
2
|
+
require "node"
|
3
|
+
|
4
|
+
#A simple implementation of doubly linked list data type
|
5
|
+
#
|
6
|
+
#Autor:: Thiago Gonzaga
|
7
|
+
#License:: MIT
|
8
|
+
class LinkedList
|
9
|
+
|
10
|
+
#Creates a new instance of a list
|
11
|
+
# * length
|
12
|
+
# * initial data
|
13
|
+
def initialize(length=0, item=nil)
|
14
|
+
@tail = nil
|
15
|
+
@head = nil
|
16
|
+
@length = 0
|
17
|
+
for i in (1..length)
|
18
|
+
self << item
|
19
|
+
yield(item) if block_given?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
#retorna o tamanho da lista
|
24
|
+
def length
|
25
|
+
@length
|
26
|
+
end
|
27
|
+
|
28
|
+
#Alias for length
|
29
|
+
#alias:: length()
|
30
|
+
def size
|
31
|
+
@length
|
32
|
+
end
|
33
|
+
|
34
|
+
#Insert data on end
|
35
|
+
#alias:: add()
|
36
|
+
def push(info)
|
37
|
+
add(info)
|
38
|
+
end
|
39
|
+
|
40
|
+
#remove the last item
|
41
|
+
def pop
|
42
|
+
remove(@length - 1)
|
43
|
+
end
|
44
|
+
|
45
|
+
#enqueue an item on end
|
46
|
+
def enq(info)
|
47
|
+
add(info)
|
48
|
+
end
|
49
|
+
|
50
|
+
#dequeue the first item
|
51
|
+
def deq
|
52
|
+
remove(0)
|
53
|
+
end
|
54
|
+
|
55
|
+
#insert an item on end
|
56
|
+
def add(info)
|
57
|
+
_new = Node.new(info, previous: @tail)
|
58
|
+
if @head == nil
|
59
|
+
@head = _new
|
60
|
+
@tail = @head
|
61
|
+
else
|
62
|
+
@tail.next = _new
|
63
|
+
@tail = _new
|
64
|
+
end
|
65
|
+
@length += 1
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
#get data from it index starting at 0
|
70
|
+
def get(index)
|
71
|
+
dir = index > @length/2? -1: 1
|
72
|
+
go(dir: dir, index: index)
|
73
|
+
end
|
74
|
+
|
75
|
+
#remove date by it index
|
76
|
+
def remove(index)
|
77
|
+
dir = index > @length/2? -1: 1
|
78
|
+
curr = go(dir: dir, index: index)
|
79
|
+
if curr != nil
|
80
|
+
curr.previous.next = curr.next if !curr.previous.nil?
|
81
|
+
curr.next.previous = curr.previous if !curr.next.nil?
|
82
|
+
@tail = curr.previous if curr == @tail
|
83
|
+
@head = curr.next if curr == @head
|
84
|
+
curr.previous, curr.next = nil, nil
|
85
|
+
@length -= 1
|
86
|
+
end
|
87
|
+
curr.info if curr != nil
|
88
|
+
end
|
89
|
+
|
90
|
+
#the first item of the list
|
91
|
+
def first
|
92
|
+
@head.info
|
93
|
+
end
|
94
|
+
|
95
|
+
#the last item of the list
|
96
|
+
def last
|
97
|
+
@tail.info
|
98
|
+
end
|
99
|
+
|
100
|
+
#go forward
|
101
|
+
def each
|
102
|
+
curr = @head
|
103
|
+
while curr != nil
|
104
|
+
yield(curr.info) if block_given?
|
105
|
+
curr = curr.next
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
#go backwards
|
110
|
+
def reverse_each
|
111
|
+
curr = @tail
|
112
|
+
while curr != nil
|
113
|
+
yield(curr.info) if block_given?
|
114
|
+
curr = curr.previous
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
#Convert to a string
|
119
|
+
def to_s
|
120
|
+
s = "["
|
121
|
+
curr = @head
|
122
|
+
while curr != nil
|
123
|
+
s << curr.to_s
|
124
|
+
curr = curr.next
|
125
|
+
end
|
126
|
+
s << "]"
|
127
|
+
end
|
128
|
+
|
129
|
+
#Convert to an array
|
130
|
+
def to_a
|
131
|
+
arr = []
|
132
|
+
each {|valor| arr << valor}
|
133
|
+
arr
|
134
|
+
end
|
135
|
+
|
136
|
+
#verify if the list is empty
|
137
|
+
def empty?
|
138
|
+
@length == 0
|
139
|
+
end
|
140
|
+
|
141
|
+
#remove all itens
|
142
|
+
def clear
|
143
|
+
@length = 0
|
144
|
+
@next = nil
|
145
|
+
@previous = nil
|
146
|
+
self
|
147
|
+
end
|
148
|
+
|
149
|
+
def [](index)
|
150
|
+
i = get(index)
|
151
|
+
i.info if i != nil
|
152
|
+
end
|
153
|
+
|
154
|
+
def []=(index, value)
|
155
|
+
i = get(index)
|
156
|
+
i.info = value if i != nil
|
157
|
+
end
|
158
|
+
|
159
|
+
def <<(info)
|
160
|
+
add(info)
|
161
|
+
end
|
162
|
+
|
163
|
+
#go through the list back and fourth
|
164
|
+
private
|
165
|
+
def go(dir: 1, index: nil)
|
166
|
+
curr, i = @head, 0 if dir == 1
|
167
|
+
curr, i = @tail, (@length-1) if dir == -1
|
168
|
+
while curr != nil
|
169
|
+
break if i == index
|
170
|
+
curr = curr.next if dir == 1
|
171
|
+
curr = curr.previous if dir == -1
|
172
|
+
i += 1 if dir == 1
|
173
|
+
i -= 1 if dir == -1
|
174
|
+
end
|
175
|
+
curr
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
data/lib/node.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class Node
|
2
|
+
|
3
|
+
attr_accessor :info, :next, :previous
|
4
|
+
|
5
|
+
def initialize(info, _next: nil, previous: nil)
|
6
|
+
@info = info
|
7
|
+
@next = _next
|
8
|
+
@previous = previous
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
s = (info == nil)? "nil": "#{@info}"
|
13
|
+
s << ", " if @next != nil
|
14
|
+
s
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/linkedlist.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 'linkedlist/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "linkedlist"
|
8
|
+
spec.version = Linkedlist::VERSION
|
9
|
+
spec.authors = ["Thiago Gonzaga"]
|
10
|
+
spec.email = ["thi_gonzaga@yahoo.com.br"]
|
11
|
+
spec.summary = %q{An iterable doubly linked list}
|
12
|
+
spec.description = %q{An iterable doubly linked list which can be used as a stack, queue or array list}
|
13
|
+
spec.homepage = "https://github.com/javaboybr/linkedlist-ruby"
|
14
|
+
spec.metadata = { "issue_tracker" => "https://github.com/javaboybr/linkedlist-ruby/issues"}
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'logger'
|
3
|
+
require_relative '../lib/linkedlist'
|
4
|
+
|
5
|
+
class LinkedListTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@log = Logger.new(STDOUT)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_instance
|
12
|
+
list = LinkedList.new(1,"test")
|
13
|
+
assert_equal 1, list.length
|
14
|
+
assert_not_nil list << 1 << 2 << "string" << false << true << nil
|
15
|
+
for i in list
|
16
|
+
@log.info "Object type: #{i.class}"
|
17
|
+
end
|
18
|
+
@log.info "List length: #{list.length}"
|
19
|
+
assert_nil list[list.length-1]
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_empty
|
23
|
+
list = LinkedList.new
|
24
|
+
assert list.empty?
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_not_found_index
|
28
|
+
list = LinkedList.new
|
29
|
+
assert_nil list[1000]
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_remove
|
33
|
+
list = LinkedList.new << 1 << 2 << "string" << false << true << nil
|
34
|
+
assert_equal list.length, 6
|
35
|
+
@log.info "List length: #{list.length} content: #{list}"
|
36
|
+
assert_nil list.pop
|
37
|
+
@log.info "Removed last from List #{list}"
|
38
|
+
assert list.remove(list.length-1)
|
39
|
+
@log.info "Removed last from List #{list}"
|
40
|
+
assert_equal list.deq, 1
|
41
|
+
@log.info "Removed first from List #{list}"
|
42
|
+
assert_equal list.remove(1), "string"
|
43
|
+
@log.info "Removed \"string\" List #{list}"
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linkedlist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thiago Gonzaga
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: An iterable doubly linked list which can be used as a stack, queue or
|
42
|
+
array list
|
43
|
+
email:
|
44
|
+
- thi_gonzaga@yahoo.com.br
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/linkedlist.rb
|
55
|
+
- lib/linkedlist/version.rb
|
56
|
+
- lib/node.rb
|
57
|
+
- linkedlist.gemspec
|
58
|
+
- test/linkedlisttest.rb
|
59
|
+
homepage: https://github.com/javaboybr/linkedlist-ruby
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata:
|
63
|
+
issue_tracker: https://github.com/javaboybr/linkedlist-ruby/issues
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.0.14
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: An iterable doubly linked list
|
84
|
+
test_files:
|
85
|
+
- test/linkedlisttest.rb
|