How would I alter this code to store ints or doubles.
Here is the output I would like:
Using bigQ to store the integers.
Contents of bigQ: 1234567891011121314151617181920212223242526
Using smallQ to generate errors.
Attempting to store 26
Attempting to store 25
Attempting to store 24
Attempting to store 23
Attempting to store 22 – Queue is full.
#include <iostream>
usingnamespace std;
constint maxQsize = 100;
class Queue {
char q[maxQsize]; // this array holds the queue
int size; // the maximun number of elements that the queue can store
int putloc, getloc; // the put and get indices
public:
// Construct a queue of a specific length.
Queue(int len) {
// Queue must be less than max and positive.
if(len > maxQsize) len = maxQsize;
elseif(len <= 0) len = 1;
size = len;
putloc = getloc = 0;
}
// Put a character into the queue.
void put(char ch) {
if(putloc == size) {
cout << " -- Queue is full.\n";
return;
}
putloc++;
q[putloc] = ch;
}
// Get a character from the queue.
char get() {
if(getloc == putloc) {
cout << " -- Queue is empty.\n";
return 0;
}
getloc++;
return q[getloc];
}
};
// Demonstrate the Queue class.
int main() {
Queue bigQ(100);
Queue smallQ(4);
char ch;
int i;
cout << "Using bigQ to store the alphabet.\n";
// put some numbers into bigQ
for(i=0; i < 26; i++)
bigQ.put('A' + i);
// retrieve and display elements from bigQ
cout << "Contents of bigQ: ";
for(i=0; i < 26; i++) {
ch = bigQ.get();
if(ch != 0) cout << ch;
}
cout << "\n\n";
cout << "Using smallQ to generate errors.\n";
// Now, use smallQ to generate some errors
for(i=0; i < 5; i++) {
cout << "Attempting to store " <<
(char) ('Z' - i);
smallQ.put('Z' - i);
cout << "\n";
}
cout << "\n";
// more errors on smallQ
cout << "Contents of smallQ: ";
for(i=0; i < 5; i++) {
ch = smallQ.get();
if(ch != 0) cout << ch;
}
cout << "\n";
}
#include <iostream>
usingnamespace std;
constint maxQsize = 100;
template < typename T > class Queue {
T q[maxQsize]; // this array holds the queue
int size; // the maximun number of elements that the queue can store
int putloc, getloc; // the put and get indices
public:
// Construct a queue of a specific length.
Queue(int len) {
// Queue must be less than max and positive.
if(len > maxQsize) len = maxQsize;
elseif(len <= 0) len = 1;
size = len;
putloc = getloc = 0;
}
// Put a 'T' into the queue.
void put( const T& v ) {
if(putloc == size) {
cout << " -- Queue is full.\n";
return;
}
putloc++;
q[putloc] = v ;
}
// Get a 'T' from the queue.
T get() {
if(getloc == putloc) {
cout << " -- Queue is empty.\n";
return 0;
}
getloc++;
return q[getloc];
}
bool empty() const { return getloc == putloc ; }
};
// Demonstrate the Queue class.
int main() {
Queue<char> charq (100);
constchar alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
for( char c : alphabet ) charq.put(c) ;
while( !charq.empty() ) cout << charq.get() ;
std::cout << '\n' ;
Queue<int> intQ(4);
for( int i = 0 ; i < 6 ; ++i ) intQ.put(i) ;
while( !intQ.empty() ) cout << intQ.get() << ' ' ;
std::cout << '\n' ;
Queue<double> dblQ(50);
for( int i = 0 ; i < 10 ; ++i ) dblQ.put( i * 1.57 ) ;
while( !dblQ.empty() ) cout << dblQ.get() << ' ' ;
std::cout << '\n' ;
}