Question

In: Computer Science

Having Trouble with this C++ assignment THe places that are marked // TODO is where code...

Having Trouble with this C++ assignment THe places that are marked // TODO is where code should be filled in at

Header (myQueue.h)

#ifndef _MYQUEUE_H_
#define _MYQUEUE_H_

using namespace std;

template
class myQueue {
public:
   myQueue(int maxSz);
   ~myQueue();
   void enqueue(T item);
   T dequeue();
int currentSize();
bool isEmpty();
bool isFull();

private:
   T *contents; /*Dynamic initiate (C++ keyword new) the holder array*/
   int front,rear; /*Index in the array of the front and rear element*/
   int arrayLength; /*The length of the contents holder array*/
       /* Keep in mind that the Queue will only hold up to (arrayLength - 1) elements*/
};

template
myQueue::myQueue(int maxSz) {
   // TODO
}

template
myQueue::~myQueue() {
   // TODO
}

template
void myQueue::enqueue(T item) {
   // TODO
}

template
T myQueue::dequeue() {
   // TODO
}

template
int myQueue::currentSize() {
   // TODO
}

template
bool myQueue::isEmpty() {
   // TODO
}

template
bool myQueue::isFull() {
   // TODO
}

#endif

Queue Test (queueTest.cpp)

#include
#include "myQueue.h"

using namespace std;

int main() {
   cout << "Testing the template myQueue, try an integer queue as an example..." << endl;
   cout << "Please enter the max size of the int queue: ";
   int capacity;
   cin >> capacity;
  
   myQueue testIntQ(capacity);
  
   while(1) {
       cout << "Please enter 'e' for enqueue, 'd' for dequeue, and 's' for stop." << endl;
       char userOption;
       cin >> userOption;
      
       if(userOption == 's')
           break;
          
       switch(userOption) {
           case 'e':
               if(!testIntQ.isFull()) {
                   cout << "Please enter the integer you want to enqueue: ";
                   int val;
                   cin >> val;
                   testIntQ.enqueue(val);
               }
               else
                   cout << "Cannot enqueue. The queue is full." << endl;
               break;
           case 'd':
               if(!testIntQ.isEmpty())
                   cout << testIntQ.dequeue() << " has been popped out." << endl;
               else
                   cout << "Cannot pop. The queue is empty." << endl;
               break;
           default:
               cout << "Illegal input character for options." << endl;
       }
   }  
  
   return 0;
}

(40’) In myQueue.h, implement the queue class template, myQueue. Keep in mind, the arrayLength needs to be one more than the capacity of the queue. Also, under this implementation, make sure your calculation of currentSize is correct, and the conditions for “Full” and “Empty” are correct. One shortcut could be: once you make sure currentSize() is implemented correctly, you might use it in isFull() and isEmpty(), and the number of elements in the queue must range from 0 to arrayLength – 1.

Solutions

Expert Solution

// myQueue.h

#ifndef _MYQUEUE_H_

#define _MYQUEUE_H_

template <class T>

class myQueue {

public:

   myQueue(int maxSz);

   ~myQueue();

   void enqueue(T item);

   T dequeue();

int currentSize();

bool isEmpty();

bool isFull();

private:

   T *contents; /*Dynamic initiate (C++ keyword new) the holder array*/

   int front,rear; /*Index in the array of the front and rear element*/

   int arrayLength; /*The length of the contents holder array*/

       /* Keep in mind that the Queue will only hold up to (arrayLength - 1) elements*/

};

// constructor to create a queue of size maxSz

template <class T>

myQueue<T>::myQueue(int maxSz) {

       arrayLength = maxSz;

       contents = new T[arrayLength];

       front = -1;

       rear = -1;

}

// destructor to deallocate the memory

template <class T>

myQueue<T>::~myQueue() {

       if(contents != NULL)

             delete [] contents;

}

// function to add the item at the end of queue

template <class T>

