doubly_linkedlist 0.1.0 → 0.2.0
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/doubly_linkedlist.gemspec +3 -2
- data/lib/doubly_linkedlist.rb +5 -2
- data/lib/doubly_linkedlist/list.rb +34 -6
- data/lib/doubly_linkedlist/node.rb +7 -0
- data/lib/doubly_linkedlist/version.rb +2 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fe67884195d2f09260a6a101925e79f8c1623866484e4b4de63d47e420b8f90
|
4
|
+
data.tar.gz: b6f40e44fcc95f1506198f9d5f97e285b841b5e522c6f31f40d14081fa4dade9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 631816dbbab742313398e44be5a8fe77ddab8dec76152ce3ab6059f7809ac3932283bd27a4fe3ff58476a15f795fda65a6daa45cfbef8386d4771d6803e6794c
|
7
|
+
data.tar.gz: a43529f9173dcea56fda8f5322f07f9b44ebd5897ead5e9dc113c482d11fa792a89b886baabaf85b29beb957d4b3ebf274d157fea01344961efbca898994b20e
|
data/doubly_linkedlist.gemspec
CHANGED
@@ -8,13 +8,14 @@ Gem::Specification.new do |spec|
|
|
8
8
|
|
9
9
|
spec.summary = %q{Ruby implementation of a Doubly Linkedlist data structure.}
|
10
10
|
spec.description = %q{Doubly Linkedlist with faster and minimal interface.}
|
11
|
-
spec.homepage = "https://github.com/anshabmk/doubly_linkedlist"
|
11
|
+
spec.homepage = "https://github.com/anshabmk/doubly_linkedlist/blob/master/README.md"
|
12
12
|
spec.license = "MIT"
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
14
|
|
15
15
|
spec.metadata["homepage_uri"] = spec.homepage
|
16
16
|
spec.metadata["source_code_uri"] = "https://github.com/anshabmk/doubly_linkedlist"
|
17
|
-
spec.metadata["
|
17
|
+
spec.metadata["documentation_uri"] = "https://rubydoc.info/gems/doubly_linkedlist"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/anshabmk/doubly_linkedlist/releases"
|
18
19
|
|
19
20
|
# Specify which files should be added to the gem when it is released.
|
20
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
data/lib/doubly_linkedlist.rb
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
require "doubly_linkedlist/version"
|
2
2
|
|
3
|
+
# Namespace for DoublyLinkedlist
|
3
4
|
module DoublyLinkedlist
|
5
|
+
# Standard Error class
|
4
6
|
class Error < StandardError; end
|
5
|
-
# Your code goes here...
|
6
7
|
end
|
7
8
|
|
8
9
|
require 'doubly_linkedlist/list'
|
9
10
|
|
10
11
|
# Monkey patching a method to_list into the Array class.
|
11
|
-
# Usage: ary.to_list -> list
|
12
12
|
class Array
|
13
|
+
# Converts a given array into a linkedlist.
|
14
|
+
#
|
15
|
+
# @return [DoublyLinkedlist::List] the linkedlist object with array elements as node values.
|
13
16
|
def to_list
|
14
17
|
DoublyLinkedlist::List.new(self)
|
15
18
|
end
|
@@ -3,17 +3,23 @@
|
|
3
3
|
require 'doubly_linkedlist/node'
|
4
4
|
|
5
5
|
module DoublyLinkedlist
|
6
|
+
# Creates list with nodes
|
6
7
|
class List
|
8
|
+
# @return [Integer] the number of nodes in the list.
|
7
9
|
attr_reader :count
|
8
10
|
|
9
|
-
#
|
11
|
+
# Initializes the object with nodes having the values passed in
|
10
12
|
# the optional argument array.
|
13
|
+
#
|
14
|
+
# @param values [Array] the values of nodes
|
11
15
|
def initialize(values = [])
|
12
16
|
@count = 0
|
13
17
|
build_list_from_array(values)
|
14
18
|
end
|
15
19
|
|
16
20
|
# Returns the value at the head of the list if present, nil otherwise.
|
21
|
+
#
|
22
|
+
# @return [Object, Nil] the node value object or nil.
|
17
23
|
def head
|
18
24
|
@head.value if @head
|
19
25
|
end
|
@@ -21,6 +27,8 @@ module DoublyLinkedlist
|
|
21
27
|
alias :first :head
|
22
28
|
|
23
29
|
# Returns the value at the tail of the list if present, nil otherwise.
|
30
|
+
#
|
31
|
+
# @return [Object, Nil] the node value object or nil.
|
24
32
|
def tail
|
25
33
|
@tail.value if @tail
|
26
34
|
end
|
@@ -28,7 +36,9 @@ module DoublyLinkedlist
|
|
28
36
|
alias :last :tail
|
29
37
|
|
30
38
|
# Returns the node object at the given index if present, nil otherwise.
|
31
|
-
#
|
39
|
+
#
|
40
|
+
# @param index [Integer] the index where to look up.
|
41
|
+
# @return [Object, Nil] the value at the given index, or nil if index is out of range.
|
32
42
|
def find_at(index)
|
33
43
|
return if (index + 1) > count
|
34
44
|
|
@@ -39,20 +49,27 @@ module DoublyLinkedlist
|
|
39
49
|
|
40
50
|
# Returns the leftmost index of the value present in the list.
|
41
51
|
# Returns nil if the value is not present.
|
42
|
-
#
|
52
|
+
#
|
53
|
+
# @param value [Object] the node value for which to be looked up.
|
54
|
+
# @return [Integer, Nil] the index of the value passed in, or nil if value is not present.
|
43
55
|
def index(value)
|
44
56
|
find_index(0, @head, :next, 1)
|
45
57
|
end
|
46
58
|
|
47
59
|
# Returns the rightmost index of value present in the list.
|
48
60
|
# Returns nil if the value is not present.
|
49
|
-
#
|
61
|
+
#
|
62
|
+
# @param value [Object] the node value for which to be looked up.
|
63
|
+
# @return [Integer, Nil] the index of the value passed in, or nil if value is not present.
|
50
64
|
def rindex(value)
|
51
65
|
find_index(count - 1, @tail, :prev, -1)
|
52
66
|
end
|
53
67
|
|
54
68
|
# Inserts a node with the given value into the head of the list,
|
55
69
|
# increments and returns the count of nodes.
|
70
|
+
#
|
71
|
+
# @param value [Object] the node value to be inserted into the list.
|
72
|
+
# @return [Integer] the number of nodes after insertion.
|
56
73
|
def insert(value)
|
57
74
|
new_node = Node.new(value)
|
58
75
|
@head.prev = new_node if @head
|
@@ -66,6 +83,9 @@ module DoublyLinkedlist
|
|
66
83
|
|
67
84
|
# Inserts a node with the given value into the tail of the list,
|
68
85
|
# increments and returns the count of nodes.
|
86
|
+
#
|
87
|
+
# @param value [Object] the node value to be enqueued into the list.
|
88
|
+
# @return [Integer] the number of nodes after insertion.
|
69
89
|
def enqueue(value)
|
70
90
|
new_node = Node.new(value)
|
71
91
|
@head = new_node unless @head
|
@@ -78,7 +98,9 @@ module DoublyLinkedlist
|
|
78
98
|
# Deletes the node at a given index and returns the value present in the
|
79
99
|
# deleted node.
|
80
100
|
# Returns nil if the given index is out of range.
|
81
|
-
#
|
101
|
+
#
|
102
|
+
# @param index [Integer] the index at which node has to be deleted.
|
103
|
+
# @return [Object] the deleted node value.
|
82
104
|
def delete_at(index)
|
83
105
|
return if (index + 1) > count
|
84
106
|
|
@@ -99,7 +121,9 @@ module DoublyLinkedlist
|
|
99
121
|
deleted.value
|
100
122
|
end
|
101
123
|
|
102
|
-
# Pops out and returns the node_value at head.
|
124
|
+
# Pops out and returns the node_value at the list head.
|
125
|
+
#
|
126
|
+
# @return [Object] the popped out node value.
|
103
127
|
def pop
|
104
128
|
delete_at(0)
|
105
129
|
end
|
@@ -107,6 +131,8 @@ module DoublyLinkedlist
|
|
107
131
|
alias :deque :pop
|
108
132
|
|
109
133
|
# Converts the list object into an array object with all the node values.
|
134
|
+
#
|
135
|
+
# @return [Array] the array object with node values as elements.
|
110
136
|
def to_a
|
111
137
|
item = @head
|
112
138
|
arr = []
|
@@ -120,6 +146,8 @@ module DoublyLinkedlist
|
|
120
146
|
end
|
121
147
|
|
122
148
|
# Converts the array representation of list into a string.
|
149
|
+
#
|
150
|
+
# @return [String] the string object after converting the array representation of list into string.
|
123
151
|
def to_s
|
124
152
|
to_a.to_s
|
125
153
|
end
|
@@ -1,10 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module DoublyLinkedlist
|
4
|
+
# Creates a node with value and links to next/previous nodes.
|
4
5
|
class Node
|
6
|
+
# @return [Object] the node value.
|
5
7
|
attr_reader :value
|
8
|
+
|
9
|
+
# @return [Node] the next/previous node object.
|
6
10
|
attr_accessor :next, :prev
|
7
11
|
|
12
|
+
# Initializes a node with the given object as its value.
|
13
|
+
#
|
14
|
+
# @param value [Object] the value to be set as node value.
|
8
15
|
def initialize(value)
|
9
16
|
@value = value
|
10
17
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: doubly_linkedlist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anshab M K
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -47,13 +47,14 @@ files:
|
|
47
47
|
- lib/doubly_linkedlist/list.rb
|
48
48
|
- lib/doubly_linkedlist/node.rb
|
49
49
|
- lib/doubly_linkedlist/version.rb
|
50
|
-
homepage: https://github.com/anshabmk/doubly_linkedlist
|
50
|
+
homepage: https://github.com/anshabmk/doubly_linkedlist/blob/master/README.md
|
51
51
|
licenses:
|
52
52
|
- MIT
|
53
53
|
metadata:
|
54
|
-
homepage_uri: https://github.com/anshabmk/doubly_linkedlist
|
54
|
+
homepage_uri: https://github.com/anshabmk/doubly_linkedlist/blob/master/README.md
|
55
55
|
source_code_uri: https://github.com/anshabmk/doubly_linkedlist
|
56
|
-
|
56
|
+
documentation_uri: https://rubydoc.info/gems/doubly_linkedlist
|
57
|
+
changelog_uri: https://github.com/anshabmk/doubly_linkedlist/releases
|
57
58
|
post_install_message:
|
58
59
|
rdoc_options: []
|
59
60
|
require_paths:
|