doubly_linkedlist 0.2.0 → 0.2.1
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/Gemfile.lock +1 -1
- data/README.md +164 -12
- data/lib/doubly_linkedlist/list.rb +5 -5
- data/lib/doubly_linkedlist/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47ad35ba5b5fce866c46f143f8d1adc8cd9230fb477ce3af85cc862c2f22d5e1
|
4
|
+
data.tar.gz: 2ef37aa5fc4465ea04328d3e8b1b091cfb1a79a8c421619ba65aa349832cf2f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2555a7110da1148d3e0e141b9aacf62c8c1d981bac0278d8ea86ff3032fdecdfcee08dd953edc12e701966e7834e1f1533f264d32697d83892d649e6a451216
|
7
|
+
data.tar.gz: 7ef1c47806d9165fd861d626949b471e9e266b2ed3ea56dcf7b12098345c81a63ebfb2484ffafe734e019ab51b3735ded134c61e0f9b4203a5374c06f964be1b
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,31 +20,183 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
### Initialization
|
24
24
|
|
25
|
-
There are
|
25
|
+
There are different ways to initialize a linkedlist including the one which converting an array into a list by calling `to_list` method on the array object.
|
26
26
|
|
27
27
|
You can find the examples below.
|
28
28
|
|
29
29
|
```ruby
|
30
|
-
|
31
|
-
|
32
|
-
list = DoublyLinkedlist.new([obj1, obj2, ...])
|
30
|
+
# You can pass an array as argument whose elements will be the created list's node values.
|
31
|
+
list = DoublyLinkedlist::List.new([obj1, obj2, ...])
|
33
32
|
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
# Argument is optional. If not given, a empty list with no nodes is created.
|
34
|
+
empty_list = DoublyLinkedlist::List.new
|
35
|
+
|
36
|
+
# You can also use `to_list` method on an array object to create a new list object.
|
37
|
+
another_list = [obj1, obj2, ...].to_list
|
38
|
+
|
39
|
+
another_list.class # => DoublyLinkedlist::List
|
40
|
+
```
|
41
|
+
|
42
|
+
### Methods
|
43
|
+
|
44
|
+
#### General methods
|
45
|
+
|
46
|
+
##### #count
|
47
|
+
|
48
|
+
Returns the number of nodes in the list.
|
49
|
+
|
50
|
+
Examples are shown below,
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
empty_list = DoublyLinkedlist::List.new
|
54
|
+
|
55
|
+
empty_list.count # => 0
|
56
|
+
|
57
|
+
another_list = [10, 20, 40, 30].to_list
|
58
|
+
|
59
|
+
another_list.count # => 4
|
37
60
|
```
|
38
|
-
|
61
|
+
|
62
|
+
##### #head (also: #first)
|
63
|
+
|
64
|
+
Returns the value at the head of the list if present, `nil` otherwise.
|
39
65
|
|
40
66
|
```ruby
|
41
|
-
|
67
|
+
list = [10, 20, 40, 50].to_list
|
42
68
|
|
43
|
-
|
69
|
+
list.head # => 10
|
70
|
+
list.first # => 10
|
44
71
|
|
45
|
-
|
72
|
+
empty_list = DoublyLinkedlist::List.new
|
73
|
+
|
74
|
+
empty_list.head # => nil
|
46
75
|
```
|
47
76
|
|
77
|
+
##### #tail (also: #last)
|
78
|
+
|
79
|
+
Returns the value at the tail of the list if present, `nil` otherwise.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
list = [10, 20, 40, 50].to_list
|
83
|
+
|
84
|
+
list.tail # => 50
|
85
|
+
list.last # => 50
|
86
|
+
|
87
|
+
empty_list = DoublyLinkedlist::List.new
|
88
|
+
|
89
|
+
empty_list.tail # => nil
|
90
|
+
```
|
91
|
+
|
92
|
+
##### #find_at(index)
|
93
|
+
|
94
|
+
Returns the node object at the given index if present, `nil` otherwise.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
list = DoublyLinkedlist::List.new [1, "Some object", 40, 100]
|
98
|
+
|
99
|
+
list.find_at(1) # => "Some object"
|
100
|
+
list.find_at(4) # => nil
|
101
|
+
```
|
102
|
+
|
103
|
+
##### #index(value) and #rindex(value)
|
104
|
+
|
105
|
+
Returns the leftmost(`#index`)/rightmost(`#rindex`) index of the value present in the list,
|
106
|
+
`nil` if not present.
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
list = DoublyLinkedlist::List.new [1, "Some object", 40, 100, 40]
|
110
|
+
|
111
|
+
list.index(40) # => 2
|
112
|
+
list.rindex(40) # => 4
|
113
|
+
|
114
|
+
list.index(200) # => nil
|
115
|
+
```
|
116
|
+
|
117
|
+
##### #delete_at(index)
|
118
|
+
|
119
|
+
Deletes the node at a given index and returns the value present in the
|
120
|
+
deleted node. Returns `nil` if the given index is out of range.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
list = DoublyLinkedlist::List.new [1, "Some object", 40, 100, 40]
|
124
|
+
list.count # => 5
|
125
|
+
|
126
|
+
list.delete_at(1) # => "Some object"
|
127
|
+
list # => [1, 40, 100, 40]
|
128
|
+
list.count # => 4
|
129
|
+
|
130
|
+
list.delete_at(6) # => nil
|
131
|
+
list.count # => 4
|
132
|
+
```
|
133
|
+
|
134
|
+
##### #to_a
|
135
|
+
|
136
|
+
Converts the list object into an array object with all the node values.
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
list = DoublyLinkedlist::List.new [1, "Some object", 40, 100, 40]
|
140
|
+
|
141
|
+
arr = list.to_a
|
142
|
+
arr.class # => Array
|
143
|
+
arr # => [1, "Some object", 40, 100, 40]
|
144
|
+
```
|
145
|
+
|
146
|
+
#### Stack Operations
|
147
|
+
|
148
|
+
##### #push(obj)
|
149
|
+
|
150
|
+
Inserts a node with the given value into the head of the list,
|
151
|
+
increments and returns the count of nodes.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
list = DoublyLinkedlist::List.new [1, "Some object", 40, 100, 40]
|
155
|
+
|
156
|
+
list.push(25) # => 6
|
157
|
+
list # => [25, 1, "Some object", 40, 100, 40]
|
158
|
+
```
|
159
|
+
|
160
|
+
##### #pop
|
161
|
+
|
162
|
+
Deletes and returns the node value at the head of the list.
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
list = DoublyLinkedlist::List.new [1, "Some object", 40, 100, 40]
|
166
|
+
|
167
|
+
list.pop # => 1
|
168
|
+
list.count # => 4
|
169
|
+
list # => ["Some object", 40, 100, 40]
|
170
|
+
```
|
171
|
+
|
172
|
+
#### Queue Operations
|
173
|
+
|
174
|
+
##### #enqueue(obj)
|
175
|
+
|
176
|
+
Appends a node with the given value into tail of the list,
|
177
|
+
increments and returns the count of nodes.
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
list = DoublyLinkedlist::List.new [1, "Some object", 40, 100, 40]
|
181
|
+
|
182
|
+
list.enqueue(25) # => 6
|
183
|
+
list # => [1, "Some object", 40, 100, 40, 25]
|
184
|
+
```
|
185
|
+
|
186
|
+
##### #deque
|
187
|
+
|
188
|
+
Deletes and returns the node value at the head of the list. (Same as `#pop`)
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
list = DoublyLinkedlist::List.new [1, "Some object", 40, 100, 40]
|
192
|
+
|
193
|
+
list.deque # => 1
|
194
|
+
list.count # => 4
|
195
|
+
list # => ["Some object", 40, 100, 40]
|
196
|
+
```
|
197
|
+
|
198
|
+
Refer [documentation](https://rubydoc.info/gems/doubly_linkedlist/DoublyLinkedlist/) for more information.
|
199
|
+
|
48
200
|
## Development
|
49
201
|
|
50
202
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
@@ -40,7 +40,7 @@ module DoublyLinkedlist
|
|
40
40
|
# @param index [Integer] the index where to look up.
|
41
41
|
# @return [Object, Nil] the value at the given index, or nil if index is out of range.
|
42
42
|
def find_at(index)
|
43
|
-
return if (index + 1) > count
|
43
|
+
return if (index + 1) > count || index < 0
|
44
44
|
|
45
45
|
item = @head
|
46
46
|
index.times { item = item.next }
|
@@ -53,7 +53,7 @@ module DoublyLinkedlist
|
|
53
53
|
# @param value [Object] the node value for which to be looked up.
|
54
54
|
# @return [Integer, Nil] the index of the value passed in, or nil if value is not present.
|
55
55
|
def index(value)
|
56
|
-
find_index(0, @head, :next, 1)
|
56
|
+
find_index(value, 0, @head, :next, 1)
|
57
57
|
end
|
58
58
|
|
59
59
|
# Returns the rightmost index of value present in the list.
|
@@ -62,7 +62,7 @@ module DoublyLinkedlist
|
|
62
62
|
# @param value [Object] the node value for which to be looked up.
|
63
63
|
# @return [Integer, Nil] the index of the value passed in, or nil if value is not present.
|
64
64
|
def rindex(value)
|
65
|
-
find_index(count - 1, @tail, :prev, -1)
|
65
|
+
find_index(value, count - 1, @tail, :prev, -1)
|
66
66
|
end
|
67
67
|
|
68
68
|
# Inserts a node with the given value into the head of the list,
|
@@ -102,7 +102,7 @@ module DoublyLinkedlist
|
|
102
102
|
# @param index [Integer] the index at which node has to be deleted.
|
103
103
|
# @return [Object] the deleted node value.
|
104
104
|
def delete_at(index)
|
105
|
-
return if (index + 1) > count
|
105
|
+
return if (index + 1) > count || index < 0
|
106
106
|
|
107
107
|
if index.zero?
|
108
108
|
deleted = @head
|
@@ -161,7 +161,7 @@ module DoublyLinkedlist
|
|
161
161
|
values.each { |v| enqueue(v) }
|
162
162
|
end
|
163
163
|
|
164
|
-
def find_index(start_index, start_item, item_iterator_name, step)
|
164
|
+
def find_index(value, start_index, start_item, item_iterator_name, step)
|
165
165
|
i = start_index
|
166
166
|
item = start_item
|
167
167
|
|