Question

In: Computer Science

Redo Program 1 so that you have a template class, that works with a char, string,...

Redo Program 1 so that you have a template class, that works with a char, string, int, or double data type.

Program 1 is below:

#include <iostream>

#include <string>

#define CAPACITY 12

using namespace std;

class queue {

private:

string strArray[CAPACITY];

int frontIndex, endIndex;

int queueSize;

public:

queue()

{

frontIndex = endIndex = -1;

queueSize = 0;

}

void enqueue(string);

string dequeue();

int size()

{

return queueSize;

}

int front()

{

return frontIndex;

}

int end()

{

return endIndex;

}

};

void queue::enqueue(string data)

{

if (endIndex == -1) {

frontIndex = endIndex = 0;

strArray[frontIndex] = data;

queueSize++;

}

else {

int pos = (endIndex + 1) % CAPACITY;

if (pos == frontIndex) {

cout << "OVERFLOW" << endl;

return;

}

else {

endIndex = pos;

strArray[pos] = data;

queueSize++;

}

}

}

string queue::dequeue()

{

if (frontIndex == -1) {

cout << "UNDERFLOW" << endl;

return "UNDERFLOW";

}

else {

if (frontIndex == endIndex) {

string element = strArray[frontIndex];

frontIndex = endIndex = -1;

queueSize = 0;

return element;

}

else {

string element = strArray[frontIndex];

frontIndex = (frontIndex + 1) % CAPACITY;

queueSize--;

return element;

}

}

}

int main()

{

cout << "Cathleen Espinoza - 10/1/20" << endl;

queue myQ;

cout << myQ.size() << endl; \

myQ.dequeue();

myQ.enqueue("Fred");

myQ.enqueue("Liv");

myQ.enqueue("Julie");

myQ.enqueue("Rich");

myQ.enqueue("William");

myQ.enqueue("Olo");

myQ.enqueue("Xi");

myQ.enqueue("Chu");

myQ.enqueue("Annie");

myQ.enqueue("Carlos");

myQ.enqueue("Tuyet");

myQ.enqueue("Sue");

myQ.enqueue("Penny");

cout << myQ.front() << endl;

cout << myQ.end() << endl;

cout << myQ.size() << endl;

cout << myQ.dequeue() << endl;

cout << myQ.dequeue() << endl;

cout << myQ.dequeue() << endl;

myQ.enqueue("Olive");

myQ.enqueue("Jim");

cout << myQ.dequeue() << endl;

cout << myQ.dequeue() << endl;

cout << myQ.front() << endl;

cout << myQ.end() << endl;

cout << myQ.size() << endl;

system("pause");

return 0;

}

Solutions

Expert Solution

Code After Modification:

#include <iostream>

#include <string>

#define CAPACITY 12

using namespace std;
template<class Type>
class queue {

private:

        Type array[CAPACITY];
        
        int frontIndex, endIndex;
        
        int queueSize;

public:

        queue()

        {

        frontIndex = endIndex = -1;

        queueSize = 0;

        }

        void enqueue(Type);

        Type dequeue();

        int size()

        {

                return queueSize;

        }

        int front()

        {

                return frontIndex;

        }

        int end()

        {

                return endIndex;
        
        }

};

template<class Type>
void queue<Type>::enqueue(Type data)

{

        if (endIndex == -1) {

        frontIndex = endIndex = 0;

        array[frontIndex] = data;

        queueSize++;

        }

        else {

        int pos = (endIndex + 1) % CAPACITY;

        if (pos == frontIndex) {

        cout << "OVERFLOW" << endl;

        return;

        }

        else {

        endIndex = pos;

        array[pos] = data;

        queueSize++;

        }

        }

}

        template<class Type>
        Type queue<Type>::dequeue()

        {

        if (frontIndex == -1) {
                Type element;

        cout << "UNDERFLOW" << endl;

                return element;

        }

        else {

        if (frontIndex == endIndex) {

        Type element = array[frontIndex];

        frontIndex = endIndex = -1;

        queueSize = 0;

        return element;

        }

        else {

        Type element = array[frontIndex];

        frontIndex = (frontIndex + 1) % CAPACITY;

        queueSize--;

        return element;

        }

        }

}

