
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 <queue>
#include <cassert> // for run time assertions
 
int main()
{
  std::queue<int> q;
  assert( q.empty() );        // initially the queue is empty
 
  q.push(1);                  // add an element
  assert( !q.empty() );       // now the queue isn't empty any more
  assert( q.front() == 1 );   // the first element is, of course, 1
 
  q.push(2);                  // add another element
  assert( !q.empty() );       // it's of course not empty again
  assert( q.front() == 1 );   // the first element didn't change
 
  q.push(3);                  // add yet an other element
  assert( !q.empty() );       // the queue is still not empty
  assert( q.front() == 1 );   // and the first element is still 1
 
  q.pop();                    // remove the first element
  assert( !q.empty() );       // the queue is not yet empty
  assert( q.front() == 2);    // the first element is now 2 (the 1 is gone)
 
  q.pop();
  assert( !q.empty() );
  assert( q.front() == 3);
 
  q.push(4);
  assert( !q.empty() );
  assert( q.front() == 3);
 
  q.pop();
  assert( !q.empty() );
  assert( q.front() == 4);
 
  q.pop();
  assert( q.empty() );
 
  q.push(5);
  assert( !q.empty() );
  assert( q.front() == 5);
 
  q.pop();
  assert( q.empty() );
}