Class: DSA::LinkedList::Singly
- Inherits:
-
Object
- Object
- DSA::LinkedList::Singly
- Includes:
- Enumerable
- Defined in:
- lib/dsa-ruby/linked_list.rb
Overview
DSA::LinkedList::Singly - Singly linked list implementation.
Each node points only to the next node. Supports push (prepend), append, pop (from front), delete, find, and reverse!. Includes Enumerable for functional-style operations.
Instance Method Summary collapse
-
#append(val) ⇒ DSA::LinkedList::Singly
Appends a value to the end of the list.
-
#delete(val) ⇒ Boolean
Deletes the first occurrence of a value from the list.
-
#each {|val| ... } ⇒ Enumerator
Yields each value in the list from front to back.
-
#empty? ⇒ Boolean
Checks if the list is empty.
-
#find(val) ⇒ Node?
Finds the first node with the given value.
-
#initialize ⇒ DSA::LinkedList::Singly
constructor
Initialize a new empty singly linked list.
-
#peek ⇒ Object
Returns the value at the front of the list without removing it.
-
#pop ⇒ Object
Removes and returns the value at the front of the list.
-
#push(val) ⇒ DSA::LinkedList::Singly
Prepends a value to the front of the list.
-
#reverse! ⇒ DSA::LinkedList::Singly
Reverses the list in-place.
-
#size ⇒ Integer
Returns the number of elements in the list.
-
#to_a ⇒ Array
Returns an array of all values in the list (front to back).
Constructor Details
#initialize ⇒ DSA::LinkedList::Singly
Initialize a new empty singly linked list.
65 66 67 68 |
# File 'lib/dsa-ruby/linked_list.rb', line 65 def initialize @head = nil @size = 0 end |
Instance Method Details
#append(val) ⇒ DSA::LinkedList::Singly
Appends a value to the end of the list.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dsa-ruby/linked_list.rb', line 90 def append(val) new_node = Node.new(val) if @head.nil? @head = new_node else node = @head node = node.next while node.next node.next = new_node end @size += 1 self end |
#delete(val) ⇒ Boolean
Deletes the first occurrence of a value from the list.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/dsa-ruby/linked_list.rb', line 139 def delete(val) return false unless @head if @head.val == val @head = @head.next @size -= 1 return true end node = @head while node.next if node.next.val == val node.next = node.next.next @size -= 1 return true end node = node.next end false end |
#each {|val| ... } ⇒ Enumerator
Yields each value in the list from front to back.
246 247 248 249 250 251 252 253 |
# File 'lib/dsa-ruby/linked_list.rb', line 246 def each(&block) return enum_for(:each) unless block_given? node = @head while node yield node.val node = node.next end end |
#empty? ⇒ Boolean
Checks if the list is empty.
216 217 218 |
# File 'lib/dsa-ruby/linked_list.rb', line 216 def empty? @size == 0 end |
#find(val) ⇒ Node?
Finds the first node with the given value.
169 170 171 172 173 174 175 176 |
# File 'lib/dsa-ruby/linked_list.rb', line 169 def find(val) node = @head while node return node if node.val == val node = node.next end nil end |
#peek ⇒ Object
Returns the value at the front of the list without removing it.
126 127 128 129 |
# File 'lib/dsa-ruby/linked_list.rb', line 126 def peek raise IndexError, "list is empty" unless @head @head.val end |
#pop ⇒ Object
Removes and returns the value at the front of the list.
110 111 112 113 114 115 116 117 |
# File 'lib/dsa-ruby/linked_list.rb', line 110 def pop raise IndexError, "list is empty" unless @head val = @head.val @head = @head.next @size -= 1 val end |
#push(val) ⇒ DSA::LinkedList::Singly
Prepends a value to the front of the list.
77 78 79 80 81 |
# File 'lib/dsa-ruby/linked_list.rb', line 77 def push(val) @head = Node.new(val, @head) @size += 1 self end |
#reverse! ⇒ DSA::LinkedList::Singly
Reverses the list in-place.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/dsa-ruby/linked_list.rb', line 185 def reverse! return self unless @head prev = nil curr = @head while curr nxt = curr.next curr.next = prev prev = curr curr = nxt end @head = prev self end |
#size ⇒ Integer
Returns the number of elements in the list.
206 207 208 |
# File 'lib/dsa-ruby/linked_list.rb', line 206 def size @size end |
#to_a ⇒ Array
Returns an array of all values in the list (front to back).
226 227 228 229 230 231 232 233 234 |
# File 'lib/dsa-ruby/linked_list.rb', line 226 def to_a result = [] node = @head while node result << node.val node = node.next end result end |