Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!aplcen!boingo.med.jhu.edu!haven!mimsy!oasys!dtoa1!roth From: roth@dtoa1.dt.navy.mil (Roth) Newsgroups: comp.lang.fortran Subject: Re: dynamic storage Message-ID: <6245@oasys.dt.navy.mil> Date: 5 Mar 91 19:51:14 GMT References: <38747@netnews.upenn.edu> Sender: news@oasys.dt.navy.mil Reply-To: roth@dtoa1.dt.navy.mil (Pete Roth) Organization: David Taylor Research Center, Bethesda, MD Lines: 48 In article <38747@netnews.upenn.edu> lmeyer@eniac.seas.upenn.edu (Basya Meyer) writes: >As a 'c' programmer who must work in fortran, I need your help! Is there >any way in fortran to create something (roughly approximating) a stack or >linked list? I would like to allocate spaces in memory as I go along. > >Thanks, > >Basya Assuming your compiler is Fortran 77 or earlier: Part 1: implement your stack as an array, and use an integer as a pointer into it. Briefly, without error checking, the data looks like integer stkptr real(?) stack( 100 ) data stkptr / 0 / & a push looks like subroutine push( areal ) if ( stkptr .LT. 100 ) then stkptr = stkptr + 1 stack( stkptr ) = areal else print *, 'stack overflow' endif end etc. Part 2: linked list as two arrays: first array is integer, and holds the index of the location of the datum in the second array. integer lstptr( 100 ) real(?) list( 100 ) an ordered list will have lstptr(1) = 1, lstptr(2) = 2, etc. Part 3: Allocate memory as you go along is not fortran unfortunately, but your system may have an interface that allows you to do that. Apologies for not having something I can just e-mail you. Recommended reading: Day, A. Colin, FORTRAN TECHNIQUES, Cambridge University Press, 32 E 57th St, NYNY, 1979 (latest edition) ISBN 0 521 09719 3 paper ISBN 0 521 08549 7 hardback Also: Kernighan & Plauger, SOFTWARE TOOLS, Addison Wesley (I think; it looks like someone stole my copy...) Peter N Roth roth@dtoa1.dt.navy.mil Objects in this office are closer than they appear.