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.
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public int compareTo(MyString other) { /* code from HW1 here */ } public char charAt(int i) { return data[i]; } public int length() { return data.length; } public int indexOf(char c) { /* code from HW1 here */ } public boolean equals(MyString other) { /* code from HW1 here */ } /* * Change this MyString by removing all occurrences of * the argument character...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
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.
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...
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...
C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU...
C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU MUST USE POINTERS. 3. YOU MUST USE THE SWITCH STATEMENT TO EXECUTE THE PROGRAM. 4. ALL MODIFICATIONS MUST BE DONE IN THE SAME ORIGINAL CHAR ARRAY WITHOUT CREATING A NEW ONE. Write a C++ program that modifies a null teminated char array as follows: Consonants are positioned at the beginning of the string and vowels are moved to the end of the string. Example...
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...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;        int number = 0;        Scanner in = new Scanner(System.in);        System.out.print("Enter a letter: ");        letter = in.next().charAt(0);        if(letter == 'A' || letter == 'B' || letter == 'C') number = 2;        if(letter == 'D' || letter == 'E' || letter == 'F') number = 3;        if(letter == 'G' || letter ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT