Abstract Data Types in Fortran 77 (Fortran-II)?
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?
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.
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.
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.
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.
精彩评论