view algs4-c++/src/CircularQueue.cpp @ 20:86281e8f24ba

1.3.39 CircularQueue (ring buffer)
author Eris Caffee <discordia@eldalin.com>
date Mon, 22 Jun 2015 15:38:25 -0500
parents
children
line source
1 // g++ -std=c++11 CircularQueue.cpp
3 #include <cstddef>
4 #include <cstdlib>
5 #include <iostream>
7 #include "CircularQueue.hpp"
9 int main( int argc, char **argv ) {
10 size_t max;
11 std::cin >> max;
12 if ( ! std::cin.good() ) {
13 std::cerr << "You must enter a size for the queue." << std::endl;
14 exit( EXIT_FAILURE );
15 }
17 CircularQueue<long> queue( max );
19 long i;
20 while ( ! std::cin.eof() ) {
21 std::cin >> i;
22 if ( std::cin.good() ) {
23 if ( queue.full() )
24 std::cout << "Queue is full: old data lost" << std::endl;
25 queue.enqueue(i);
26 }
27 }
29 std::cout << "Queue has " << queue.size() << " entries." << std::endl;
31 std::cout << "Dequeuing entries:" << std::endl;
32 while ( ! queue.empty() ) {
33 long i = queue.dequeue();
34 std::cout << i << std::endl;
35 }
37 }