int main()

{

        cout << "Cathleen Espinoza - 10/1/20" << endl;

        //create queue to hold type string
        queue<string> myQ;

        cout << myQ.size() << endl; 

        myQ.dequeue();

        myQ.enqueue("Fred");

        myQ.enqueue("Liv");

        myQ.enqueue("Julie");

        myQ.enqueue("Rich");

        myQ.enqueue("William");

        myQ.enqueue("Olo");

        myQ.enqueue("Xi");

        myQ.enqueue("Chu");

        myQ.enqueue("Annie");

        myQ.enqueue("Carlos");

        myQ.enqueue("Tuyet");

        myQ.enqueue("Sue");

        myQ.enqueue("Penny");

        cout << myQ.front() << endl;

        cout << myQ.end() << endl;

        cout << myQ.size() << endl;

        cout << myQ.dequeue() << endl;

        cout << myQ.dequeue() << endl;

        cout << myQ.dequeue() << endl;

        myQ.enqueue("Olive");

        myQ.enqueue("Jim");

        cout << myQ.dequeue() << endl;

        cout << myQ.dequeue() << endl;

        cout << myQ.front() << endl;

        cout << myQ.end() << endl;

        cout << myQ.size() << endl;

        system("pause");

        return 0;

}

Output Of Code:

Cathleen Espinoza - 10/1/20
0
UNDERFLOW
OVERFLOW
0
11
12
Fred
Liv
Julie
Rich
William
5
1
9
sh: 1: pause: not found

Images Of Code:

Image Of Output:


Related Solutions

A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
Write a program to print the number of class assignments you have done so far, the...
Write a program to print the number of class assignments you have done so far, the total grades, and average grade. Prompt a 0 input to end the program. Java Using while statements in the lecture this was provided. Wording confused me, Total grade is sum of all grades.
So I need to parse tweets with the String class and I have no idea what...
So I need to parse tweets with the String class and I have no idea what should I do for this lab and how to start my coding. It will be nice if anyone guide me what should I do exactly and how I start this lab. Use the Scanner class (as discussed in lecture) to read in a tweet entered by the user and store it in a String variable named tweet. You will be splitting up (parsing) the...
Given: You are given a Python Class template. In this class there is a class variable...
Given: You are given a Python Class template. In this class there is a class variable vector, is a list of N non-negative integers and are stored (in positions 0, 1, 2, ... (N-1)), where at least one integer is 0. Task: Write a recursive function "findAllPaths" to find all possible path through V starting at position 0, and ending at the location of 0, in accordance with the Rule below. If no such path exists, "paths" should be an...
1.) A class template can be derived from a non-template class True or False 2.) Inaccessible...
1.) A class template can be derived from a non-template class True or False 2.) Inaccessible pointer is a potential problem on simple linked list True or False 3.) Array based lists are faster in term of acing data True or False 4.) Simple linked lists use less space than double linked lists True or False 5.) For large lists "array based lists" are more efficient for insertion and deleting operational True or False 6.) We can remove data only...
Determining character type without using char methods. (epic confusion) 1. Call your class Program4A, so your...
Determining character type without using char methods. (epic confusion) 1. Call your class Program4A, so your filename will be Program4A.java. It is essential for grading purposes that everyone have the same class name. 5. Ask the user for a single character "Please enter a single character, followed by the enter/return key. 6. Read in the character and assign it to a variable of char. 7. compile and run program. Don't move on until this part is working. I have entered...
I have a char memory[1024]. I want to write the string "mike" into the memory starting...
I have a char memory[1024]. I want to write the string "mike" into the memory starting from an index. I also need a function to move "mike" to another index in the same memory swapping m and k in mike. Basically, i need a function to write to memory and a function to move the string to an index. Thank you In c++ please.
I have this program in C that takes three char arrays that each have a first...
I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers. #include <stdio.h> void upper_string(char []); int main() { char name1[100]="John Smith"; char name2[100]="Mary Cohen"; char name3[100]="Carl Williams"; upper_string(name1);// calling...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT