hello,
I'm fairly new to programming in general, and i'm having some trouble figuring out how to use a stack and queue to check to see if a word or certain order of numbers/letters form a palindrome or not.
My logic is to read in the original string, push/pop characters into the stack/queue, and then compare the original string with the reversed string, but I am having trouble understanding where to loop I think.
a sample input/output would be:
Infile: mom
outFile: mom is a palindrome
Here is my code thus far:
#include "stack.h"
#include "queue.h"
int main()
{
ifstream inFile;
ofstream outFile;
Queue q;
StackType s;
string oWord;
string revWord = "";
int i = 0;
int z = 0;
inFile.open("in.data");
outFile.open("out.data");
if (inFile.fail() || outFile.fail()){
cout << "File error, check input or output files." << endl;
return 1;
}
inFile >> oWord;
outFile << oWord;
while ((!q.IsFull) && (!s.IsFull)){
q.Enqueue(oWord[i]);
s.Push(oWord[i]);
i++;
}
while ((!q.IsEmpty()) && (!s.IsEmpty())){
s.Pop(oWord[z]);
q.Dequeue(oWord[z]);
revWord = oWord[i];
i--;
}
if (revWord == oWord)
outFile << " is a palindrome" << endl;
else
outFile << " is not a palindrome" << endl;
return 0;
}
I know there's a lot of things wrong, I just am stumped on how to figure out this logic.
Thanks for your time and help.