Note that there are some explanatory texts on larger screens.

plurals
  1. POfortran : trying to make a minimal stack datastructure
    primarykey
    data
    text
    <p>A question about stacks on SO, finally! All my life has led me to this point. </p> <p>So I needed to incorporate some rather large custom datastructure I made into a stack. I decided to write a minimal stack structure consisting of one integer value only. Here it is -</p> <pre><code>MODULE STACK_MODULE IMPLICIT NONE TYPE ELEMENT_TYPE INTEGER(4) :: VAL TYPE(ELEMENT_TYPE), POINTER :: PREV END TYPE ELEMENT_TYPE TYPE STACK_TYPE INTEGER(4) :: SIZE=0 TYPE(ELEMENT_TYPE), POINTER :: LASTIN END TYPE STACK_TYPE CONTAINS SUBROUTINE PUSH(VAL_,STACK) IMPLICIT NONE INTEGER(4), INTENT(IN) :: VAL_ TYPE(STACK_TYPE), INTENT(INOUT) :: STACK TYPE(ELEMENT_TYPE),TARGET :: CURRENT ! INIT CURRENT CURRENT%VAL = VAL_ CURRENT%PREV =&gt; STACK%LASTIN ! ADD CURRENT TO STACK STACK%LASTIN =&gt; CURRENT STACK%SIZE = STACK%SIZE+1 RETURN END SUBROUTINE PUSH SUBROUTINE POP(STACK,VAL_) IMPLICIT NONE TYPE(STACK_TYPE), INTENT(INOUT) :: STACK INTEGER(4) , INTENT(OUT) :: VAL_ TYPE(ELEMENT_TYPE), POINTER :: B4LASTIN !WRITE TO VAL_ IF (ASSOCIATED(STACK%LASTIN)) THEN VAL_ = STACK%LASTIN%VAL !TAKE OUT THE LAST-IN ELEMENT B4LASTIN =&gt; STACK%LASTIN%PREV STACK%LASTIN =&gt; B4LASTIN STACK%SIZE = STACK%SIZE-1 ELSE IF (STACK%SIZE.NE.0) THEN PRINT*, STACK%SIZE STOP 'MISMATCH BETWEEN STACKSIZE AND POINTER: BAD BOOK KEEPING!' END IF END IF RETURN END SUBROUTINE POP END MODULE STACK_MODULE PROGRAM MAIN USE STACK_MODULE IMPLICIT NONE INTEGER(4) :: I,J TYPE(STACK_TYPE) :: STACK1 DO I = 1,10 CALL PUSH(I,STACK1) END DO DO WHILE (ASSOCIATED(STACK1%LASTIN)) CALL POP(STACK1,J) PRINT*, J END DO END PROGRAM </code></pre> <p>The result surprised me considerably! I had doubts about whether the <code>SIZE</code> counter would keep up .. but not this.</p> <pre><code> 10 -1076898780 1752444 -1219604480 1651076143 stderr Program received signal SIGSEGV: Segmentation fault - invalid memory reference. Backtrace for this error: #0 0xB777FD63 #1 0xB77803F0 #2 0xB78713FF #3 0x8048666 in __stack_module_MOD_pop #4 0x80487A3 in MAIN__ at prog.f95:? </code></pre> <p>Any pointers (<em>pauses while giggling uncontrollably</em>) on where I went wrong? <a href="http://ideone.com/xMJ2mW" rel="nofollow">Here is the link to the code on IDEONE.</a><br> EDIT: <a href="http://ideone.com/xMJ2mW" rel="nofollow">The IDEONE link</a> now contains the updated version of the code, with the solution incorporated into it! Hope it can be reused as a generic template for stacks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload