Class: Column
- Inherits:
-
Object
- Object
- Column
- Includes:
- Enumerable
- Defined in:
- lib/connect_four/data_structure/column.rb
Overview
class LinkedList for creating linked list with help of Node class’s objects
Constant Summary collapse
- MAX_SIZE =
6
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Instance Method Summary collapse
-
#append(value) ⇒ Object
appends value at the end of the list.
-
#at(index) ⇒ Object
return node of the list at the given index start from 0.
-
#contains?(value) ⇒ Boolean
returns true if a node with given value exists in list else returns false.
-
#each ⇒ Object
iterates over each node of the list and calls the block on it.
-
#find(value) ⇒ Object
finds index of the node with data equals given value.
-
#full? ⇒ Boolean
checks if size reach MAX_SIZE(6).
-
#initialize ⇒ Column
constructor
A new instance of Column.
-
#insert(index, node) ⇒ Object
insert_at’s insert method for inserting node in between list with given index.
-
#insert_at(value, index) ⇒ Object
inserts node with given value at index.
-
#pop ⇒ Object
removes last element of the list.
-
#prepend(value) ⇒ Object
prepends value at the top of the list.
-
#remove(index) ⇒ Object
used to remove any element between but not including head and tail.
-
#remove_at(index) ⇒ Object
removes element at a given index.
-
#to_s ⇒ Object
prints list in a readable format.
Constructor Details
#initialize ⇒ Column
Returns a new instance of Column.
10 11 12 13 14 15 16 17 |
# File 'lib/connect_four/data_structure/column.rb', line 10 def initialize # head/start of the list @head = nil # tail/end of the list @tail = nil # total number of nodes in the list @size = 0 end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
8 9 10 |
# File 'lib/connect_four/data_structure/column.rb', line 8 def head @head end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
8 9 10 |
# File 'lib/connect_four/data_structure/column.rb', line 8 def size @size end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
8 9 10 |
# File 'lib/connect_four/data_structure/column.rb', line 8 def tail @tail end |
Instance Method Details
#append(value) ⇒ Object
appends value at the end of the list
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/connect_four/data_structure/column.rb', line 26 def append(value) node = Node.new(value) if @head.nil? @head = node else @tail.next = node end @tail = node @size += 1 end |
#at(index) ⇒ Object
return node of the list at the given index start from 0
68 69 70 71 72 73 |
# File 'lib/connect_four/data_structure/column.rb', line 68 def at(index) return nil if index >= size || index.negative? node, _node_index = detect { |_node, node_index| node_index == index } node end |
#contains?(value) ⇒ Boolean
returns true if a node with given value exists in list else returns false.
91 92 93 |
# File 'lib/connect_four/data_structure/column.rb', line 91 def contains?(value) any? { |node, _node_index| node.data == value } end |
#each ⇒ Object
iterates over each node of the list and calls the block on it
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/connect_four/data_structure/column.rb', line 55 def each return nil if @head.nil? pointer = @head index = 0 until pointer.nil? yield(pointer, index) pointer = pointer.next index += 1 end end |
#find(value) ⇒ Object
finds index of the node with data equals given value
96 97 98 |
# File 'lib/connect_four/data_structure/column.rb', line 96 def find(value) find_index { |node, _node_index| node.data == value } end |
#full? ⇒ Boolean
checks if size reach MAX_SIZE(6)
21 22 23 |
# File 'lib/connect_four/data_structure/column.rb', line 21 def full? @size == MAX_SIZE end |
#insert(index, node) ⇒ Object
insert_at’s insert method for inserting node in between list with given index
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/connect_four/data_structure/column.rb', line 124 def insert(index, node) # current node at the index current_node = at(index) # previous node from the index previous_node = at(index - 1) # pp current_node # pp previous_node node.next = current_node previous_node.next = node @size += 1 end |
#insert_at(value, index) ⇒ Object
inserts node with given value at index
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/connect_four/data_structure/column.rb', line 108 def insert_at(value, index) list_size = size return 'only positive index' if index.negative? || index > list_size node = Node.new value if index.zero? prepend(value) elsif index == list_size - 1 append(value) else insert(index, node) end end |
#pop ⇒ Object
removes last element of the list
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/connect_four/data_structure/column.rb', line 76 def pop return nil if @head.nil? if @head == @tail @head = nil @tail = nil else before_tail = at size - 2 before_tail.next = nil @tail = before_tail end @size -= 1 end |
#prepend(value) ⇒ Object
prepends value at the top of the list
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/connect_four/data_structure/column.rb', line 39 def prepend(value) node = Node.new(value) if @head.nil? @tail = node else node.next = @head end @head = node @size += 1 end |
#remove(index) ⇒ Object
used to remove any element between but not including head and tail
151 152 153 154 155 |
# File 'lib/connect_four/data_structure/column.rb', line 151 def remove(index) previous_node = at(index - 1) next_node = at(index + 1) previous_node.next = next_node end |
#remove_at(index) ⇒ Object
removes element at a given index
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/connect_four/data_structure/column.rb', line 137 def remove_at(index) return nil if at(index).nil? if index == size - 1 pop elsif index.zero? @head = @head.next else remove(index) end @size -= 1 end |
#to_s ⇒ Object
prints list in a readable format
101 102 103 104 105 |
# File 'lib/connect_four/data_structure/column.rb', line 101 def to_s string = +'' each { |elem| string << "( #{elem.data} )->" } string << '( nil )' end |