In a traditional linked list, the "only" way to access element N is to
traverse the N-1 previous elements. That's not entirely true, of course.
You can have dynamically maintained pointers to the midpoint of the list,
for instance, in which case you don't have to look at all the previous
elements. But the philosophy is still the same - you have to start at some
node, and traverse the pointers until you get to your desired node. The
logic is straightforward. You can then encapsulate this procedure with a
call to something like GetElement(Head,k), or somesuch, which will give you
the illusion of random access like an array.
On the other hand, you can, as you rightly pointed out, create an
allocatable vector (or pointer) of type element. Accessing the k-th element
would then be "built-in". And you could use something like the reallocate()
function in Numerical Recipes to handle resizing.
To my mind, the easiest to implement is the reallocateable vector; however,
that depends on what you want to do with the elements. If you're going to do
a lot of inserting of elements, perhaps because of h-p adaption, you may
want to consider the encapsulated approach and the linked list. You will,
however, almost certainly be slower than with a reallocatable array. If
speed is of the essence, do the reallocatable arrays. And you can still do
adaptivity - it's just trickier.
I hope this gives you some insight! Let us know...
Alvaro Fernandez
Rice University
Jacques Brancher wrote:
> Hi,
>
> I wrote a computer code that manipulates a linked list with
> the following structure:
>
> type element
> integer :: nelm
> integer :: typ
> integer :: region
> integer :: nnodes
> integer,dimension(:),pointer :: neighbour
> type(element),pointer :: next
> end type element
>
> What I'm looking for is a way to access a single element of the
> linked list (like a vector of the structure above).
>
> Another choice is to create a allocatable vector that increase its
> dimension with each insertion I do.
>
> Which is the easiest choice? how may I do that?
>
> Thanks in advance and apologize my english.
>
> Jacques.
> ________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|