mutable struct ListElmt{T}
value::T
next::Union{Nothing,ListElmt}
end
mutable struct List
size::Int
head::Union{Nothing,ListElmt}
tail::Union{Nothing,ListElmt}
ins_pos::Int
end
#--------init--------#
List()=List(0,nothing,nothing)
ListElmt(data)=ListElmt(data,nothing)
#--------init--------#
#--------macro----------#
macro list_size(list)
:($(list).size)
end
macro list_head(list)
:($(list).head)
end
macro list_tail(list)
:($(list).tail)
end
macro list_next(elmt)
:($(elmt).next)
end
macro list_data(elmt)
:($(elmt).value)
end
#--------macro----------#
#--------function---------#
function push(list::List,data)
new_elmt=ListElmt(data)
if list.size==0
list.head=list.tail=new_elmt
else
list.tail=list.tail.next=new_elmt
end
list.size+=1
end
function remove(list::List)
if list.size==0
return nothing
else
current=list.head
for i=1:list.size-2
current=current.next
end
res=current.next.value
list.tail=current
list.size-=1
return res
end
end
function show(list::List)
current=list.head
for i in 1:list.size
print(current.value," ")
current=current.next
end
println()
end
function contain(list::List,data)
current=list.head
while current != nothing
if data==current.value
return true
end
current=current.next
end
return false
end
#--------function---------#
另外发现一点小问题,在函数内竟然不能用宏
发现的错误:
可以保证的是不在函数内用宏是没有问题的