Fortran使头指向第一个元素/添加到列表的末尾

时间:2018-08-01 10:22:20

标签: linked-list fortran fortran95

我正在尝试将稀疏向量实现为Fortran95中的链表。 List节点具有索引和组件值。当我尝试将向量保存为列表时,头最终指向添加的最后一个节点,而我似乎无法使其始终指向“最小索引”

我想要的是我的清单的结尾看起来像这样

head => node1 => node2 => node3 =>空

我目前(从输出中得到)

head => node3 =>节点2 =>节点1

MODULE sparse
IMPLICIT NONE
! INTERFACE OPERATOR (*)
 ! MODULE PROCEDURE vekmult
! END INTERFACE

TYPE content
    INTEGER:: idx
    REAL:: wert
END TYPE content

TYPE node
    TYPE(content):: komponente
    TYPE(node), POINTER:: next => NULL()
END TYPE node 

TYPE sparsevektor
    PRIVATE
    TYPE(node), POINTER:: head=> NULL()
END TYPE

CONTAINS

 SUBROUTINE lesevektor(vek)
    INTEGER:: i
    REAL:: v
    TYPE(sparsevektor), INTENT(INOUT):: vek
    TYPE(node), POINTER:: tmp, new, tail

    i=1
    DO
            READ(*,*) i,v
            IF(i>0) THEN
            ALLOCATE(new)
            new%komponente%idx=i
            new%komponente%wert=v
            IF (.not. associated(vek%head)) THEN
            vek%head=> new
            tail => new
            ELSE 
            tmp => vek%head
            vek%head => new
            vek%head%next => tmp
            END IF
            ELSE
            EXIT
            END IF
    END DO
END SUBROUTINE lesevektor


SUBROUTINE WRITEVEK(vek)
TYPE(sparsevektor), INTENT(IN):: vek
TyPE(node), POINTER:: tmp
 tmp => vek%head
 DO WHILE (ASSOCIATED(tmp))
    WRITE(*,*) tmp%komponente%idx, tmp%komponente%wert
    tmp => tmp%next
    END DO 
END SUBROUTINE WRITEVEK
 ! REAL FUNCTION(x,y)
    ! TYPE(sparsevektor), INTENT(in)::x,y
    ! REAL:: z


END MODULE sparse

PROGRAM vektor
USE sparse
IMPLICIT NONE

TYPE(sparsevektor):: vv

CALL lesevektor(vv)
CALL WRITEVEK(vv)

END PROGRAM vektor

1 个答案:

答案 0 :(得分:1)

我在上一条评论中的意思是:

MODULE sparse
IMPLICIT NONE
! INTERFACE OPERATOR (*)
 ! MODULE PROCEDURE vekmult
! END INTERFACE

TYPE content
    INTEGER:: idx
    REAL:: wert
END TYPE content

TYPE node
    TYPE(content):: komponente
    TYPE(node), POINTER:: next => NULL()
END TYPE node

TYPE sparsevektor
    PRIVATE
    TYPE(node), POINTER:: head=> NULL()
END TYPE

CONTAINS

 SUBROUTINE lesevektor(vek)
    INTEGER:: i
    REAL:: v
    TYPE(sparsevektor), INTENT(INOUT):: vek
    TYPE(node), POINTER:: tmp, new, tail

    i=1
    DO
            READ(*,*) i,v
            IF(i>0) THEN
              ALLOCATE(new)
              new%komponente%idx=i
              new%komponente%wert=v
              IF (.not. associated(vek%head)) THEN
                vek%head=> new
                tail => new
              ELSE
                tail%next => new      ! ==> new
                tail => new           ! ==> new
                !tmp => vek%head      ! ==> old
                !vek%head => new      ! ==> old
                !vek%head%next => tmp ! ==> old
              END IF
            ELSE
              EXIT
            END IF
    END DO
END SUBROUTINE lesevektor


SUBROUTINE WRITEVEK(vek)
TYPE(sparsevektor), INTENT(IN):: vek
TyPE(node), POINTER:: tmp
 tmp => vek%head
 DO WHILE (ASSOCIATED(tmp))
    WRITE(*,*) tmp%komponente%idx, tmp%komponente%wert
    tmp => tmp%next
    END DO
END SUBROUTINE WRITEVEK
 ! REAL FUNCTION(x,y)
    ! TYPE(sparsevektor), INTENT(in)::x,y
    ! REAL:: z


END MODULE sparse

PROGRAM vektor
USE sparse
IMPLICIT NONE

TYPE(sparsevektor):: vv

CALL lesevektor(vv)
CALL WRITEVEK(vv)

END PROGRAM vektor