-
Notifications
You must be signed in to change notification settings - Fork 254
Add MutableLinkedList #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5734280
Add MutableLinkedList
c-p-murphy b4c21d1
fix test coverage
c-p-murphy 4784501
update documentation
c-p-murphy 2b86a38
update boundscheck, show, ==
c-p-murphy c3aa419
update error checking tests
c-p-murphy be73573
fixed typo
c-p-murphy f11fa44
fix error in tests
c-p-murphy 366f551
fix typo in docs
c-p-murphy c3d32dc
Merge branch 'master' into mutable_list
c-p-murphy 967a53c
fixed typo in docs
c-p-murphy f4665bc
update map
c-p-murphy 45cfa3f
edit iterate
c-p-murphy 70d017e
update first and last
c-p-murphy 63fed45
update map
c-p-murphy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Mutable Linked List | ||
|
||
The `MutableLinkedList` type implements a doubly linked list with mutable nodes. | ||
This data structure supports constant-time insertion/removal of elements | ||
at both ends of the list. | ||
|
||
Usage: | ||
|
||
```julia | ||
l = MutableLinkedList{T}() # initialize an empty list of type T | ||
l = MutableLinkedList{T}(elts...) # initialize a list with elements of type T | ||
isempty(l) # test whether list is empty | ||
length(l) # get the number of elements in list | ||
collect(l) # return a vector consisting of list elements | ||
eltype(l) # return type of list | ||
first(l) # return value of first element of list | ||
last(l) # return value of last element of list | ||
l1 == l2 # test lists for equality | ||
map(f, l) # return list with f applied to elements | ||
filter(f, l) # return list of elements where f(el) == true | ||
reverse(l) # return reversed list | ||
copy(l) # return a copy of list | ||
getindex(l, idx) # get value at index | ||
setindex!(l, data, idx) # set value at index to data | ||
append!(l1, l2) # attach l2 at the end of l1 | ||
append!(l, elts...) # attach elements at end of list | ||
delete!(l, idx) # delete element at index | ||
delete!(l, range) # delete elements within range [a,b] | ||
push!(l, data) # add element to end of list | ||
pushfirst!(l, data) # add element to beginning of list | ||
pop!(l) # remove element from end of list | ||
popfirst!(l) # remove element from beginning of list | ||
``` | ||
|
||
`MutableLinkedList` implements the Iterator interface, iterating over the list | ||
from first to last. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
mutable struct ListNode{T} | ||
data::T | ||
prev::ListNode{T} | ||
next::ListNode{T} | ||
function ListNode{T}() where T | ||
node = new{T}() | ||
node.prev = node | ||
node.next = node | ||
return node | ||
end | ||
function ListNode{T}(data) where T | ||
node = new{T}(data) | ||
return node | ||
end | ||
end | ||
|
||
mutable struct MutableLinkedList{T} | ||
len::Int | ||
front::ListNode{T} | ||
back::ListNode{T} | ||
function MutableLinkedList{T}() where T | ||
l = new{T}() | ||
l.len = 0 | ||
l.front = ListNode{T}() | ||
l.back = ListNode{T}() | ||
l.front.next = l.back | ||
l.back.prev = l.front | ||
return l | ||
end | ||
end | ||
|
||
MutableLinkedList() = MutableLinkedList{Any}() | ||
|
||
function MutableLinkedList{T}(elts...) where T | ||
l = MutableLinkedList{T}() | ||
for elt in elts | ||
push!(l, elt) | ||
end | ||
return l | ||
end | ||
|
||
iterate(l::MutableLinkedList) = begin | ||
l.len == 0 ? nothing : (l.front.next.data, l.front.next.next) | ||
end | ||
iterate(l::MutableLinkedList, n::ListNode) = begin | ||
n.next == n ? nothing : (n.data, n.next) | ||
end | ||
|
||
isempty(l::MutableLinkedList) = l.len == 0 | ||
length(l::MutableLinkedList) = l.len | ||
collect(l::MutableLinkedList{T}) where T = T[x for x in l] | ||
eltype(l::MutableLinkedList{T}) where T = T | ||
function first(l::MutableLinkedList) | ||
isempty(l) && throw(ArgumentError("List is empty")) | ||
return l.front.next.data | ||
end | ||
function last(l::MutableLinkedList) | ||
isempty(l) && throw(ArgumentError("List is empty")) | ||
return l.back.prev.data | ||
end | ||
|
||
==(l1::MutableLinkedList{T}, l2::MutableLinkedList{S}) where {T,S} = false | ||
|
||
function ==(l1::MutableLinkedList{T}, l2::MutableLinkedList{T}) where T | ||
length(l1) == length(l2) || return false | ||
for (i, j) in zip(l1, l2) | ||
i == j || return false | ||
end | ||
return true | ||
end | ||
|
||
function map(f::Base.Callable, l::MutableLinkedList{T}) where T | ||
if isempty(l) && f isa Function | ||
S = Core.Compiler.return_type(f, (T,)) | ||
return MutableLinkedList{S}() | ||
elseif isempty(l) && f isa Type | ||
return MutableLinkedList{f}() | ||
else | ||
S = typeof(f(first(l))) | ||
l2 = MutableLinkedList{S}() | ||
for h in l | ||
el = f(h) | ||
if el isa S | ||
push!(l2, el) | ||
else | ||
R = typejoin(S, typeof(el)) | ||
l2 = MutableLinkedList{R}(collect(l2)...) | ||
push!(l2, el) | ||
end | ||
end | ||
return l2 | ||
end | ||
end | ||
|
||
function filter(f::Function, l::MutableLinkedList{T}) where T | ||
l2 = MutableLinkedList{T}() | ||
for h in l | ||
if f(h) | ||
push!(l2, h) | ||
end | ||
end | ||
return l2 | ||
end | ||
|
||
function reverse(l::MutableLinkedList{T}) where T | ||
l2 = MutableLinkedList{T}() | ||
for h in l | ||
pushfirst!(l2, h) | ||
end | ||
return l2 | ||
end | ||
|
||
function copy(l::MutableLinkedList{T}) where T | ||
l2 = MutableLinkedList{T}() | ||
for h in l | ||
push!(l2, h) | ||
end | ||
return l2 | ||
end | ||
|
||
function getindex(l::MutableLinkedList, idx::Int) | ||
@boundscheck 0 < idx <= l.len || throw(BoundsError(l, idx)) | ||
node = l.front | ||
for i = 1:idx | ||
node = node.next | ||
end | ||
return node.data | ||
end | ||
|
||
function setindex!(l::MutableLinkedList{T}, data, idx::Int) where T | ||
@boundscheck 0 < idx <= l.len || throw(BoundsError(l, idx)) | ||
node = l.front | ||
for i = 1:idx | ||
node = node.next | ||
end | ||
node.data = convert(T, data) | ||
return l | ||
end | ||
|
||
function append!(l1::MutableLinkedList{T}, l2::MutableLinkedList{T}) where T | ||
l1.back.prev.next = l2.front.next | ||
l2.front.next.prev = l1.back.prev | ||
l1.len += length(l2) | ||
return l1 | ||
end | ||
|
||
function append!(l::MutableLinkedList, elts...) | ||
for elt in elts | ||
push!(l, elt) | ||
end | ||
l.len += length(elts) | ||
return l | ||
end | ||
|
||
function delete!(l::MutableLinkedList, idx::Int) | ||
@boundscheck 0 < idx <= l.len || throw(BoundsError(l, idx)) | ||
node = l.front | ||
for i = 1:idx | ||
node = node.next | ||
end | ||
prev = node.prev | ||
next = node.next | ||
prev.next = next | ||
next.prev = prev | ||
l.len -= 1 | ||
return l | ||
end | ||
|
||
function delete!(l::MutableLinkedList, r::UnitRange) | ||
@boundscheck 0 < first(r) < last(r) <= l.len || throw(BoundsError(l, r)) | ||
node = l.front | ||
for i = 1:first(r) | ||
node = node.next | ||
end | ||
prev = node.prev | ||
len = length(r) | ||
for j in 1:len | ||
node = node.next | ||
end | ||
next = node | ||
prev.next = next | ||
next.prev = prev | ||
l.len -= len | ||
return l | ||
end | ||
|
||
function push!(l::MutableLinkedList{T}, data) where T | ||
last = l.back.prev | ||
node = ListNode{T}(data) | ||
node.next = l.back | ||
node.prev = last | ||
l.back.prev = node | ||
last.next = node | ||
l.len += 1 | ||
return l | ||
end | ||
|
||
function pushfirst!(l::MutableLinkedList{T}, data) where T | ||
first = l.front.next | ||
node = ListNode{T}(data) | ||
node.prev = l.front | ||
node.next = first | ||
l.front.next = node | ||
first.prev = node | ||
l.len += 1 | ||
return l | ||
end | ||
|
||
function pop!(l::MutableLinkedList) | ||
isempty(l) && throw(ArgumentError("List must be non-empty")) | ||
last = l.back.prev.prev | ||
data = l.back.prev.data | ||
last.next = l.back | ||
l.back.prev = last | ||
l.len -= 1 | ||
return data | ||
end | ||
|
||
function popfirst!(l::MutableLinkedList) | ||
isempty(l) && throw(ArgumentError("List must be non-empty")) | ||
first = l.front.next.next | ||
data = l.front.next.data | ||
first.prev = l.front | ||
l.front.next = first | ||
l.len -= 1 | ||
return data | ||
end | ||
|
||
function show(io::IO, node::ListNode) | ||
x = node.data | ||
print(io, "$(typeof(node))($x)") | ||
end | ||
|
||
function show(io::IO, l::MutableLinkedList) | ||
c-p-murphy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(io, typeof(l), '(') | ||
join(io, l, ", ") | ||
print(io, ')') | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.