Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Doubly Linked List From Tail to Head

Status
Not open for further replies.

turnerdaniet

Programmer
May 3, 2012
3
US
I AM TRYING TO PRINT OUT A DOUBLY LINKED LIST. I AM ABLE TO GET THE LIST TO PRINT FROM HEAD TO TAIL.. HOWEVER WHEN I PRINT TAIL TO HEAD ONLY THE HEAD WILL PRINT. THIS IS THE LAST PART OF THE CODE. I HAVE INCLUDED THE LINK LIST CODE. ANY SUGGESTIONS?

*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(TEMP))
PRINT 5,CURRENT%PERSON
PRINT 10,CURRENT%AGE
CURRENT => CURRENT%PREV
END DO
 
Code:
      PRINT *,'-----PRINTING fROM TAIL TO HEAD------'
      DO WHILE(ASSOCIATED(TEMP))  !<--- TEMP ??
          PRINT 5,CURRENT%PERSON
          PRINT 10,CURRENT%AGE
          CURRENT => CURRENT%PREV
      END DO

Norbert




The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
It should be current. I have fixed that. It was was from previously when i used temp in place of current such as the head to tail loop
 
You are right. TEMP is left disassociated from the printing head to tail, so this loop should not execute at all, if TEMP was used.

But I Found this in your code

Code:
   *CHANGE TAIL POINTER TO NEW END
              TAIL => CURRENT
              NULLIFY(CURRENT%NEXT)
              NULLIFY(CURRENT%PREV)
         END IF    
      END DO

Seems that you nullify TAIL%PREV any time you add a new item to the list, so the back link does not exist.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top