Class: LinkedList::List
- Inherits:
-
Object
- Object
- LinkedList::List
- Includes:
- Conversions
- Defined in:
- lib/linked-list/list.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
(also: #size)
readonly
Returns the value of attribute length.
Instance Method Summary collapse
-
#each ⇒ Object
Iterates over nodes from top to bottom passing node data to the block if given.
-
#first ⇒ Object
Returns the first element of the list or nil.
-
#initialize ⇒ List
constructor
A new instance of List.
- #inspect ⇒ Object
-
#last ⇒ Object
Returns the last element of the list or nil.
-
#pop ⇒ Object
Removes data from the end of the list.
-
#push(node) ⇒ Object
(also: #<<)
Pushes new nodes to the end of the list.
-
#reverse ⇒ Object
Reverse list of nodes and returns new instance of the list.
-
#reverse! ⇒ Object
Reverses list of nodes in place.
-
#shift ⇒ Object
Removes data from the beginning of the list.
-
#to_a ⇒ Object
(also: #to_ary)
Converts list to array.
-
#to_list ⇒ Object
Conversion function, see
Conversions.List. -
#unshift(node) ⇒ Object
Pushes new nodes on top of the list.
Methods included from Conversions
Constructor Details
#initialize ⇒ List
Returns a new instance of List.
8 9 10 11 12 |
# File 'lib/linked-list/list.rb', line 8 def initialize @head = nil @tail = nil @length = 0 end |
Instance Attribute Details
#length ⇒ Object (readonly) Also known as: size
Returns the value of attribute length.
5 6 7 |
# File 'lib/linked-list/list.rb', line 5 def length @length end |
Instance Method Details
#each ⇒ Object
Iterates over nodes from top to bottom passing node data to the block if given. If no block given, returns Enumerator.
Returns:
Enumerator or yields data to the block stored in every node on the list.
132 133 134 135 |
# File 'lib/linked-list/list.rb', line 132 def each return to_enum(__callee__) unless block_given? __each { |node| yield(node.data) } end |
#first ⇒ Object
Returns the first element of the list or nil.
16 17 18 |
# File 'lib/linked-list/list.rb', line 16 def first @head && @head.data end |
#inspect ⇒ Object
144 145 146 |
# File 'lib/linked-list/list.rb', line 144 def inspect sprintf('#<%s:%#x %s>', self.class, self.__id__, to_a.inspect) end |
#last ⇒ Object
Returns the last element of the list or nil.
22 23 24 |
# File 'lib/linked-list/list.rb', line 22 def last @tail && @tail.data end |
#pop ⇒ Object
Removes data from the end of the list.
Returns:
Data stored in the node or nil.
75 76 77 78 79 80 81 82 83 |
# File 'lib/linked-list/list.rb', line 75 def pop return nil unless @head tail = __pop @head = nil unless @tail @length -= 1 tail.data end |
#push(node) ⇒ Object Also known as: <<
Pushes new nodes to the end of the list.
Parameters:
- node
-
Any object, including
Nodeobjects.
Returns:
self of List object.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/linked-list/list.rb', line 34 def push(node) node = Node(node) @head ||= node if @tail @tail.next = node node.prev = @tail end @tail = node @length += 1 self end |
#reverse ⇒ Object
Reverse list of nodes and returns new instance of the list.
Returns:
New List in reverse order.
105 106 107 |
# File 'lib/linked-list/list.rb', line 105 def reverse List(to_a).reverse! end |
#reverse! ⇒ Object
Reverses list of nodes in place.
Returns:
self in reverse order.
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/linked-list/list.rb', line 114 def reverse! return self unless @head __each do |curr_node| curr_node.prev, curr_node.next = curr_node.next, curr_node.prev end @head, @tail = @tail, @head self end |
#shift ⇒ Object
Removes data from the beginning of the list.
Returns:
Data stored in the node or nil.
90 91 92 93 94 95 96 97 98 |
# File 'lib/linked-list/list.rb', line 90 def shift return nil unless @head head = __shift @tail = nil unless @head @length -= 1 head.data end |
#to_a ⇒ Object Also known as: to_ary
Converts list to array.
139 140 141 |
# File 'lib/linked-list/list.rb', line 139 def to_a each.to_a end |
#to_list ⇒ Object
Conversion function, see Conversions.List.
Returns:
self
153 154 155 |
# File 'lib/linked-list/list.rb', line 153 def to_list self end |
#unshift(node) ⇒ Object
Pushes new nodes on top of the list.
Parameters:
- node
-
Any object, including
Nodeobjects.
Returns:
self of List object.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/linked-list/list.rb', line 58 def unshift(node) node = Node(node) @tail ||= node node.next = @head @head.prev = node if @head @head = node @length += 1 self end |