shea-linked_list 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '085fe42905eec757672446e0de19d31a063f20b4741c96a44c476ba76f860f66'
4
+ data.tar.gz: 97df35eff3c10e48f99acb506a640804d3f2cf4bfdef97ccc472b66f485cca79
5
+ SHA512:
6
+ metadata.gz: c45f27345816beaab11d3bc8967e392f358e9af74a35724e5fdba45bcb48d9ac4bcfff980b5c72af07418d75cabc32ef90412e6b96f35f6ccc3a824f34fb936e
7
+ data.tar.gz: 8123db559a20fd2458404103d6cbb48a5d5cad2bc2e08369e4a1e9ec5ca4a7255d4f8e1021581522d17ecda683914e16a0be4f1ba75e9b94dbcd16dc12605b04
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './node'
4
+
5
+ # A linear collection of Node data elements that “point” to the next Node by means of a pointer
6
+ class LinkedList
7
+ attr_accessor :head, :tail
8
+
9
+ def initialize
10
+ @head = nil
11
+ @tail = nil
12
+ end
13
+
14
+ # adds a new node containing value to the end of the list
15
+ def append(value)
16
+ node = Node.new(value)
17
+ @head ||= node
18
+ @tail.next_node = node if @tail
19
+ @tail = node
20
+ end
21
+
22
+ # adds a new node containing value to the start of the list
23
+ def prepend(value)
24
+ next_node = @head if @head
25
+ node = Node.new(value, next_node)
26
+ @tail ||= node
27
+ @head = node
28
+ end
29
+
30
+ # returns the total number of nodes in the list
31
+ def size
32
+ size = 0
33
+ if @head
34
+ current_node = @head
35
+ while current_node
36
+ size += 1
37
+ current_node = current_node.next_node
38
+ end
39
+ end
40
+ size
41
+ end
42
+
43
+ # returns the node at the given index
44
+ def at(index)
45
+ i = 0
46
+ node = @head
47
+ while i < index
48
+ i += 1
49
+ node = node.next_node
50
+ end
51
+ node
52
+ end
53
+
54
+ # removes the last element from the list
55
+ def pop
56
+ new_tail = at(size - 2)
57
+ new_tail.next_node = nil if new_tail
58
+ @tail = new_tail
59
+ end
60
+
61
+ # returns true if the passed in value is in the list and otherwise returns false.
62
+ def contains?(value)
63
+ current_node = @head
64
+ while current_node
65
+ return true if current_node.value == value
66
+
67
+ current_node = current_node.next_node
68
+ end
69
+ false
70
+ end
71
+
72
+ # returns the index of the node containing value, or nil if not found.
73
+ def find(value)
74
+ current_node = @head
75
+ i = 0
76
+ while current_node
77
+ return i if current_node.value == value
78
+
79
+ i += 1
80
+ current_node = current_node.next_node
81
+ end
82
+ end
83
+
84
+ # inserts a new node with the provided value at the given index
85
+ def insert_at(value, index)
86
+ next_node = at(index)
87
+ node = Node.new(value, next_node)
88
+ prev_node = at(index - 1)
89
+ prev_node.next_node = node
90
+ end
91
+
92
+ # removes the node at the given index
93
+ def remove_at(index)
94
+ prev_node = at(index - 1)
95
+ next_node = at(index + 1)
96
+ prev_node.next_node = next_node
97
+ end
98
+
99
+ # the format should be: ( value ) -> ( value ) -> ( value ) -> nil
100
+ def to_s
101
+ output = +''
102
+ current_node = @head
103
+ while current_node
104
+ output << "( #{current_node.value} ) -> "
105
+ current_node = current_node.next_node
106
+ end
107
+ output << 'nil'
108
+ end
109
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Holds a single element of data and a link/pointer to the next Node in the LinkedList
4
+ class Node
5
+ attr_accessor :value, :next_node
6
+
7
+ def initialize(value = nil, next_node = nil)
8
+ @value = value
9
+ @next_node = next_node
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ require 'shea-linked_list/linked_list'
2
+ require 'shea-linked_list/node'
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shea-linked_list
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shea Cronin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/shea-linked_list.rb
20
+ - lib/shea-linked_list/linked_list.rb
21
+ - lib/shea-linked_list/node.rb
22
+ homepage:
23
+ licenses: []
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.3.4
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.5.11
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Linked List project
44
+ test_files: []