Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Mar 31st, 2020 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Header file QueueAsArray
  2.  
  3. #ifndef H_QueueAsArray
  4. #define H_QueueAsArray
  5.  
  6. #include <iostream>
  7. #include <vector>
  8. #include <cassert>
  9.  
  10. #include "queueADT.h"
  11.  
  12. using namespace std;
  13.  
  14. //*************************************************************
  15. // Author: D.S. Malik
  16. //
  17. // This class specifies the basic operation on a queue as an
  18. // array.
  19. //*************************************************************
  20.  
  21. template <class Type>
  22. class queueType: public queueADT<Type>
  23. {
  24. public:
  25.     const queueType<Type>& operator=(const queueType<Type>&);
  26.       //Overload the assignment operator.
  27.  
  28.     bool isEmptyQueue() const;
  29.       //Function to determine whether the queue is empty.
  30.       //Postcondition: Returns true if the queue is empty,
  31.       //    otherwise returns false.
  32.  
  33.     bool isFullQueue() const;
  34.       //Function to determine whether the queue is full.
  35.       //Postcondition: Returns true if the queue is full,
  36.       //    otherwise returns false.
  37.  
  38.     void initializeQueue();
  39.       //Function to initialize the queue to an empty state.
  40.       //Postcondition: count = 0; queueFront = 0;
  41.       //    queueRear = maxQueueSize – 1
  42.  
  43.     Type front() const;
  44.       //Function to return the first element of the queue.
  45.       //Precondition: The queue exists and is not empty.
  46.       //Postcondition: If the queue is empty, the program
  47.       //    terminates; otherwise, the first element of
  48.       //    the queue is returned.  
  49.     Type back() const;
  50.       //Function to return the last element of the queue.
  51.       //Precondition: The queue exists and is not empty.
  52.       //Postcondition: If the queue is empty, the program
  53.       //    terminates; otherwise, the last element of  
  54.       //    the queue is returned.
  55.  
  56.     void addQueue(const Type& queueElement);
  57.       //Function to add queueElement to the queue.
  58.       //Precondition: The queue exists and is not full.
  59.       //Postcondition: The queue is changed and queueElement
  60.       //    is added to the queue.
  61.  
  62.     void deleteQueue();
  63.       //Function to remove the first element of the queue.
  64.       //Precondition: The queue exists and is not empty.
  65.       //Postcondition: The queue is changed and the first
  66.       //    element is removed from the queue.
  67.  
  68.     queueType(int queueSize = 100);
  69.       //Constructor
  70.  
  71.     queueType(const queueType<Type>& otherQueue);
  72.       //Copy constructor
  73.  
  74.     ~queueType();
  75.       //Destructor
  76.  
  77.     void displayInfo(); // displays the values of queueFront and queueRear
  78.  
  79.       // add the prototype for the function moveNthFront here
  80.     void moveNthFront(int position);
  81.  
  82. private:
  83.     int maxQueueSize; //variable to store the maximum queue size
  84.     int count;        //variable to store the number of
  85.                       //elements in the queue
  86.     int queueFront;   //variable to point to the first
  87.                       //element of the queue
  88.     int queueRear;    //variable to point to the last
  89.                       //element of the queue
  90.     Type *list;       //pointer to the array that holds
  91.                       //the queue elements
  92. };
  93.  
  94. template <class Type>
  95. void queueType<Type>::displayInfo()
  96. {
  97.     cout << "queueFront: " << queueFront << " " << "queueRear: " << queueRear << endl;
  98. }
  99.  
  100. template <class Type>
  101. bool queueType<Type>::isEmptyQueue() const
  102. {
  103.     return (count == 0);
  104. } //end isEmptyQueue
  105.  
  106. template <class Type>
  107. bool queueType<Type>::isFullQueue() const
  108. {
  109.     return (count == maxQueueSize);
  110. } //end isFullQueue
  111.  
  112. template <class Type>
  113. void queueType<Type>::initializeQueue()
  114. {
  115.     queueFront = 0;
  116.     queueRear = maxQueueSize - 1;
  117.     count = 0;
  118. } //end initializeQueue
  119.  
  120. template <class Type>
  121. Type queueType<Type>::front() const
  122. {
  123.     assert(!isEmptyQueue());
  124.     return list[queueFront];
  125. } //end front
  126.  
  127. template <class Type>
  128. Type queueType<Type>::back() const
  129. {
  130.     assert(!isEmptyQueue());
  131.     return list[queueRear];
  132. } //end back
  133.  
  134. template <class Type>
  135. void queueType<Type>::addQueue(const Type& newElement)
  136. {
  137.     if (!isFullQueue())
  138.     {  
  139.         queueRear = (queueRear + 1) % maxQueueSize; //use mod
  140.                             //operator to advance queueRear  
  141.                             //because the array is circular
  142.         count++;
  143.         list[queueRear] = newElement;
  144.     }
  145.     else
  146.         cout << "Cannot add to a full queue." << endl;
  147. } //end addQueue
  148.  
  149. template <class Type>
  150. void queueType<Type>::deleteQueue()
  151. {
  152.     if (!isEmptyQueue())
  153.     {  
  154.         count--;
  155.         queueFront = (queueFront + 1) % maxQueueSize; //use the
  156.                         //mod operator to advance queueFront
  157.                         //because the array is circular
  158.     }
  159.     else
  160.         cout << "Cannot remove from an empty queue" << endl;
  161. } //end deleteQueue
  162.  
  163.     //Constructor
  164. template <class Type>
  165. queueType<Type>::queueType(int queueSize)  
  166. {
  167.     if (queueSize <= 0)
  168.     {
  169.         cout << "Size of the array to hold the queue must "
  170.              << "be positive." << endl;
  171.         cout << "Creating an array of size 100." << endl;
  172.  
  173.         maxQueueSize = 100;
  174.     }
  175.     else
  176.         maxQueueSize = queueSize;   //set maxQueueSize to
  177.                                     //queueSize
  178.  
  179.     queueFront = 0;                 //initialize queueFront
  180.     queueRear = maxQueueSize - 1;   //initialize queueRear
  181.     count = 0;
  182.     list = new Type[maxQueueSize];  //create the array to
  183.                                     //hold the queue elements
  184. } //end constructor
  185.  
  186.     //Destructor
  187. template <class Type>
  188. queueType<Type>::~queueType()  
  189. {
  190.     delete [] list;
  191. } //end destructor
  192.  
  193. template <class Type>
  194. const queueType<Type>& queueType<Type>::operator=
  195.                        (const queueType<Type>& otherQueue)
  196. {
  197.     if (this != &otherQueue) //avoid self-copy
  198.     {
  199.         maxQueueSize = otherQueue.maxQueueSize;
  200.         queueFront = otherQueue.queueFront;
  201.         queueRear = otherQueue.queueRear;
  202.         count = otherQueue.count;
  203.  
  204.         delete [] list;
  205.         list = new Type[maxQueueSize];
  206.  
  207.         //copy other queue in this queue
  208.         if (count != 0)
  209.             for (int j = queueFront; j != (queueRear+1)%maxQueueSize;j = (j + 1) % maxQueueSize)
  210.                 list[j] = otherQueue.list[j];
  211.     } //end if
  212.  
  213.     return *this;
  214. } //end assignment operator
  215.  
  216. template <class Type>
  217. queueType<Type>::queueType(const queueType<Type>& otherQueue)
  218. {
  219.     maxQueueSize = otherQueue.maxQueueSize;
  220.     queueFront = otherQueue.queueFront;
  221.     queueRear = otherQueue.queueRear;
  222.     count = otherQueue.count;
  223.  
  224.     list = new Type[maxQueueSize];
  225.  
  226.     //copy other queue in this queue
  227.     for (int j = queueFront; j != (queueRear+1)%maxQueueSize;j = (j + 1) % maxQueueSize)
  228.         list[j] = otherQueue.list[j];
  229. } //end copy constructor
  230.  
  231. // add the definition for the function moveNthFront here
  232. template <class Type>
  233. void queueType<Type>::moveNthFront(int position) {
  234.  
  235.     int pos = ((queueFront+position)%maxQueueSize)-1;
  236.     if (position < count) {
  237.         Type temp = front();
  238.         Type before;
  239.         list[queueFront] = list[pos];
  240.  
  241.             for (int j = queueFront+1; j != (queueRear + 1) % maxQueueSize; j = (j + 1) % maxQueueSize) {
  242.  
  243.                 before = list[(j + 1) % maxQueueSize];
  244.                 if (queueFront + 1 == j) {
  245.                     before = list[j];
  246.                     list[j] = temp;
  247.                    
  248.                 }
  249.                 //before = list[(j + 1) % maxQueueSize];
  250.                 list[(j + 1) % maxQueueSize] = before;
  251.             }
  252.  
  253.     }
  254.     else {
  255.         cout << position << " enter position less than " << count << endl;
  256.     }
  257. }
  258.  
  259.  
  260. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top