
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


import system'collections;
import extensions;
 
public program()
{
    // Create a queue and "push" items into it
    var queue := new Queue();
 
    queue.push:1;
    queue.push:3;
    queue.push:5;
 
    // "Pop" items from the queue in FIFO order
    console.printLine(queue.pop()); // 1
    console.printLine(queue.pop()); // 3
    console.printLine(queue.pop()); // 5
 
    // To tell if the queue is empty, we check the count
    console.printLine("queue is ",(queue.Length == 0).iif("empty","nonempty"));
 
    // If we try to pop from an empty queue, an exception
    // is thrown.
    queue.pop() | on:(e){ console.writeLine:"Queue empty." }
}