
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


include "cowgol.coh";
 
typedef QueueData is uint8; # the queue will contain bytes
include "queue.coh"; # from the Queue/Definition task
 
var queue := MakeQueue();
 
# enqueue bytes 0 to 20
print("Enqueueing: ");
var n: uint8 := 0; 
while n < 20 loop
    print_i8(n);
    print_char(' ');
    Enqueue(queue, n);
    n := n + 1;
end loop;
print_nl();
 
# dequeue and print everything in the queue
print("Dequeueing: ");
while QueueEmpty(queue) == 0 loop
    print_i8(Dequeue(queue));
    print_char(' ');
end loop;
print_nl();
 
# free the queue
FreeQueue(queue);