I am creating a doubly linked list in fortran. Pretty much everything is working correctly. Although, I am having one issue... I build the list properly, but when printed to the screen, I get jumble for the first and last lines. Here is my code

        DO WHILE (.NOT. quit .AND. inStat .EQ. 0)
            ALLOCATE(cur)                                !create a new node
            NULLIFY(cur%prev,cur%next)                   !nullify the pointers
            IF (.NOT. ASSOCIATED(head)) THEN
                !start a new doubly linked list
                ALLOCATE(head)                           !create a head
                NULLIFY(head%prev,head%next)             !nullify head's pointers
                ALLOCATE(tail)                           !create a tail
                NULLIFY(tail%prev,tail%next)             !nullify tail's pointers
                head => cur                              !point head to cur
                READ(1,*,IOSTAT=inStat)cur%name          !store name in cur
                READ(1,*,IOSTAT=inStat)cur%cnt           !store count in cur
            ELSE
                cur%prev => tail                         !point cur's prev to the previous tail
                tail%next => cur                         !point tail's next to cur
                READ(1,*,IOSTAT=inStat)cur%name          !store name in cur
                READ(1,*,IOSTAT=inStat)cur%cnt           !store cnt in cur
            END IF
            tail => cur                                  !point tail to cur
        END DO
        cur => head                                      !point cur to head
        DO WHILE (ASSOCIATED(cur))                       !while there is more
            PRINT *,cur%name, ' ', cur%cnt
            cur => cur%next
        END DO

The input file contains the following:
nick
7
jim
5
mike
16
rob
15
steven
16
johnathan
-5

What is printed:

 J↓K l■(     ─ ( e┬æuƒ          704
 nick                                 7
 jim                                  5
 mike                                16
 rob                                 15
 steven                              16
 johnathan                           -5
 P_K └ K                              0

I've changed the code around a bit and fixed a few things and now I am getting the correct output. One problem, though, is that initially the program is not entering the IF(.NOT. Associated(head)) THEN structure.

      TYPE (node), POINTER      :: head, cur, tail      !pointers to the data
      TYPE (node), TARGET       :: new
      INTEGER                   :: cntTemp              !for data verification while importing from file

      !*****************************************************************
      !**************       BEGIN MAIN PROGRAM          ****************
      !*****************************************************************
      cur => new
      DO WHILE (.NOT. quit)
        CALL openInputFile(inFile, quit, inStat)
        !CALL openOutputFile(outFile, quit)
        DO WHILE (.NOT. quit .AND. inStat .EQ. 0)
            NULLIFY(cur%prev,cur%next)
            IF (.NOT. ASSOCIATED(head)) THEN
                !start a new doubly linked list
                PRINT *,'starting a new list'
                ALLOCATE(head)
                head => cur
                READ(1,*,IOSTAT=inStat)cur%name
                READ(1,*,IOSTAT=inStat)cntTemp
                IF (cntTemp .NE. 0) THEN
                    cur%cnt = cntTemp
                ELSE
                    WRITE(*,100)
100                 FORMAT('INVALID INPUT FROM FILE.')
                    quit = .TRUE.
                END IF
            ELSE
                ALLOCATE(cur)
                cur%prev => tail
                tail%next => cur
                READ(1,*,IOSTAT=inStat)cur%name
                READ(1,*,IOSTAT=inStat)cntTemp
                IF (cntTemp .NE. 0) THEN
                    cur%cnt = cntTemp
                ELSE
                    WRITE(*,100)
                    quit = .TRUE.
                END IF
            END IF
            tail => cur
        END DO
        IF (.NOT. quit) THEN
            cur => head                                 !point cur to head
        END IF
        PRINT *,"HEAD'S cnt: ", head%cnt
        DO WHILE (ASSOCIATED(cur) .AND. .NOT. quit)     !while there is more and quit wasn't entered
            IF(ASSOCIATED(cur%next) .AND. ASSOCIATED(cur%prev)) THEN
                PRINT *,cur%name, ' ', cur%cnt
            END IF
            cur => cur%next
        END DO
        CLOSE(1)
      END DO
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.