
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


class LinkedQueue(T) {
    private static struct Node {
        T data;
        Node* next;
    }
 
    private Node* head, tail;
 
    bool empty() { return head is null; }
 
    void push(T item) {
        if (empty())
            head = tail = new Node(item);
        else {
            tail.next = new Node(item);
            tail = tail.next;
        }
    }
 
    T pop() {
        if (empty())
            throw new Exception("Empty LinkedQueue.");
        auto item = head.data;
        head = head.next;
        if (head is tail) // Is last one?
            // Release tail reference so that GC can collect.
            tail = null;
        return item;
    }
 
    alias push enqueue;
    alias pop dequeue;
}
 
void main() {
    auto q = new LinkedQueue!int();
    q.push(10);
    q.push(20);
    q.push(30);
    assert(q.pop() == 10);
    assert(q.pop() == 20);
    assert(q.pop() == 30);
    assert(q.empty());
}