views:

283

answers:

4

I'm attempting to work in Fotran 77, and I've found the need for a tree based data structure. Aside from implementing a tree with an array, is there any way to build a tree with pointer nodes to other nodes, as per a standard implementation in most languages?

The documentation for this beast is scarce, and there doesn't appear to be any standard structure type that would make this possible.

Thoughts?

+1  A: 

This would be much easier in Fortran 95/2003, which has user-defined derived types and pointer types. Using these features one can setup data structures such as linked lists and trees. (The pointer types are called pointers, but they are more like alias, in that pointer arithmetic isn't possible). Fortran >=95 has many improvements over Fortran 77. My recommendation is not to use Fortran 77 unless one is making minor modifications to legacy code that is in Fortran 77. A good book is "Fortran 95/2003 explained" by Metcalf, Reid and Cohen.

M. S. B.
This really doesn't help me at all. I need a fortran 77 solution.
Stefan Kendall
Why do you NEED a FORTRAN77 solution ? I'm with MSB on this. Is someone holding a gun to your head ?
High Performance Mark
Also: you asked for thoughts, and you've had mine and MSB's.
High Performance Mark
Also with these guys on this one ... why f77 ? What are the specifics that demand you must use the old standard ... expecially since f77 is a subset of f90 (completely), and will be so until ... I think f2008, or f2010.
ldigas
+5  A: 

Hi

I suggest you move to Fortran 90 or later. FORTRAN77 and earlier didn't have pointers in the language specification, so compiler writers (and users) came up with a whole raft of clever* ways of adding the necessary functionality to do just the sort of thing you want to do. Fortran 90 has proper pointers for dynamic data structures.

clever* means, of course requiring advanced programming skills and understanding of memory, pointers, referencing and de-referencing (all of which are alien to most Fortran programmers) with the inevitable consequence that clever* programs are not portable between compilers, nor between hardware platforms, nor between programmers.

I don't understand why you would be restricted to working in FORTRAN77 -- standard FORTRAN77 remains syntactically correct and compilable with Fortran 90 compilers. Sure, you have to integrate your new tree-processing code with the existing codebase in the old language, but that doesn't mean that you have to write new units in the old language.

And, in passing, FORTRAN77 was way more modern than FORTRANII.

Regards

Mark

High Performance Mark
Please pay this heed. Attempting something like this when updated versions of the languages handle it much better will lead to a parallel to the scene in The Fellowship of the Ring where Saruman proclaims that "You have chosen the way of pain!".
Tim Whitcomb
A: 

If you're really stuck with Fortran-77, you can use Cray Pointers:

http://gcc.gnu.org/onlinedocs/gfortran/Cray-pointers.html

Cray Pointers are non-standard and have some drawbacks, but they'll give you something similar to a C pointer. They're supported by gfortran and most commercial compilers.

With that said, you would probably be better off using newer Fortran features, like Fortran-90 pointers or the C-interoperability features in Fortran 2003.

Asher L.
A: 

Without Cray pointers or other hackery, the only way to implement a "data type" is with parallel arrays, each of which represents a field. An index, then, can refer to an instantiation of the data type.

Stefan Kendall