Class: TrickBag::Collections::LinkedList
- Inherits:
-
Object
- Object
- TrickBag::Collections::LinkedList
- Defined in:
- lib/trick_bag/collections/linked_list.rb
Overview
Linked List based on [email protected]:neilparikh/ruby-linked-list.git, but modified somewhat.
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#first ⇒ Object
Returns the value of attribute first.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
-
#initialize(*items) ⇒ LinkedList
constructor
A new instance of LinkedList.
-
#pop ⇒ Object
Returns the last element from the list and removes it.
-
#push(value) ⇒ Object
Self.
-
#shift ⇒ Object
Removes the first element from the list.
-
#to_a ⇒ Object
The values in this list as an array.
-
#to_ary ⇒ Object
The values in this list as an array.
-
#unshift(value) ⇒ Object
Adds a value to the beginning of the list.
Constructor Details
#initialize(*items) ⇒ LinkedList
Returns a new instance of LinkedList.
22 23 24 25 26 |
# File 'lib/trick_bag/collections/linked_list.rb', line 22 def initialize(*items) @length = items.length @first = Node.new(items.shift) items.each { |item| push(item) } end |
Instance Attribute Details
#first ⇒ Object
Returns the value of attribute first.
17 18 19 |
# File 'lib/trick_bag/collections/linked_list.rb', line 17 def first @first end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
18 19 20 |
# File 'lib/trick_bag/collections/linked_list.rb', line 18 def length @length end |
Instance Method Details
#pop ⇒ Object
Returns the last element from the list and removes it
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/trick_bag/collections/linked_list.rb', line 45 def pop case(@length) when 0 raise "List is empty" when 1 @length = 0 value = @first.value @first = nil value else current = @first while current.next && current.next.next current = current.next end value = current.next.value current.next = nil @length -= 1 value end end |
#push(value) ⇒ Object
Returns self.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/trick_bag/collections/linked_list.rb', line 31 def push(value) node = Node.new(value) current_node = @first while current_node.next current_node = current_node.next end current_node.next = node @length += 1 self end |
#shift ⇒ Object
Removes the first element from the list
83 84 85 86 87 88 89 |
# File 'lib/trick_bag/collections/linked_list.rb', line 83 def shift raise "List is empty" if @length < 1 return_value = @first.value @first = @first.next @length -= 1 return_value end |
#to_a ⇒ Object
Returns the values in this list as an array.
93 94 95 96 97 98 99 100 101 |
# File 'lib/trick_bag/collections/linked_list.rb', line 93 def to_a current_node = @first array = [] while current_node array << current_node.value current_node = current_node.next end array end |
#to_ary ⇒ Object
Returns the values in this list as an array.
105 106 107 |
# File 'lib/trick_bag/collections/linked_list.rb', line 105 def to_ary to_a end |