shea-linked_list 0.1.0 → 0.1.2
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/lib/shea-linked_list/linked_list.rb +17 -6
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 31c91530bafbcd86744563af3d65fa7a37cd19607ae36cfd584ce6c65d043c39
|
|
4
|
+
data.tar.gz: 58cf583c26a51cd2a6e22e49b45544834e508ab2e6390f64efb2b61ae518083c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 12a12886383e78226faf5b31ca9b84dceb232b1a7773861da4c993e95c8da11f6f2afcfe6f324a2c442afb30994dddda30a3750882db623f8ccd9fad9750ba25
|
|
7
|
+
data.tar.gz: f5a5dfc75b4b7b28e2a83e62d9331280983196aa468353eeeabb30cf116cfc0d4700e558b67b13237daf40ea750e3687d40833b1a59520c388809d0851762570
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require 'shea-linked_list/node'
|
|
4
4
|
|
|
5
5
|
# A linear collection of Node data elements that “point” to the next Node by means of a pointer
|
|
6
6
|
class LinkedList
|
|
@@ -42,6 +42,8 @@ class LinkedList
|
|
|
42
42
|
|
|
43
43
|
# returns the node at the given index
|
|
44
44
|
def at(index)
|
|
45
|
+
return nil if index.negative? || index > size - 1
|
|
46
|
+
|
|
45
47
|
i = 0
|
|
46
48
|
node = @head
|
|
47
49
|
while i < index
|
|
@@ -83,17 +85,26 @@ class LinkedList
|
|
|
83
85
|
|
|
84
86
|
# inserts a new node with the provided value at the given index
|
|
85
87
|
def insert_at(value, index)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
case index
|
|
89
|
+
when 0 then prepend(value)
|
|
90
|
+
when size - 1 then append(value)
|
|
91
|
+
else
|
|
92
|
+
next_node = at(index)
|
|
93
|
+
node = Node.new(value, next_node)
|
|
94
|
+
prev_node = at(index - 1)
|
|
95
|
+
prev_node.next_node = node
|
|
96
|
+
end
|
|
90
97
|
end
|
|
91
98
|
|
|
92
99
|
# removes the node at the given index
|
|
93
100
|
def remove_at(index)
|
|
94
101
|
prev_node = at(index - 1)
|
|
95
102
|
next_node = at(index + 1)
|
|
96
|
-
|
|
103
|
+
if index.zero?
|
|
104
|
+
@head = next_node
|
|
105
|
+
else
|
|
106
|
+
prev_node.next_node = next_node
|
|
107
|
+
end
|
|
97
108
|
end
|
|
98
109
|
|
|
99
110
|
# the format should be: ( value ) -> ( value ) -> ( value ) -> nil
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: shea-linked_list
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shea Cronin
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-09-
|
|
11
|
+
date: 2024-09-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description:
|
|
14
14
|
email:
|