双向链表Fortran.从尾部到头部打印



我正在尝试打印出一个双向链表。我能够让列表从头到尾打印。但是,当我尝试从尾部到头部打印时,只有头部打印。这是代码的最后一部分。我已经包含了链接列表代码。有什么建议吗?

*INPUT UP TO EOF
   NULLIFY(HEAD,TAIL)
   DO WHILE(.TRUE.)
       READ(1,*,END=999)NAMEIN
       READ(1,*,END=999)AGEIN
*INITIALIZE THE POINTERS TO NULL
      NULLIFY(CURRENT)
      ALLOCATE(CURRENT)
      CURRENT%PERSON = NAMEIN
      CURRENT%AGE = AGEIN 
*IF THERE IS NOT AT LEAST ONE NODE
      IF(.NOT.ASSOCIATED(HEAD))THEN
          HEAD => CURRENT
          TAIL => CURRENT
          NULLIFY(HEAD%NEXT,HEAD%PREV)
*IF THE CURRENT IS LAST NODE      
      ELSE
*PLACE AT END OF LIST
          TAIL%NEXT =>CURRENT
         CURRENT%PREV=>TAIL
          NULLIFY(CURRENT%NEXT)
*CHANGE TAIL POINTER TO NEW END
          TAIL => CURRENT
          NULLIFY(CURRENT%NEXT)
          NULLIFY(CURRENT%PREV)
     END IF    
  END DO
999    CONTINUE
*PRINT FROM HEAD TO TAIL      
*POINT TO THE BEGINNING
  NULLIFY(TEMP)   
  TEMP => HEAD
*PRINT EACH NODE
  PRINT *,'-----PRINTING fROM HEAD TO TAIL-----'
  DO WHILE(ASSOCIATED(TEMP))
      PRINT 5,TEMP%PERSON
      PRINT 10,TEMP%AGE
      TEMP => TEMP%NEXT
  END DO
*PRINT FROM TAIL TO HEAD      
*POINT TO THE BEGINNING   
*PRINT EACH NODE
  CURRENT => TAIL
  PRINT *,'-----PRINTING fROM TAIL TO HEAD------'
  DO WHILE(ASSOCIATED(cCURRENT))
      PRINT 5,CURRENT%PERSON
      PRINT 10,CURRENT%AGE
      CURRENT => CURRENT%PREV
  END DO    

您将PREV指针作废,从而破坏反向链接:

*CHANGE TAIL POINTER TO NEW END
          TAIL => CURRENT
          NULLIFY(CURRENT%NEXT)
          NULLIFY(CURRENT%PREV) <--- THE PROBLEM LIES HERE

相关内容

  • 没有找到相关文章

最新更新