turnerdaniet
Programmer
I am learning linked lists in fortran and I am working on a program now that reads from a file. Inside the file is a name followed by an age on the next line. the program reads all of the names, and ages until it reaches the end of file. it then prints the names and ages from head to tail. my linked list is not storing information properly. any help or guidance will be appreciated. below is my code
PROGRAM MAIN
*STRUCTURES
TYPE NODE
TYPE(NODE), POINTER :: NEXT, PREV
CHARACTER PERSON*30
INTEGER AGE
END TYPE NODE
*Declarations
CHARACTER INFILE *30, OUTFILE*30, NAMEIN*30
INTEGER MORE, INCOUNT, TOPRINT, AGEIN
LOGICAL EXISTS
TYPE(NODE), POINTER :: TEMP, HEAD, TAIL, CURRENT
*initializations
MORE = 1
EXISTS = .TRUE.
ALLOCATE(HEAD)
ALLOCATE(TAIL)
ALLOCATE(CURRENT)
NULLIFY(HEAD%NEXT,HEAD%PREV)
NULLIFY(TAIL%NEXT,TAIL%PREV)
NULLIFY(CURRENT%NEXT,CURRENT%PREV)
*MAIN
*PROMPT FOR USER INPUT FILENAME
DO WHILE(MORE.EQ.1)
PRINT *,'PLEASE ENTER AN INPUT FILENAME'
READ (*,*) INFILE
IF(INFILE.EQ.'QUIT'.OR.INFILE.EQ.'quit')THEN
MORE = 0
*CHECK FOR EXISTENCE OF INPUT FILE
ELSE
INQUIRE(FILE=INFILE,EXIST=EXISTS)
IF(.NOT.EXISTS)THEN
PRINT*,'THE FILENAME GIVEN DOES NOT EXIST'
PRINT*,'THE PROGRAM HAS TERMINATED'
MORE = 0
*IF IT EXISTS, OPEN THE FILE
ELSE
OPEN(UNIT=1,FILE=INFILE,STATUS='OLD')
MORE = 0
END IF
END IF
END DO
MORE = 1
*PROMPT FOR USER OUTPUT FILENAME
DO WHILE(MORE.EQ.1)
PRINT*,'PLEASE ENTER AN OUTPUT FILENAME'
READ (*,*) OUTFILE
IF(INFILE.EQ.'QUIT'.OR.INFILE.EQ.'quit')THEN
MORE = 0
*CHECK FOR EXISTENCE OF OUTPUT FILE
ELSE
INQUIRE(FILE=OUTFILE,EXIST=EXISTS)
IF(.NOT.EXISTS)THEN
*IF IT DOESN'T EXIST, OPEN AS NEW
OPEN(UNIT=2,FILE=OUTFILE,STATUS='NEW')
MORE = 0
ELSE
*OTHERWISE OPEN AS OLD
OPEN(UNIT=2,FILE=OUTFILE,STATUS ='OLD')
MORE = 0
END IF
END IF
END DO
*INPUT UP TO EOF
DO WHILE(.TRUE.)
READ(1,*,END=999)NAMEIN
READ(1,*,END=999)AGEIN
NULLIFY(CURRENT,HEAD,TAIL)
ALLOCATE(CURRENT)
IF(ASSOCIATED(HEAD))THEN
TAIL%NEXT=>CURRENT
CURRENT%PREV=>TAIL
NULLIFY(CURRENT%NEXT)
TAIL=>CURRENT
NULLIFY(CURRENT%NEXT)
NULLIFY(CURRENT%PREV)
ELSE
*EMPTY LINKED LIST
HEAD=>CURRENT
TAIL=>CURRENT
NULLIFY(HEAD%NEXT)
NULLIFY(HEAD%PREV)
END IF
CURRENT%PERSON = NAMEIN
CURRENT%AGE = AGEIN
END DO
999 CONTINUE
*PRINT FROM HEAD TO TAIL
*POINT TO THE BEGINNING
PRINT EACH NODE
PRINT *,'PRINTING OUTPUT FILE'
DO WHILE(ASSOCIATED(CURRENT%NEXT))
PRINT *,CURRENT%PERSON
PRINT *,CURRENT%AGE
CURRENT => CURRENT%NEXT
END DO
*END OF PROGRAM
STOP
END
PROGRAM MAIN
*STRUCTURES
TYPE NODE
TYPE(NODE), POINTER :: NEXT, PREV
CHARACTER PERSON*30
INTEGER AGE
END TYPE NODE
*Declarations
CHARACTER INFILE *30, OUTFILE*30, NAMEIN*30
INTEGER MORE, INCOUNT, TOPRINT, AGEIN
LOGICAL EXISTS
TYPE(NODE), POINTER :: TEMP, HEAD, TAIL, CURRENT
*initializations
MORE = 1
EXISTS = .TRUE.
ALLOCATE(HEAD)
ALLOCATE(TAIL)
ALLOCATE(CURRENT)
NULLIFY(HEAD%NEXT,HEAD%PREV)
NULLIFY(TAIL%NEXT,TAIL%PREV)
NULLIFY(CURRENT%NEXT,CURRENT%PREV)
*MAIN
*PROMPT FOR USER INPUT FILENAME
DO WHILE(MORE.EQ.1)
PRINT *,'PLEASE ENTER AN INPUT FILENAME'
READ (*,*) INFILE
IF(INFILE.EQ.'QUIT'.OR.INFILE.EQ.'quit')THEN
MORE = 0
*CHECK FOR EXISTENCE OF INPUT FILE
ELSE
INQUIRE(FILE=INFILE,EXIST=EXISTS)
IF(.NOT.EXISTS)THEN
PRINT*,'THE FILENAME GIVEN DOES NOT EXIST'
PRINT*,'THE PROGRAM HAS TERMINATED'
MORE = 0
*IF IT EXISTS, OPEN THE FILE
ELSE
OPEN(UNIT=1,FILE=INFILE,STATUS='OLD')
MORE = 0
END IF
END IF
END DO
MORE = 1
*PROMPT FOR USER OUTPUT FILENAME
DO WHILE(MORE.EQ.1)
PRINT*,'PLEASE ENTER AN OUTPUT FILENAME'
READ (*,*) OUTFILE
IF(INFILE.EQ.'QUIT'.OR.INFILE.EQ.'quit')THEN
MORE = 0
*CHECK FOR EXISTENCE OF OUTPUT FILE
ELSE
INQUIRE(FILE=OUTFILE,EXIST=EXISTS)
IF(.NOT.EXISTS)THEN
*IF IT DOESN'T EXIST, OPEN AS NEW
OPEN(UNIT=2,FILE=OUTFILE,STATUS='NEW')
MORE = 0
ELSE
*OTHERWISE OPEN AS OLD
OPEN(UNIT=2,FILE=OUTFILE,STATUS ='OLD')
MORE = 0
END IF
END IF
END DO
*INPUT UP TO EOF
DO WHILE(.TRUE.)
READ(1,*,END=999)NAMEIN
READ(1,*,END=999)AGEIN
NULLIFY(CURRENT,HEAD,TAIL)
ALLOCATE(CURRENT)
IF(ASSOCIATED(HEAD))THEN
TAIL%NEXT=>CURRENT
CURRENT%PREV=>TAIL
NULLIFY(CURRENT%NEXT)
TAIL=>CURRENT
NULLIFY(CURRENT%NEXT)
NULLIFY(CURRENT%PREV)
ELSE
*EMPTY LINKED LIST
HEAD=>CURRENT
TAIL=>CURRENT
NULLIFY(HEAD%NEXT)
NULLIFY(HEAD%PREV)
END IF
CURRENT%PERSON = NAMEIN
CURRENT%AGE = AGEIN
END DO
999 CONTINUE
*PRINT FROM HEAD TO TAIL
*POINT TO THE BEGINNING
PRINT EACH NODE
PRINT *,'PRINTING OUTPUT FILE'
DO WHILE(ASSOCIATED(CURRENT%NEXT))
PRINT *,CURRENT%PERSON
PRINT *,CURRENT%AGE
CURRENT => CURRENT%NEXT
END DO
*END OF PROGRAM
STOP
END