Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Header file QueueAsArray
- #ifndef H_QueueAsArray
- #define H_QueueAsArray
- #include <iostream>
- #include <vector>
- #include <cassert>
- #include "queueADT.h"
- using namespace std;
- //*************************************************************
- // Author: D.S. Malik
- //
- // This class specifies the basic operation on a queue as an
- // array.
- //*************************************************************
- template <class Type>
- class queueType: public queueADT<Type>
- {
- public:
- const queueType<Type>& operator=(const queueType<Type>&);
- //Overload the assignment operator.
- bool isEmptyQueue() const;
- //Function to determine whether the queue is empty.
- //Postcondition: Returns true if the queue is empty,
- // otherwise returns false.
- bool isFullQueue() const;
- //Function to determine whether the queue is full.
- //Postcondition: Returns true if the queue is full,
- // otherwise returns false.
- void initializeQueue();
- //Function to initialize the queue to an empty state.
- //Postcondition: count = 0; queueFront = 0;
- // queueRear = maxQueueSize – 1
- Type front() const;
- //Function to return the first element of the queue.
- //Precondition: The queue exists and is not empty.
- //Postcondition: If the queue is empty, the program
- // terminates; otherwise, the first element of
- // the queue is returned.
- Type back() const;
- //Function to return the last element of the queue.
- //Precondition: The queue exists and is not empty.
- //Postcondition: If the queue is empty, the program
- // terminates; otherwise, the last element of
- // the queue is returned.
- void addQueue(const Type& queueElement);
- //Function to add queueElement to the queue.
- //Precondition: The queue exists and is not full.
- //Postcondition: The queue is changed and queueElement
- // is added to the queue.
- void deleteQueue();
- //Function to remove the first element of the queue.
- //Precondition: The queue exists and is not empty.
- //Postcondition: The queue is changed and the first
- // element is removed from the queue.
- queueType(int queueSize = 100);
- //Constructor
- queueType(const queueType<Type>& otherQueue);
- //Copy constructor
- ~queueType();
- //Destructor
- void displayInfo(); // displays the values of queueFront and queueRear
- // add the prototype for the function moveNthFront here
- void moveNthFront(int position);
- private:
- int maxQueueSize; //variable to store the maximum queue size
- int count; //variable to store the number of
- //elements in the queue
- int queueFront; //variable to point to the first
- //element of the queue
- int queueRear; //variable to point to the last
- //element of the queue
- Type *list; //pointer to the array that holds
- //the queue elements
- };
- template <class Type>
- void queueType<Type>::displayInfo()
- {
- cout << "queueFront: " << queueFront << " " << "queueRear: " << queueRear << endl;
- }
- template <class Type>
- bool queueType<Type>::isEmptyQueue() const
- {
- return (count == 0);
- } //end isEmptyQueue
- template <class Type>
- bool queueType<Type>::isFullQueue() const
- {
- return (count == maxQueueSize);
- } //end isFullQueue
- template <class Type>
- void queueType<Type>::initializeQueue()
- {
- queueFront = 0;
- queueRear = maxQueueSize - 1;
- count = 0;
- } //end initializeQueue
- template <class Type>
- Type queueType<Type>::front() const
- {
- assert(!isEmptyQueue());
- return list[queueFront];
- } //end front
- template <class Type>
- Type queueType<Type>::back() const
- {
- assert(!isEmptyQueue());
- return list[queueRear];
- } //end back
- template <class Type>
- void queueType<Type>::addQueue(const Type& newElement)
- {
- if (!isFullQueue())
- {
- queueRear = (queueRear + 1) % maxQueueSize; //use mod
- //operator to advance queueRear
- //because the array is circular
- count++;
- list[queueRear] = newElement;
- }
- else
- cout << "Cannot add to a full queue." << endl;
- } //end addQueue
- template <class Type>
- void queueType<Type>::deleteQueue()
- {
- if (!isEmptyQueue())
- {
- count--;
- queueFront = (queueFront + 1) % maxQueueSize; //use the
- //mod operator to advance queueFront
- //because the array is circular
- }
- else
- cout << "Cannot remove from an empty queue" << endl;
- } //end deleteQueue
- //Constructor
- template <class Type>
- queueType<Type>::queueType(int queueSize)
- {
- if (queueSize <= 0)
- {
- cout << "Size of the array to hold the queue must "
- << "be positive." << endl;
- cout << "Creating an array of size 100." << endl;
- maxQueueSize = 100;
- }
- else
- maxQueueSize = queueSize; //set maxQueueSize to
- //queueSize
- queueFront = 0; //initialize queueFront
- queueRear = maxQueueSize - 1; //initialize queueRear
- count = 0;
- list = new Type[maxQueueSize]; //create the array to
- //hold the queue elements
- } //end constructor
- //Destructor
- template <class Type>
- queueType<Type>::~queueType()
- {
- delete [] list;
- } //end destructor
- template <class Type>
- const queueType<Type>& queueType<Type>::operator=
- (const queueType<Type>& otherQueue)
- {
- if (this != &otherQueue) //avoid self-copy
- {
- maxQueueSize = otherQueue.maxQueueSize;
- queueFront = otherQueue.queueFront;
- queueRear = otherQueue.queueRear;
- count = otherQueue.count;
- delete [] list;
- list = new Type[maxQueueSize];
- //copy other queue in this queue
- if (count != 0)
- for (int j = queueFront; j != (queueRear+1)%maxQueueSize;j = (j + 1) % maxQueueSize)
- list[j] = otherQueue.list[j];
- } //end if
- return *this;
- } //end assignment operator
- template <class Type>
- queueType<Type>::queueType(const queueType<Type>& otherQueue)
- {
- maxQueueSize = otherQueue.maxQueueSize;
- queueFront = otherQueue.queueFront;
- queueRear = otherQueue.queueRear;
- count = otherQueue.count;
- list = new Type[maxQueueSize];
- //copy other queue in this queue
- for (int j = queueFront; j != (queueRear+1)%maxQueueSize;j = (j + 1) % maxQueueSize)
- list[j] = otherQueue.list[j];
- } //end copy constructor
- // add the definition for the function moveNthFront here
- template <class Type>
- void queueType<Type>::moveNthFront(int position) {
- int pos = ((queueFront+position)%maxQueueSize)-1;
- if (position < count) {
- Type temp = front();
- Type before;
- list[queueFront] = list[pos];
- for (int j = queueFront+1; j != (queueRear + 1) % maxQueueSize; j = (j + 1) % maxQueueSize) {
- before = list[(j + 1) % maxQueueSize];
- if (queueFront + 1 == j) {
- before = list[j];
- list[j] = temp;
- }
- //before = list[(j + 1) % maxQueueSize];
- list[(j + 1) % maxQueueSize] = before;
- }
- }
- else {
- cout << position << " enter position less than " << count << endl;
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement