3

我可以用这种方式创建一个没有指针的 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

这个列表在用于创建链接列表时会起作用吗?什么可能出错,有什么缺点吗?指针相对于可分配对象的优势是什么?

4

1 回答 1

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

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

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

于 2021-10-13T02:12:00.117 回答