我正在尝试打印出一个双向链表。我能够从头到尾打印列表。然而,当我尝试从头到尾打印只有头部打印。这是代码的最后一部分。我已经包含了链接列表代码。有什么建议吗?
*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
答案 0 :(得分:1)
您使PREV
指针无效,从而破坏了反向链接:
*CHANGE TAIL POINTER TO NEW END
TAIL => CURRENT
NULLIFY(CURRENT%NEXT)
NULLIFY(CURRENT%PREV) <--- THE PROBLEM LIES HERE