Create an empty array, then insert (Priority Queue)

you can have an array of type Any for its elements. But unless you use some structure like a linked list your insert operation might be really slow and inefficient since you need to allocate a new array every time.

String in Julia must be wrapped in double quotes ". ' are reserved for characters.

You want something like this

mutable struct EgList

   array::Vector{Any}

   EgList() = new([])

end

function insert!(a::EgList, pos, val)

    if pos - length(a.array) == 1

        push!(a.array, val)

    elseif pos == 1

        a.array = vcat([val], a.array)

    else

        a.array = vcat(a.array[1:pos-1], [val], a.array[pos:end])

    end

    a

end

A = EgList()

insert!(A,1,0.2)        #  A = [0.2]

insert!(A,1,0.1)        #  A = [0.1 0.2]

insert!(A,1,0.1)        #  A = [0.1 0.1 0.2]  # allow double entries

insert!(A,2,0.3)        #  A = [0.1 0.3 0.1 0.2]

A = EgList()

insert!(A,1,("toto"))

insert!(A,2,("titi", 1.0)) # allow multi-parameter and -type
1 Like