Advertisement
Guest User

newq

a guest
Oct 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. module intqueue
  2. implicit none
  3.  
  4. type :: queue
  5. integer :: n, i
  6. integer, allocatable :: a(:)
  7.  
  8. contains
  9. procedure :: push
  10. procedure :: pop
  11. procedure :: empty
  12. final :: free
  13. end type queue
  14.  
  15. interface queue
  16. module procedure newQueue
  17. end interface queue
  18.  
  19. contains
  20.  
  21. !########################################################
  22. !######################CONSTRUCTORS######################
  23. !########################################################
  24.  
  25. function newQueue(n) result(res)
  26. implicit none
  27.  
  28. type(queue) :: res
  29. integer, intent(IN) :: n
  30.  
  31. res%n = n
  32. res%i = 1
  33. allocate(res%a(n))
  34.  
  35. end function newQueue
  36.  
  37. !########################################################
  38. !########################METHODS#########################
  39. !########################################################
  40.  
  41. subroutine push(o, element)
  42. implicit none
  43.  
  44. integer, intent(IN) :: element
  45. class(queue), intent(INOUT) :: o
  46.  
  47. o%a(o%i) = element
  48. o%i = o%i + 1
  49.  
  50. end subroutine push
  51.  
  52. function pop(o) result(element)
  53. implicit none
  54.  
  55. integer :: element, i
  56. class(queue), intent(INOUT) :: o
  57.  
  58. element = o%a(1)
  59.  
  60. do i=1,size(o%a)-1
  61. o%a(i) = o%a(i+1)
  62. enddo
  63.  
  64. o%i = o%i - 1
  65.  
  66.  
  67. end function pop
  68.  
  69. function empty(o) result(r)
  70. implicit none
  71.  
  72. class(queue), intent(INOUT) :: o
  73. logical :: r
  74.  
  75. r = o%ipop-o%ipush .eq. 0
  76.  
  77. end function empty
  78.  
  79. subroutine free(o)
  80. implicit none
  81.  
  82. type(queue), intent(INOUT) :: o
  83.  
  84. if (allocated(o%a)) deallocate(o%a)
  85.  
  86. end subroutine free
  87.  
  88. end module intqueue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement