I asked a similar question here which is:
The language L={anbn} where n ≥ 1, is a language of all words with the following properties:
- The words consist of strings of a’s followed by b’s.
- The number of b’s is equal the number of a’s.
- Examples of words that belong to L are:
ab, where n=1;
aabb, where n=2;
aaabbb, where n=3;
aaaabbbb, where n=4.
One way to test if a word w belong to this language L is to use a stack to check if the number of a’s balances the number of b’s. Use the following header and write a function isInLanguageL2 that uses a stack to test if any word belongs to L. If w belongs to L then the isInLanguageL2 should return true otherwise isInLanguageL2 should return false.
bool isInLanguageL(string w);
Note the following:
- Only words belonging to L should be accepted.
- The word bbaa does not belong to L.
- Do not count the number of a’s or b’s.
A follow up to that question asks to redo the question using queues (I realize a queue probably isn't the best way to do this, however that's not the point of the exercise):
Repeat the previous question using the following header:
bool isInLanguageL(queueType< char> & w);
My solution for doing this without actually counting the a's and b's is below. It uses two queues. I "copy" all the a's from the original queue into a temp queue and remove them from the original queue which should have only b's remaining. I then iterate through the temp queue and pop the original queue for every a that is in the temp queue. After checking through the temp queue and if both queues are empty at the end, then the word is valid.
My question: How can I accomplish this using a single queue as I think using a temporary queue would be a waste of memory.
My code:
bool isInLanguageL(linkedQueueType<char> &w){
linkedQueueType<char> temp;
if(w.front() != 'a') //queue must start with an a
return false;
while(w.front() == 'a'){ //iterate through the a's and add them to temp queue
temp.addQueue(w.front());
w.deleteQueue(); //remove a's from original queue
}
while(!temp.isEmptyQueue() && !w.isEmptyQueue()){ //delete b for each a
if(w.front() != 'b') //only b's should remain in original queue
return false;
else{
w.deleteQueue();
temp.deleteQueue();
}
}
//both stacks must be empty then number of b=a and word is valid
if(w.isEmptyQueue() && temp.isEmptyQueue())
return true;
return false;
}
int main()
{
linkedQueueType<char> wordQueue;
std::string word;
std::cout << "Enter a word belonging to language L. ";
std::cin >> word;
for(int i = 0; i < word.length(); i++){
wordQueue.addQueue(word[i]);
}
if(isInLanguageL(wordQueue))
std::cout << word << " is a valid word.";
else
std::cout << word << " is not a valid word.";
return 0;
}
std::stack
tostd::queue
and you're done. \$\endgroup\$queue
replacesstring
, notstack
. But why would you need to know how manychar
s are in the queue? Justpush
into the stack thea
spop
ped out of the queue; thenpop
b
s from the queue anda
s from the stack and test if they're emptied out at the same time. \$\endgroup\$