An Introduction to Queues
With Examples in C++ |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
Values: | Homogeneous elements of any type. | ||||||||||||||||||
Operations: |
|
In addition,
if the stack has a "size limit", one could add an
isFull()
operation; some people add a
peek()
operation; and some people add a
makeEmpty()
operation.
#ifndef edu_jmu_cs_Queue_h #define edu_jmu_cs_Queue_h const int maxqueue = 10; /** * A Queue (FIFO list) class * * @author Prof. David Bernstein, James Madison University */ class Queue { public: /** * Default constructor */ Queue(); /** * Remove and return an element from the front of the queue * * @return The front element */ int dequeue(void); /** * Append an element onto the end of the queue * * @param item The element to append */ void enqueue(int item); /** * Is this Queue empty? * * @return true if the queue is empty and false otherwise */ bool isEmpty(void); private: int count; // The array containing the items int entry[maxqueue]; }; #endif
#include "Queue.h" /** * This implementation uses an array and "shuffles" the entries * every time an element is served */ Queue::Queue() { count = 0; } int Queue::dequeue(void) { int i, item; if (count <= 0) { item = -1; } else { item = entry[0]; count--; for (i = 0; i < count; i++) entry[i] = entry[i + 1]; } return item; } void Queue::enqueue(int item) { if (count < maxqueue) { entry[count] = item; count++; } } bool Queue::isEmpty(void) { bool result; if (count == 0) result = true; else result = false; return result; }
#ifndef edu_jmu_cs_Queue_h #define edu_jmu_cs_Queue_h const int maxqueue = 10; /** * A Queue (FIFO list) class. * * @author Prof. David Bernstein, James Madison University */ class Queue { public: /** * Default constructor */ Queue(); /** * Remove and return an element from the front of the queue * * @return The front element */ int dequeue(void); /** * Append an element onto the end of the queue * * @param item The element to append */ void enqueue(int item); /** * Is this Queue empty? * * @return true if the queue is empty and false otherwise */ bool isEmpty(void); private: int count, front, rear; // The array containing the items int entry[maxqueue]; }; #endif
#include "Queue.h" /** * * This implementation uses an array and a front and rear * pointer. When the rear pointer reaches the maximum value, the * contents are moved down (if space permits). */ Queue::Queue() { rear = 0; front = 0; count = 0; // What do you think about this? } int Queue::dequeue(void) { int item; if (count <= 0) { item = -1; } else { item = entry[front]; count--; front++; } return item; } void Queue::enqueue(int item) { int i; if (count < maxqueue) { entry[rear] = item; count++; rear++; if ((rear == maxqueue) && (front > 0)) { for (i = 0; i < count; i++) entry[i] = entry[front + i]; front = 0; rear = count; } } } bool Queue::isEmpty(void) { bool result; if (count == 0) result = true; else result = false; return result; }
#ifndef edu_jmu_cs_Queue_h #define edu_jmu_cs_Queue_h const int maxqueue = 10; /** * A Queue (FIFO list) class. * * @author Prof. David Bernstein, James Madison University */ class Queue { public: /** * Default constructor. */ Queue(); /** * Remove and return an element from the front of the queue. * * @return The front element */ int dequeue(void); /** * Append an element onto the end of the queue. * * @param item The element to append */ void enqueue(int item); /** * Is this Queue empty? * * @return true if the queue is empty and false otherwise */ bool isEmpty(void); private: int count, front, rear; // The array containing the items int entry[maxqueue]; }; #endif
#include "Queue.h" /** * This implementation uses "circular" array. */ Queue::Queue() { rear = maxqueue - 1; front = 0; count = 0; // What do you think about this? } int Queue::dequeue(void) { int item; if (count <= 0) { item = -1; } else { item = entry[front]; count--; front = ((front + 1) == maxqueue) ? 0 : (front + 1); } return item; } void Queue::enqueue(int item) { if (count < maxqueue) { count++; rear = ((rear + 1) == maxqueue) ? 0 : (rear + 1); // Another way: // // if ((rear+1) == maxqueue) rear = 0; // else rear++; // // A third way: // // rear = ((rear + 1) % maxqueue); entry[rear] = item; } } bool Queue::isEmpty(void) { return (count == 0); // Understand? }