Class: Algorithmable::DataStructs::LinkedList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/algorithmable/data_structs/linked_list.rb

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLinkedList

Returns a new instance of LinkedList.



7
8
9
10
11
12
# File 'lib/algorithmable/data_structs/linked_list.rb', line 7

def initialize
  @head = nil
  @tail = nil
  @length = 0
  @op_counter = 0
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



5
6
7
# File 'lib/algorithmable/data_structs/linked_list.rb', line 5

def length
  @length
end

Instance Method Details

#each(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/algorithmable/data_structs/linked_list.rb', line 30

def each(&block)
  next_node = @head
  nodes = []
  while next_node
    nodes << next_node
    next_node = next_node.succ
  end
  nodes.each(&block)
end

#firstObject



22
23
24
# File 'lib/algorithmable/data_structs/linked_list.rb', line 22

def first
  @head ? @head.dup : fail_index_error
end

#lastObject



26
27
28
# File 'lib/algorithmable/data_structs/linked_list.rb', line 26

def last
  @tail ? @tail.dup : fail_index_error
end

#push(data) ⇒ Object



18
19
20
# File 'lib/algorithmable/data_structs/linked_list.rb', line 18

def push(data)
  link_tail_node data
end

#unshift(data) ⇒ Object



14
15
16
# File 'lib/algorithmable/data_structs/linked_list.rb', line 14

def unshift(data)
  link_head_node data
end