Class: RubyStructures::LinkedList
- Inherits:
-
Object
- Object
- RubyStructures::LinkedList
- Defined in:
- lib/rubystructures/linked_list.rb
Instance Method Summary collapse
-
#append(value) ⇒ Object
Public: Add a value to the end of the LinkedList.
-
#empty? ⇒ Boolean
Public: Checks to see if the LinkedList is empty.
-
#head ⇒ Object
Public: Returns the value at the head of the LinkedList.
-
#initialize ⇒ LinkedList
constructor
Public: Create a new instance of LinkedList.
-
#item_at(index) ⇒ Object
Public: Returns the Node at the desired :index.
-
#item_at_head ⇒ Object
Public: Gets the Node at the head of the LinkedList.
-
#prepend(value) ⇒ Object
Public: Add a value to the beginning of the LinkedList.
-
#remove(value) ⇒ Object
Public: Finds and removes the first occurrence of a Node with the desired value.
-
#remove_at_head ⇒ Object
Public: Removes the Node at the head of the LinkedList.
-
#remove_at_tail ⇒ Object
Public: Removes the Node at the tail of the LinkedList.
-
#search(value) ⇒ Object
Public: Find an element by its value and return it.
Constructor Details
#initialize ⇒ LinkedList
Public: Create a new instance of LinkedList
Examples
@linked_list = RubyStructures::LinkedList.new # => LinkedList
Returns a new instance of LinkedList.
11 12 13 |
# File 'lib/rubystructures/linked_list.rb', line 11 def initialize @head = RubyStructures::Node.new(self, nil) end |
Instance Method Details
#append(value) ⇒ Object
Public: Add a value to the end of the LinkedList
value - Ruby object to be inserted into the LinkedList
Examples
@linked_list.append(42) # => Node
Returns the Node added to the LinkedList.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rubystructures/linked_list.rb', line 66 def append(value) if self.empty? self.prepend(value) return end element = self.head # loop through each element in the LinkedList, until element.next # equals @head, signifying we have reached the end of the list. while(element != self.head) element = element.next end new_element = RubyStructures::Node.new(self, value) # insert the new element at the end of the LinkedList. new_element.next = element.next element.next = new_element end |
#empty? ⇒ Boolean
Public: Checks to see if the LinkedList is empty.
Examples
@linked_list.empty? # => false
Returns true if the LinkedList is empty, returns false otherwise.
36 37 38 |
# File 'lib/rubystructures/linked_list.rb', line 36 def empty? @head.next == nil end |
#head ⇒ Object
Public: Returns the value at the head of the LinkedList.
Examples
@linked_list.head # => 42
Returns the value held by the Node at the head of the list, otherwise if the list is empty, returns nil.
24 25 26 |
# File 'lib/rubystructures/linked_list.rb', line 24 def head @head.next end |
#item_at(index) ⇒ Object
Public: Returns the Node at the desired :index.
index - the Integer index value of Node to return.
Examples
@linked_list.item_at(42) # => #<Node: >
Returns a Node located at position :index in the LinkedList.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rubystructures/linked_list.rb', line 131 def item_at(index) element = self.head count = 0 while count < index return nil if element.nil? element = element.next count += 1 end element end |
#item_at_head ⇒ Object
Public: Gets the Node at the head of the LinkedList.
Examples
@linked_list.item_at_head # => #<Node: >
Returns the Node at the head of the LinkedList.
117 118 119 |
# File 'lib/rubystructures/linked_list.rb', line 117 def item_at_head @head.next end |
#prepend(value) ⇒ Object
Public: Add a value to the beginning of the LinkedList
value - Ruby object to be inserted into the LinkedList
Examples
@linked_list.prepend(42) # => Node
Returns the Node added to the LinkedList.
50 51 52 53 54 |
# File 'lib/rubystructures/linked_list.rb', line 50 def prepend(value) element = RubyStructures::Node.new(self, value) element.next = @head.next @head.next = element end |
#remove(value) ⇒ Object
Public: Finds and removes the first occurrence of a Node with the desired value.
value - the Ruby object value to find and remove from the LinkedList
Examples
@linked_list.remove(42) # => #<Node: >
Returns the node that was removed from the LinkedList
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rubystructures/linked_list.rb', line 153 def remove(value) element = self.head previous_element = @head while element.value != value if element.next.nil? return nil else previous_element = element element = element.next end end previous_element.next = element.next element end |
#remove_at_head ⇒ Object
Public: Removes the Node at the head of the LinkedList.
Examples
@linked_list.remove_at_head # => #<Node: >
Returns the Node at the head of the list.
177 178 179 180 181 182 |
# File 'lib/rubystructures/linked_list.rb', line 177 def remove_at_head return nil if self.empty? element = self.head @head.next = nil || element.next element end |
#remove_at_tail ⇒ Object
Public: Removes the Node at the tail of the LinkedList.
Examples
@linked_list.remove_at_tail # => #<Node: >
Returns
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/rubystructures/linked_list.rb', line 192 def remove_at_tail return nil if self.empty? element = self.head previous_element = @head until element.next.nil? previous_element = element element = element.next end previous_element.next = nil element end |
#search(value) ⇒ Object
Public: Find an element by its value and return it.
value - Ruby object to be inserted into the LinkedList
Examples
@linked_list.search(42) # => 42
Returns the value of the element if it is in the LinkedList, returns nil otherwise.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rubystructures/linked_list.rb', line 96 def search(value) return nil if self.empty? element = self.head while element.value != value if element.next.nil? return nil else element = element.next end end element end |