
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


/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
-- Queue Usage Demonstration Program -------------------------------------------
method main(args = String[]) public constant
  kew = RCQueueImpl()
  do
    say kew.pop()
  catch ex = IndexOutOfBoundsException
    say ex.getMessage
    say
  end
 
  melancholyDane = ''
  melancholyDane[0] = 4
  melancholyDane[1] = 'To be'
  melancholyDane[2] = 'or'
  melancholyDane[3] = 'not to be?'
  melancholyDane[4] = 'That is the question.'
 
  loop p_ = melancholyDane[0] to 1 by -1
    kew.push(melancholyDane[p_])
    end p_
 
  loop while \kew.empty
    popped = kew.pop
    say popped '\-'
    end
  say; say
 
  -- demonstrate stowing something other than a text string in the queue
  kew.push(melancholyDane)
  md = kew.pop
  loop l_ = 1 to md[0]
    say md[l_] '\-'
    end l_
  say
 
  return
 
-- Queue implementation --------------------------------------------------------
class RCQueueImpl
  properties private
    qqq = Deque
 
method RCQueueImpl() public
  qqq = ArrayDeque()
  return
 
method push(stuff) public
  qqq.push(stuff)
  return
 
method pop() public returns Rexx signals IndexOutOfBoundsException
  if qqq.isEmpty then signal IndexOutOfBoundsException('The queue is empty')
  return Rexx qqq.pop()
 
method empty() public binary returns boolean
  return qqq.isEmpty
 
method isTrue public constant binary returns boolean
  return 1 == 1
 
method isFalse public constant binary returns boolean
  return \isTrue
 