Advertisement
Guest User

qqqqqqqq

a guest
Oct 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. module intqueue
  2. implicit none
  3.  
  4. type :: queue
  5. integer :: n, ipop, ipush
  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%ipop = 1
  33. res%ipush = 1
  34. allocate(res%a(n))
  35.  
  36. end function newQueue
  37.  
  38. !########################################################
  39. !########################METHODS#########################
  40. !########################################################
  41.  
  42. subroutine push(o, element)
  43. implicit none
  44.  
  45. integer, intent(IN) :: element
  46. class(queue), intent(INOUT) :: o
  47.  
  48. o%a(o%ipush) = element
  49. o%ipush = o%ipush + 1
  50.  
  51. end subroutine push
  52.  
  53. function pop(o) result(element)
  54. implicit none
  55.  
  56. integer :: element
  57. class(queue), intent(INOUT) :: o
  58.  
  59. element = o%a(o%ipop)
  60. o%ipop = o%ipop + 1
  61. o%ipush = o%ipush - 1
  62.  
  63. end function pop
  64.  
  65. function empty(o) result(r)
  66. implicit none
  67.  
  68. class(queue), intent(INOUT) :: o
  69. logical :: r
  70.  
  71. r = o%ipop-o%ipush .eq. 0
  72.  
  73. end function empty
  74.  
  75. subroutine free(o)
  76. implicit none
  77.  
  78. type(queue), intent(INOUT) :: o
  79.  
  80. if (allocated(o%a)) deallocate(o%a)
  81.  
  82. end subroutine free
  83.  
  84. end module intqueue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement