Akhil Akkapelli
Akhil Akkapelli

Reputation: 176

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

Can I create a Fortran Linked List without pointers in this way:

List:

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

Linked 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

Will this List when used to create Linked List work? What may go wrong and are there any disadvantages? What are the advantages of Pointers over Allocatables?

Upvotes: 3

Views: 232

Answers (1)

Akhil Akkapelli
Akhil Akkapelli

Reputation: 176

  • The TailList of the Linked List should be a Pointer. The CurrList can be an ALLOCATABLE or POINTER.

  • The CurrList ALLOCATABLE has a disadvantage of 1) Reset to HeadList for every Connect and 2) Extra copy(memory) used to store part of Linked List under the CurrList.

Here is the implementation of Linked List using Allocatables and Pointers: https://github.com/AkhilAkkapelli/DataStructures/tree/main/LinkedList

Upvotes: 0

Related Questions