LinkedList(A)
Inherits Enumerable < Reference < Object
A linked list is a Enumerable (see the Enumerable module) data structure
that stores multiple pieces of data in non contiguous locations in memory.
To create a linked list:
list = LinkedList(Int32).new
list.push(2)
puts list.pop
The above produces:
2
Constructors
ditto
Creates an empty linked list.
Creates a linked list with the values as the values of the nodes.
Instance methods
Returns a new LinkedList with all of the elements from the first list
followed by all of the elements in the second list.
first_list = LinkedList(Int32).new(1, 2)
second_list = LinkedList(String).new("foo", "bar")
combined_list = first_list + second_list
combined_list.peek() # => "bar"
combined_list.shift() # => 1
Override the << (shift) operator to add a value to the end of a linked list. This method returns itself so it can be chained.
list = LinkedList(Int32).new(1)
list << 2 << 3
list.pop() # => 3
list.pop() # => 2
list.pop() # => 1
Adds a value to the end of a linked list.
list = LinkedList(Int32).new
list.append(1)
list.push(2)
list.pop() # => 2
list.pop() # => 1
Adds a list of values to the end of a linked list.
list = LinkedList(Int32 | String).new
list.append(1, "foo")
list.pop() # => "foo"
list.pop() # => 1
Adds all the elemenets of the list to the end of the current linked list.
first_list = LinkedList(Int32).new(1, 2)
second_list = LinkedList(Int32).new(3, 4)
combined_list = first_list.concat(second_list)
combined_list.peek() # => 4
combined_list.shift() # => 1
Iterates over all the values in the linked list.
values = [1, 2, 3]
list = LinkedList(Int32).new(values)
list.each do |elem|
puts elem
end
The above produces:
```text
1
2
3
Returns true if and only if there are no elements in the list.
list = LinkedList(Int32).new
list.empty? # => true
list.push(1)
list.empty? # => false
Adds a value to the linked list at a specified index.
list = LinkedList(Int32).new
list.append(1)
list.append(2)
list.insert_at(3, 1)
Returns the value of the tail of the linked list, or nil if no value was supplied.
list = LinkedList(Float64).new(1.23)
list.push(4.56)
list.peek() # => 4.56
Returns the last Node from the list and removes it.
list = LinkedList(Float64).new(1.23)
list.push(4.56)
list.pop() # => 4.56
list.peek() # => 1.23
ditto
same as append
Creates a copy of the LinkedList with the order reversed.
list = LinkedList(Int32).new(1, 2, 3)
reversed_list = list.reverse
list.pop() # => 1
list.pop() # => 2
list.pop() # => 3
Returns the first Node from the list and removes it.
list = LinkedList(Float64).new(1.23)
list.push(4.56)
list.shift() # => 1.23
list.peek() # => 4.56
Overloading the to_s function to print the contents of the linked list This calls the to_s function of each node in the linked list
values = [1, 2, 3]
puts values
The above produces:
```text
[ 1, 2, 3 ]
Adds a value to the beginning of a linked list.
list = LinkedList(Int32).new(1)
list.unshift(2)
list.pop() # => 1
list.pop() # => 2