In C++ write a main program that uses the list based queue class (defined below) to check if an input string is of the form anbnan meaning ‘n’ times the character ‘a’ followed by ‘n’ times the character ‘b’ followed by ‘n’ times the character ‘a’. For example, consider: ‘aaaabbbbbbaaaa’ vs. ‘aaabbbaa’ or ‘abaabbab’
You should modularize program using: AnBnAn.cpp, LQ.cpp and LQ.h.
Write a class LLQ that implements a Queue in linked a list of characters. The class should include the following “utilities”:
1.A constructor and a destructor.
2. At least one inline getter and one inline setter for private members
3. An insert function that gets a character value and inserts it at the “end” of the queue
4. A remove function that gets the value of the “first” queue element and stores it in a character variable
5. A Boolean function ‘is_full’ that returns trueif the queue is “full” (otherwise it returns false). This function should be called before inserting items to the queue.
6. A Boolean function ‘is_empty’ that returns true if the queue is “empty” (otherwise it returns false). This function should be called before removing items from the queue
Expert Answer
//LQ.h
#ifndef LQ_H
#define LQ_H
class LQ {
public:
void insert( char letter ); //setter
char removeChar( int index ); //getter
bool isFull(void);
bool isEmpty(void);
LQ(int len); // This is the constructor declaration
~LQ(); // This is the destructor: declaration
private:
int currIndex = 0;
int capacity = 0;
char queue[20];
};
—————————————————————————————————————————————————
//LQ.cpp
#include <iostream>
#include “LQ.h”
using namespace std;
LQ::LQ(int len) {
capacity = len;
}
LQ::~LQ() {
}
void LQ::insert( char letter ) {
if(!isFull()) {
queue[currIndex] = letter;
currIndex++;
cout<<“Inserted”<<endl;
}
else{
cout<<“Queue full”<<endl;
}
}
char LQ::removeChar(int index ) {
return queue[index];
}
bool LQ::isFull( ) {
if(capacity == currIndex) {
return true;
}
return false;
}
bool LQ::isEmpty( ) {
if(capacity == 0) {
return true;
}
return false;
}
————————————————————————————————————————————————–
AnBnAn.cpp
#includes <iostream>
using namespace std;
// Main function for the program
int main( ) {
LQ obj(6);
cout<<“Inserting characters: “<<endl;
obj.insert(‘a’);
obj.insert(‘a’);
obj.insert(‘b’);
obj.insert(‘b’);
obj.insert(‘a’);
obj.insert(‘a’);
int totalAs = 0;
int totalBs = 0;
//loop iteration
for(int i=0;i<6;i++) {
char _letter = obj.removeChar(i);
if(_letter == ‘a’) {
totalAs++;
}
else if(_letter == ‘b’) {
totalBs++;
}
}
cout<<“A’s : “<<totalAs<<“Total b’s: “<<totalBs<<endl;
if(2*totalBs == totalAs) {
cout<<“Given string is recognizable..”<<endl;
}
else{
cout<<“Given string is not recognizable..”<<endl;
}
return 0;
}