2012-02-07 23 views
7

मैं एक अनिर्धारित लंबाई के डेटा की सरणी रखने के लिए फोरट्रान में लिंक्ड सूचियों का उपयोग करना चाहता हूं।फोरट्रान लिंक्ड सूचियों को कैसे हटाता है?

TYPE linked_list 
    INTEGER :: data 
    TYPE(linked_list) :: next_item => NULL() 
END TYPE 

अब मैं इस तरह के एक सूची बना कहते हैं::

मैं निम्नलिखित सेटअप

TYPE(LINKED_LIST) :: example_list 
example_list%data =1 
ALLOCATE(example_list%next_item) 
example_list%next_item%data = 2 
ALLOCATE(example_list%next_item%next_item) 
example_list%next_item%next_item%data = 3 

मेरे सवाल है, अगर मैं प्रदर्शन:

DEALLOCATE(example_list) 

होगा सब घोंसले के स्तर को भी हटा दिया जा सकता है या क्या मुझे सूची को सबसे गहरे तत्व में पार करने की आवश्यकता है और गहरे ग्यारहों से हटाना है ऊपर की ओर?

+4

की तरह यह एक लंबे समय के बाद से मैं फोरट्रान में ऐसा किया गया है, लेकिन मैं यकीन है कि आप मैन्युअल रूप से पुनःआवंटन करने के लिए है हूँ। यदि आप केवल सिर को हटा देते हैं, तो आप संदर्भ खो देंगे और मेमोरी लीक लेंगे। – ChrisF

+0

हां। मैं उससे बहुत डरता था। मुझे हालांकि कहना होगा, मुझे परेशानी हो रही है, वाक्यांश क्या है, अपना खुद का कचरा संग्रह रोलिंग? – EMiller

+0

आप मेमोरी प्रबंधित फोर्टन को लागू नहीं कर सकते हैं। –

उत्तर

9

आपको प्रत्येक नोड को मैन्युअल रूप से डिलीकेट करना होगा। यह वह जगह है जहां शैली की तरह "ऑब्जेक्ट उन्मुख" उपयोगी होती है।

module LinkedListModule 
    implicit none 
    private 

    public :: LinkedListType 
    public :: New, Delete 
    public :: Append 

    interface New 
     module procedure NewImpl 
    end interface 

    interface Delete 
     module procedure DeleteImpl 
    end interface 

    interface Append 
     module procedure AppendImpl 
    end interface 

    type LinkedListType 
     type(LinkedListEntryType), pointer :: first => null() 
    end type 

    type LinkedListEntryType 
     integer :: data 
     type(LinkedListEntryType), pointer :: next => null() 
    end type 

contains 

    subroutine NewImpl(self) 
     type(LinkedListType), intent(out) :: self 

     nullify(self%first) 
    end subroutine 

    subroutine DeleteImpl(self) 
     type(LinkedListType), intent(inout) :: self 

     if (.not. associated(self%first)) return 

     current => self%first 
     next => current%next 
     do 
      deallocate(current) 
      if (.not. associated(next)) exit 
      current => next 
      next => current%next 
     enddo 

    end subroutine 

    subroutine AppendImpl(self, value) 

     if (.not. associated(self%first)) then 
      allocate(self%first) 
      nullify(self%first%next) 
      self%first%value = value 
      return 
     endif 


     current => self%first 
     do 
      if (associated(current%next)) then 
       current => current%next 
      else 
      allocate(current%next) 
      current => current%next 
      nullify(current%next) 
      current%value = value 
      exit 
      endif 
     enddo 

    end subroutine 

end module 

सावधान: यह अतीत आधी रात है और मैं एक ब्राउज़र विंडो में कोडिंग के वास्तव में शौकीन नहीं हूँ। यह कोड काम नहीं कर सकता है। यह सिर्फ एक लेआउट है।

उपयोग इस

program foo 
    use LinkedListModule 
    type(LinkedListType) :: list 

    call New(list) 
    call Append(list, 3) 
    call Delete(list) 
end program 
+1

बिंगो। DeleteImpl विधि ठीक वही है जो मैं ढूंढ रहा था। इस ऑब्जेक्ट उन्मुख फोर्टन कितना अच्छा और साफ है। – EMiller

+1

@emiller: यह ऑब्जेक्ट उन्मुख नहीं है। यह वस्तु उन्मुख शैली है। –

+0

'सूची संलग्न करें' पूरी सूची के माध्यम से सिर से पूंछ तक, इसलिए यह बहुत अक्षम है।पूंछ नोड का ट्रैक रखना और नए नोड को इसमें जोड़ना बेहतर है। 'LinkedListEntryType' से 'LinkedListType' को अलग क्यों करें? –

संबंधित मुद्दे