Skip to content

Commit

Permalink
Merge pull request #883 from chengchingwen/master
Browse files Browse the repository at this point in the history
fix append! iterable to MutableLinkedList
  • Loading branch information
oxinabox authored Dec 7, 2023
2 parents b462388 + 5725e7a commit 2cd8f52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/mutable_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ end

function Base.append!(l::MutableLinkedList, elts...)
for elt in elts
push!(l, elt)
for v in elt
push!(l, v)
end
end
return l
end
Expand Down Expand Up @@ -213,6 +215,14 @@ function Base.push!(l::MutableLinkedList{T}, data) where T
return l
end

function Base.push!(l::MutableLinkedList{T}, data1, data...) where T
push!(l, data1)
for v in data
push!(l, v)
end
return l
end

function Base.pushfirst!(l::MutableLinkedList{T}, data) where T
oldfirst = l.node.next
node = ListNode{T}(data)
Expand Down
6 changes: 5 additions & 1 deletion test/test_mutable_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@
@test l2 == MutableLinkedList{Int}()
@test collect(l) == collect(MutableLinkedList{Int}(1:2n...))
l3 = MutableLinkedList{Int}(1:n...)
append!(l3, n+1:2n...)
append!(l3, n+1:2n)
@test l3 == MutableLinkedList{Int}(1:2n...)
@test collect(l3) == collect(MutableLinkedList{Int}(1:2n...))
l4 = MutableLinkedList{Int}(1:n...)
push!(l4, n+1:2n...)
@test l4 == MutableLinkedList{Int}(1:2n...)
@test collect(l4) == collect(MutableLinkedList{Int}(1:2n...))
end

@testset "delete" begin
Expand Down

0 comments on commit 2cd8f52

Please sign in to comment.