The idea of this assignment is to use both a stack and a queue to implement a palindrome-recognition. I have finished this code. However, I wanted to get feedback to make sure I did this assignment correctly. Please let me know of any concerns or idea that will help me with this assignment. Also, did I implement the stack and queue correctly for this assignment. The code includes a driver.cpp, driver.h, and palindrome.cpp.
// driver.cpp
#include <string>
#include <iostream>
#include "Driver.h"
int main() {
// Declare variables
std::string text = " ";
// Have user enter text for palindrome
std::cout << "Enter text: ";
std::getline(std::cin, text);
std::cout << std::endl;
// Call on isPalindrome method to see if the text entered is one
if (!isPalindrome(text)) {
std::cout << "The text you entered is not a palindrome."; // not a palindrome
}
else {
std::cout << "The text you entered is a palindrome."; // is a palindrome
}
}
// driver.h
#include <string>
#include <iostream>
// Palindrome method tells whether the string is a palindrome
bool isPalindrome(std::string str);
// palindrome.cpp
#include <string>
#include <queue>
#include <stack>
#include <iostream>
#include "Driver.h"
// ----------------------------------------------------------------------
// isPalindrome: Whether the given string is a palindrome
// string str: is the user entered text
// returns what whenever the characters for the string are true or false
// ----------------------------------------------------------------------
bool isPalindrome(std::string str) {
// Declare variables
int length;
char nextCharacter;
bool charactersEqual;
char queueFront;
char stackTop;
// Create an empty queue and stack
std::queue<char> queue;
std::stack<char> stack;
// Set lenth to length of string
length = str.length();
// Add each character of string to both queue and stack
for (int i = 0; i < length; i++) {
nextCharacter = str.at(i);
queue.push(nextCharacter);
stack.push(nextCharacter);
}
// Set characters to true
charactersEqual = true;
// Compare queue characters with stack characters
while (!queue.empty() && charactersEqual) {
queueFront = queue.front();
stackTop = stack.top();
if (queueFront == stackTop) {
queue.pop();
stack.pop();
}
else {
charactersEqual = false;
}
}
return charactersEqual; // return characters that are equal
}
I guess it depends whether you think "Abba" is palindromic or not.
You are also comparing twice as many characters as you need to in order to determine palindromicity: you only need to consider the first N/2, not empty the containers. (The same is true of comparing a string with the whole of its reverse.)
Just trying to see if I used the queue and stack properly. In some examples of the queue I have found they use Empty(), enqueuer(), dequeuer(), and peekFront(). This is first time writing code that use both the stack and queue to implement something
bool isPalindrome(const std::string& str) {
std::queue<char> queue;
std::stack<char> stack;
// Add each character of string to both queue and stack
for (constchar c : str) {
queue.push(c);
stack.push(c);
}
// Set characters to true
bool charactersEqual {true};
// Compare queue characters with stack characters
while (queue.size() > str.size() / 2 && charactersEqual)
if (queue.front() == stack.top()) {
queue.pop();
stack.pop();
} else
charactersEqual = false;
return charactersEqual; // return characters that are equal
}
or even:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
bool isPalindrome(const std::string& str) {
std::queue<char> queue;
std::stack<char> stack;
// Add each character of string to both queue and stack
for (constchar c : str) {
queue.push(c);
stack.push(c);
}
// Compare queue characters with stack characters
for (; queue.size() > str.size() / 2; queue.pop(), stack.pop())
if (queue.front() != stack.top())
returnfalse;
returntrue; // return characters that are equal
}