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