我可以仅使用 ALLOCATABLE 变量而不使用指针在 Fortran 中创建链表吗?

Can I create a Linked list in Fortran only using ALLOCATABLE variables and not pointers?

我可以这样创建一个没有指针的 Fortran 链表吗:

列表:

TYPE Allocation_List   

  PRIVATE  
  CLASS(*), ALLOCATABLE :: Item
  CLASS(Allocation_List), ALLOCATABLE :: Next
  
  CONTAINS
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: IsItem   => IsItem_AllocationList
    PROCEDURE, PASS(self), PUBLIC                  :: SetItem  => SetItem_AllocationList
    PROCEDURE, PASS(self), PUBLIC                  :: GetItem  => GetItem_AllocationList
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: FreeItem => FreeItem_AllocationList
    
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: IsNext   => IsNext_AllocationList
    PROCEDURE, PASS(self), PUBLIC                  :: SetNext  => SetNext_AllocationList
    PROCEDURE, PASS(self), PUBLIC                  :: GetNext  => GetNext_AllocationList
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: FreeNext => FreeNext_AllocationList
    
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: Reset    => Reset_AllocationList
    
    PROCEDURE, PASS(self), PUBLIC                  :: Display  => Display_AllocationListItem

END TYPE Allocation_List

链表:

TYPE Allocation_LinkedList

  PRIVATE  
  CLASS(Allocation_List), ALLOCATABLE :: HeadList
  CLASS(Allocation_List), ALLOCATABLE :: CurrList
  CLASS(Allocation_List), ALLOCATABLE :: TailList  
  
  CONTAINS
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: IsSet     => IsSet_AllocationLinkedList
    
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: Initiate  => Initiate_AllocationLinkedList
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: Connect   => Connect_AllocationLinkedList

    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: GetCurr   => GetCurrent_AllocationLinkedList
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: ForCurr   => ForwardCurrent_AllocationLinkedList
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: DispCurr  => DisplayCurrent_AllocationLinkedList
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: ResetCurr => ResetCurrent_AllocationLinkedList
        
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: Reset     => Reset_AllocationLinkedList
    PROCEDURE, PASS(self), PUBLIC, NON_OVERRIDABLE :: Display   => Display_AllocationLinkedList


END TYPE Allocation_LinkedList

这个List用来创建Linked List可以吗?可能会出什么问题,有什么缺点吗? Pointers 相对于 Allocatables 有什么优势?

  • LinkedList的TailList应该是一个Pointer。 CurrList 可以是 ALLOCATABLE 或 POINTER。

  • CurrList ALLOCATABLE 的缺点是 1) 为每个 Connect 重置为 HeadList 和 2) 用于在 CurrList 下存储部分链表的额外副本(内存)。

下面是使用可分配表和指针的链表的实现: https://github.com/AkhilAkkapelli/DataStructures/tree/main/LinkedList