
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.
 Illustration of FIFO behavior
Task

Create a queue data structure and demonstrate its operations.

(For implementations of queues, see the FIFO task.)



Operations:

   push       (aka enqueue) - add element
   pop         (aka dequeue) - pop first element
   empty     - return truth value when empty


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


module fifo_nodes
  type fifo_node
     integer :: datum
     ! the next part is not variable and must be present
     type(fifo_node), pointer :: next
     logical :: valid
  end type fifo_node
end module fifo_nodes
 
program FIFOTest
  use fifo
  implicit none
 
  type(fifo_head) :: thehead
  type(fifo_node), dimension(5) :: ex, xe
  integer :: i
 
  call new_fifo(thehead)
 
  do i = 1, 5
     ex(i)%datum = i
     call fifo_enqueue(thehead, ex(i))
  end do
 
  i = 1
  do
     call fifo_dequeue(thehead, xe(i))
     print *, xe(i)%datum
     i = i + 1
     if ( fifo_isempty(thehead) ) exit
  end do
 
end program FIFOTest