class

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

new(values : Enumerable(A))

ditto

new

Creates an empty linked list.

new(*values)

Creates a linked list with the values as the values of the nodes.

Instance methods

+(list : Enumerable(C)) forall C

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
<<(value : A)

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
[](index : Int32)
append(value : A)

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
append(*values)

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
concat(list : LinkedList(A))

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
each

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
empty?

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
insert_at(value : A, index : Int32)

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)
peek

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
pop

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
push(value : A)

ditto

push(*values)

same as append

reverse

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
shift

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
to_s(io)

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 ]
unshift(value : A)

Adds a value to the beginning of a linked list.

list = LinkedList(Int32).new(1)
list.unshift(2)
list.pop() # => 1
list.pop() # => 2

Nested types