void myQueue<T>::enqueue(T item) {

       if(!isFull()) // check if queue is full

       {

             if(front == -1) // if queue is empty, insert it as the first element

             {

                    front++;

                    rear++;

                    contents[front]=item;

             }else /// else add the item at the end of queue

             {

                    rear++;

                    contents[rear] = item;

             }

       }

}

// function to delete and return the front element of the queue

template <class T>

T myQueue<T>::dequeue() {

   if(!isEmpty()) // check if queue is empty

   {

          T item = contents[front]; // get the front element

          front++;

          // if queue is empty, make front and rear = -1

          if(front > rear)

          {

                rear = -1;

                front = -1;

          }else

          {

                // move the elements to the left

                for(int i=front-1;i<rear;i++)

                       contents[i] = contents[i+1];

                front--;

                rear--;

          }

          return item; // return the item

   }

   return T(NULL);

}

// function to return the current size of the queue

template <class T>

int myQueue<T>::currentSize() {

   return (rear+1);

}

// function to return if the queue is empty or not

template <class T>

bool myQueue<T>::isEmpty() {

   return(front == -1);

}

// function to return if queue is full or not

template <class T>

bool myQueue<T>::isFull() {

   return(rear == (arrayLength-1));

}

#endif

//end of myQueue.h

//queueTest.cpp : C++ program to test the MyQueue class

#include <iostream>

#include "myQueue.h"

using namespace std;

int main() {

   cout << "Testing the template myQueue, try an integer queue as an example..." << endl;

   cout << "Please enter the max size of the int queue: ";

   int capacity;

   cin >> capacity;

   myQueue<int> testIntQ(capacity);

   while(1) {

       cout << "Please enter 'e' for enqueue, 'd' for dequeue, and 's' for stop." << endl;

       char userOption;

       cin >> userOption;

       if(userOption == 's')

           break;

       switch(userOption) {

           case 'e':

               if(!testIntQ.isFull()) {

                   cout << "Please enter the integer you want to enqueue: ";

                   int val;

                   cin >> val;

                   testIntQ.enqueue(val);

               }

               else

                   cout << "Cannot enqueue. The queue is full." << endl;

               break;

           case 'd':

             if(!testIntQ.isEmpty())

                   cout << testIntQ.dequeue() << " has been popped out." << endl;

               else

                   cout << "Cannot pop. The queue is empty." << endl;

               break;

           default:

               cout << "Illegal input character for options." << endl;

       }

   }

   return 0;

}

//end of queueTest.cpp

Output:


Related Solutions

I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I am having trouble with my assignment and getting compile errors on the following code. The...
I am having trouble with my assignment and getting compile errors on the following code. The instructions are in the initial comments. /* Chapter 5, Exercise 2 -Write a class "Plumbers" that handles emergency plumbing calls. -The company handles natural floods and burst pipes. -If the customer selects a flood, the program must prompt the user to determine the amount of damage for pricing. -Flood charging is based on the numbers of damaged rooms. 1 room costs $300.00, 2 rooms...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips) You will need to create a NOS table and use the structure below and write a function called product. The structure used in this program is: struct Values { short left; short right; int...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
Sherald is having trouble finding a procedural code in the Alphabetic Index for removal of a...
Sherald is having trouble finding a procedural code in the Alphabetic Index for removal of a cataract. What are some options and/or alternative ways she can perform an Alphabetic Index search?
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so it has a field called frequency and initialize it as one. Add a search function. If a number is in the list, return its frequency. Modify Insert function. Remove push, append, and insertAfter as one function called insert(self, data). If you insert a data that is already in the list, simply increase this node’s frequency. If the number is not in the list, add...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics {...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics { /** * Calculates and returns the complement of a DNA sequence. In DNA sequences, 'A' and 'T' are * complements of each other, as are 'C' and 'G'. The complement is formed by taking the * complement of each symbol (e.g., the complement of "GTCA" is "CAGT"). * * @param dna a char array representing a DNA sequence of arbitrary length, * containing only...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT