DoubleLinkedList: []

toying-with-ramdb
Philippe PITTOLI 2024-05-26 01:30:46 +02:00
parent 99e1a5e6ed
commit c89c32eca0
2 changed files with 9 additions and 3 deletions

View File

@ -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

View File

@ -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.