1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4 ! Description: This module implements a queue of user-defined data types and
5 ! a set of routines related to the maintenance and manipulation of the queue.
6 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12 type q_data ! The user-defined datatype to store in the queue
19 character (len=128) :: units, description, stagger
23 type q_item ! Wrapper for item to be stored in the queue
25 type (q_item), pointer :: next
28 type queue ! The queue object, defined by a head and tail pointer
29 type (q_item), pointer :: head, tail
37 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
40 ! Purpose: To initialize a queue
41 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
47 type (queue), intent(inout) :: q
56 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59 ! Purpose: To insert an item in the tail of the queue
60 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
61 subroutine q_insert(q, qitem)
66 type (queue), intent(inout) :: q
67 type (q_data), intent(in) :: qitem
70 type (q_item), pointer :: newitem
75 if (.not.associated(q%tail)) then
83 q%length = q%length + 1
85 end subroutine q_insert
88 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
91 ! Purpose: This function returns FALSE if the queue is empty and TRUE otherwise
92 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
98 type (queue), intent(in) :: q
105 if (associated(q%head) .and. (q%length >= 1)) then
109 end function q_isdata
112 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
115 ! Purpose: To return the item in the head of the queue, without
116 ! actually removing the item
117 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
123 type (queue), intent(in) :: q
126 type (q_data) :: q_peek
128 if (associated(q%head)) then
131 call mprintf(.true.,ERROR,'q_peek(): Trying to peek at an empty queue')
137 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
140 ! Purpose: To return the number of items currently in the queue
141 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
147 type (queue), intent(in) :: q
150 ! type (q_item), pointer :: cursor
155 ! USE THE FOLLOWING TO COUNT THE LENGTH BY ACTUALLY TRAVERSING THE LINKED LIST
156 ! REPRESENTATION OF THE QUEUE
157 ! if (associated(q%head)) then
158 ! q_length = q_length + 1
160 ! do while(associated(cursor%next))
161 ! cursor=>cursor%next
162 ! q_length = q_length + 1
166 end function q_length
169 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
172 ! Purpose: To return the item stored at the head of the queue
173 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
179 type (queue), intent(inout) :: q
182 type (q_data) :: q_remove
183 type (q_item), pointer :: cursor
185 if (associated(q%head)) then
186 if (associated(q%head%next)) then
188 q_remove = q%head%data
192 q_remove = q%head%data
197 q%length = q%length - 1
199 call mprintf(.true.,ERROR,'q_remove(): Trying to remove from an empty queue')
202 end function q_remove
205 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
208 ! Purpose: To free all memory allocated by the queue, thus destroying any
209 ! items that have not been removed
210 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
211 subroutine q_destroy(q)
216 type (queue), intent(inout) :: q
219 type (q_item), pointer :: cursor
223 if (associated(q%head)) then
224 do while(associated(q%head%next))
232 end subroutine q_destroy
234 end module queue_module