
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


procedure main(arglist)
queue := []
write("Usage:\nqueue x x x - x - - - - -\n\t- pops elements\n\teverything else pushes")
write("Queue is:")
every x := !arglist do {
   case x of {
      "-"     : pop(queue)  | write("pop(empty) failed.")    # pop if the next arglist[i] is a -
      default : put(queue,x)                                 # push arglist[i]
      }
   if empty(queue) then writes("empty")
   else every writes(!queue," ")
   write()
   }
end
 
procedure empty(X)        #: fail if X is not empty
if *X = 0 then return 
end