Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module list
- use environment
- implicit none
- integer, parameter :: name_len = 15
- type file
- character(name_len, kind = CH_) :: name = ""
- type(file), pointer :: next => Null()
- type(file), pointer :: previous => Null()
- end type
- type files
- integer :: count = 0
- type(file), pointer :: start => null()
- type(file), pointer :: end => null()
- type(file), pointer :: curr => null()
- end type
- contains
- pure subroutine add_file(list, f)
- type(files), intent(inout) :: list
- type(file), intent(inout), target :: f
- type(file), pointer :: tmp
- if(associated(list%end)) then
- call add_with_sort(list, list%start, f)
- else
- list%start => f
- list%end => f
- list%count = list%count + 1
- end if
- end subroutine
- pure subroutine relate(f1, f2)
- type(file), intent(inout), target :: f1, f2
- f1%next => f2
- f2%previous => f1
- end subroutine
- !СУПЕР-ПУПЕР-ГИПЕР ФУНКЦИЯ СОРТИРОВКИ ЧЕРЕЗ РЕКУРСИЮ И ЕСЛИ ОНА НЕ ЧИСТАЯ, Я ПОВЕШУСЬ, НУ ЧИСТАЯ ЖЕ НУ!!!!
- pure recursive subroutine add_with_sort(list, f1, f2)
- type(files), intent(inout) :: list
- type(file), intent(inout), target :: f1, f2
- type(file), pointer :: tmp1, tmp2
- if(.not. associated(f1%previous)) then
- if(f1%name > f2%name) then
- tmp1 => f1
- list%start => f2
- call relate(f2, f1)
- else
- if(.not. associated(f1%next)) then
- call relate(f1, f2)
- return
- else
- call add_with_sort(list, f1%next, f2)
- end if
- end if
- else
- if(f1%name > f2%name) then
- tmp1 => f1%previous
- call relate(tmp1, f2)
- call relate(f2, f1)
- else
- if(.not. associated(f1%next)) then
- call relate(f1, f2)
- return
- else
- call add_with_sort(list, f1%next, f2)
- end if
- end if
- end if
- end subroutine
- function next(List) result(curr)
- type(files), intent(inout) :: List
- type(file), pointer :: curr
- if(associated(List%curr)) then
- curr => List%curr%next
- List%curr => curr%next
- else
- curr => List%start
- List%curr => curr%next
- end if
- end function
- function Read_file_list(input_file) result(list)
- character(*), intent(in) :: input_file
- type(files), pointer :: list
- allocate(list)
- open(newunit = in, file = input_file, encoding = E_)
- call Read_file(in, list)
- close(in)
- end function
- recursive subroutine Read_file(in, list)
- integer, intent(in) :: in
- type(files), intent(inout) :: list
- type(file), pointer :: f
- character(:), allocatable :: format
- character(name_len, kind = CH_) :: str
- integer io, pos
- format = "(a)"
- allocate(f)
- read(in, format, iostat = io) str
- f%name = str
- if(io == 0) then
- call add_file(list, f)
- call Read_file(in, list)
- else
- deallocate(f)
- nullify(f)
- end if
- end subroutine
- subroutine Write_list(output_file, list, pos)
- character(*), intent(in) :: output_file
- type(files), intent(inout) :: list
- character(*), intent(in) :: pos
- type(file), pointer :: curr
- open(newunit = out, file = output_file, encoding = E_, position = pos)
- write(out,*)
- curr => list%start
- call write_item(out, curr)
- close(out)
- end subroutine
- recursive subroutine write_item(out, f)
- integer, intent(in) :: out
- type(file), intent(in) :: f
- type(file), pointer :: tmp
- character(:), allocatable :: format
- format = "(2(a, 1x))"
- tmp => f%next
- write(out, format) f%name
- if(associated(tmp)) &
- call write_item(out, tmp)
- end subroutine
- end module
- PROGRAM Lab2_1
- use list
- use environment
- implicit none
- character(*), parameter :: data_file = "../data/product.dat"
- character(*), parameter :: upd_file = "../data/updates"
- type(files), pointer :: linked_list => Null()
- type(files), pointer :: linked_list2 => Null()
- linked_list => Read_file_list(upd_file)
- call Write_list(output, linked_list, "rewind")
- end program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement