Class: Node

Inherits:
Object
  • Object
show all
Defined in:
lib/lrjew/linked_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, _next) ⇒ Node

Returns a new instance of Node.



57
58
59
60
# File 'lib/lrjew/linked_list.rb', line 57

def initialize(data, _next)
  @data = data
  @next = _next
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



55
56
57
# File 'lib/lrjew/linked_list.rb', line 55

def data
  @data
end

#nextObject

Returns the value of attribute next.



54
55
56
# File 'lib/lrjew/linked_list.rb', line 54

def next
  @next
end

#prevObject

Returns the value of attribute prev.



54
55
56
# File 'lib/lrjew/linked_list.rb', line 54

def prev
  @prev
end

Instance Method Details

#inspectObject Also known as: to_s



76
77
78
79
80
# File 'lib/lrjew/linked_list.rb', line 76

def inspect
  prev_data = @prev && @prev.data || nil
  next_data = @next && @next.data || nil
  "#{prev_data.inspect} <- #{@data.inspect} -> #{next_data.inspect}"
end

#lengthObject



62
63
64
65
66
67
68
# File 'lib/lrjew/linked_list.rb', line 62

def length
  if @data.nil?
    0
  else
    1 + @next.length
  end
end

#push(data) ⇒ Object



70
71
72
73
74
# File 'lib/lrjew/linked_list.rb', line 70

def push(data)
  node = Node.new(data, self)
  self.prev = node
  node
end