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
20 character (len=128) :: units, description, stagger
24 type q_item ! Wrapper for item to be stored in the queue
26 type (q_item), pointer :: next
29 type queue ! The queue object, defined by a head and tail pointer
30 type (q_item), pointer :: head, tail
38 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41 ! Purpose: To initialize a queue
42 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
48 type (queue), intent(inout) :: q
57 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
60 ! Purpose: To insert an item in the tail of the queue
61 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
62 subroutine q_insert(q, qitem)
67 type (queue), intent(inout) :: q
68 type (q_data), intent(in) :: qitem
71 type (q_item), pointer :: newitem
76 if (.not.associated(q%tail)) then
84 q%length = q%length + 1
86 end subroutine q_insert
89 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
92 ! Purpose: This function returns FALSE if the queue is empty and TRUE otherwise
93 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
99 type (queue), intent(in) :: q
106 if (associated(q%head) .and. (q%length >= 1)) then
110 end function q_isdata
113 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
116 ! Purpose: To return the item in the head of the queue, without
117 ! actually removing the item
118 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
124 type (queue), intent(in) :: q
127 type (q_data) :: q_peek
129 if (associated(q%head)) then
132 call mprintf(.true.,ERROR,'q_peek(): Trying to peek at an empty queue')
138 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
141 ! Purpose: To return the number of items currently in the queue
142 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
148 type (queue), intent(in) :: q
151 ! type (q_item), pointer :: cursor
156 ! USE THE FOLLOWING TO COUNT THE LENGTH BY ACTUALLY TRAVERSING THE LINKED LIST
157 ! REPRESENTATION OF THE QUEUE
158 ! if (associated(q%head)) then
159 ! q_length = q_length + 1
161 ! do while(associated(cursor%next))
162 ! cursor=>cursor%next
163 ! q_length = q_length + 1
167 end function q_length
170 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
173 ! Purpose: To return the item stored at the head of the queue
174 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
180 type (queue), intent(inout) :: q
183 type (q_data) :: q_remove
184 type (q_item), pointer :: cursor
186 if (associated(q%head)) then
187 if (associated(q%head%next)) then
189 q_remove = q%head%data
193 q_remove = q%head%data
198 q%length = q%length - 1
200 call mprintf(.true.,ERROR,'q_remove(): Trying to remove from an empty queue')
203 end function q_remove
206 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
209 ! Purpose: To free all memory allocated by the queue, thus destroying any
210 ! items that have not been removed
211 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
212 subroutine q_destroy(q)
217 type (queue), intent(inout) :: q
220 type (q_item), pointer :: cursor
224 if (associated(q%head)) then
225 do while(associated(q%head%next))
233 end subroutine q_destroy
235 end module queue_module