private int dataBUFFERSIZE Use an array to store data int numitems Number of

Private int databuffersize use an array to store data

This preview shows page 18 - 21 out of 21 pages.

private : int data[BUFFER_SIZE]; // Use an array to store data int num_items; // Number of items on the queue int first; // Index of the first item; start from 0 public : // CONSTRUCTOR member functions int_queue(); // Default constructor // ACCESSOR member functions: const => won't modify data members bool empty() const ; // Check if the queue is empty bool full() const ; // Check if the queue is full int size() const ; // Give the number of data currently stored int front() const ; // Retrieve the value of the front item // MUTATOR member functions void enqueue( int ); // Add a new item to the back of the queue void dequeue(); // Remove the front item from the queue }; { kccecia, lixin, mak } @cse.ust.hk COMP2011 (Fall 2017) p.18
Image of page 18
Circular Queue of int Data — Test Program #include "int-queue.h" /* File: int-queue-test.cpp */ void print_queue_info( const int_queue& a) { cout << "No. of data currently on the queue = " << a.size() << "\t" ; if (!a.empty()) cout << "Front item = " << a.front(); cout << endl << "Empty: " << boolalpha << a.empty(); cout << "\t\t" << "Full: " << boolalpha << a.full() << endl << endl; } int main() { int_queue a; print_queue_info(a); a.enqueue(4); print_queue_info(a); a.enqueue(15); print_queue_info(a); a.enqueue(26); print_queue_info(a); a.enqueue(37); print_queue_info(a); a.dequeue(); print_queue_info(a); a.enqueue(48); print_queue_info(a); a.enqueue(59); print_queue_info(a); a.dequeue(); print_queue_info(a); a.dequeue(); print_queue_info(a); a.dequeue(); print_queue_info(a); a.dequeue(); print_queue_info(a); a.dequeue(); print_queue_info(a); return 0; } /* compile: g++ -L. -o int-queue-test int-queue-test.cpp -lintqueue */ { kccecia, lixin, mak } @cse.ust.hk COMP2011 (Fall 2017) p.19
Image of page 19
Circular Queue of int Data — Constructors, Assessors #include "int-queue.h" /* File: int-queue1.cpp */ /***** Default CONSTRUCTOR member function *****/ // Create an empty queue int_queue::int_queue() { first = 0; num_items = 0; } /***** ACCESSOR member functions *****/ // Check if the int_queue is empty bool int_queue::empty() const { return (num_items == 0); } // Check if the int_queue is full bool int_queue::full() const { return (num_items == BUFFER_SIZE); } // Give the number of data currently stored int int_queue::size() const { return num_items; } // Retrieve the value of the front item int int_queue::front() const { if (!empty()) return data[first]; cerr << "Warning: Queue is empty; can't retrieve any data!" << endl; exit(-1); } { kccecia, lixin, mak } @cse.ust.hk COMP2011 (Fall 2017) p.20
Image of page 20
Circular Queue of int Data — Mutators #include "int-queue.h" /* File: int-queue2.cpp */ void int_queue::enqueue( int x) // Add a new item to the back of the queue { if (!full()) { data[(first+num_items) % BUFFER_SIZE] = x; ++num_items; } else { cerr << "Error: Queue is full; can't add (" << x << ")!" << endl; exit(-1); } } void int_queue::dequeue() // Remove the front item from the queue { if (!empty()) {
Image of page 21

You've reached the end of your free preview.

Want to read all 21 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes