DoubleLinkedList: []
parent
99e1a5e6ed
commit
c89c32eca0
|
@ -42,6 +42,10 @@ describe "DoubleLinkedList" do
|
||||||
list.pop
|
list.pop
|
||||||
end
|
end
|
||||||
|
|
||||||
|
expect_raises DoubleLinkedList::OutOfBounds do
|
||||||
|
list[0]
|
||||||
|
end
|
||||||
|
|
||||||
list.size.should eq 0
|
list.size.should eq 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -299,16 +299,18 @@ class DoubleLinkedList(V)
|
||||||
#
|
#
|
||||||
# TODO: either start with the first entry or the last depending on the index.
|
# TODO: either start with the first entry or the last depending on the index.
|
||||||
def [](index : Int32) : Node(V)
|
def [](index : Int32) : Node(V)
|
||||||
raise OutOfBounds.new if index > @size
|
raise OutOfBounds.new if index >= @size
|
||||||
|
|
||||||
return @first if index == 0
|
return @first.not_nil! if index == 0
|
||||||
return @last if index == @size - 1
|
return @last.not_nil! if index == @size - 1
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
each_node do |node|
|
each_node do |node|
|
||||||
return node if i == index
|
return node if i == index
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
raise BrokenList.new "couldn't find the node, smth must be brkn"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Concats two lists.
|
# Concats two lists.
|
||||||
|
|
Loading…
Reference in New Issue