Palindrome method help

I'm writing a program to see if a string is a palindrome. I'm having trouble with the implementing the queue.enqueue(nextCharacter), stack.push(nextCharacter), queueFront = queue.peekFront(), stackTop = stack.peek(), queue.dequeu(). I tried looking at examples online of implementations of peek(), deque, push, and enqueue. Could not find any that worked correctly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #include <string>
#include <queue>
#include <stack>
#include <iostream>
#include "Driver.h"

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<std::string> queue;
	std::stack<std::string> 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.enqueue(nextCharacter); // ???
		stack.push(nextCharacter); // ?????

	}

	// Set characters to true
	charactersEqual = true;
	
	// Compare queue characters with stack characters
	while (!queue.empty() && charactersEqual) {
		queueFront = queue.peekFront(); // ?????
		stackTop = stack.peek(); // ?????

		if (queueFront == stackTop) {
			queue.dequeu();B// ?????
			stack.pop();
		}
		else {
			charactersEqual = false;
		}


	}


	return charactersEqual;  // return charcters that are equal

}
Is it a requirement to code it like this - as there's much simpler ways of doing this.

std::queue and std::stack uses push() and pop() to add/remove elements.

See
http://www.cplusplus.com/reference/stack/stack/
http://www.cplusplus.com/reference/queue/queue/

Last edited on
Yes I have to code it this way. I have to follow a certain algorithm.
Last edited on
OK. Try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <string>
#include <queue>
#include <stack>
#include <iostream>
#include <iomanip>

bool isPalindrome(const std::string& str) {
	// Create an empty queue and stack
	std::queue<char> queue;
	std::stack<char> stack;

	// Add each character of string to both queue and stack
	for (const auto& ch : str) {
		queue.push(ch);
		stack.push(ch);
	}

	// Set characters to true
	bool charactersEqual {true};

	// Compare queue characters with stack characters
	while (!queue.empty() && !stack.empty() && charactersEqual) {
		charactersEqual = queue.front() == stack.top();
		queue.pop();
		stack.pop();
	}

	return charactersEqual;  // return characters that are equal
}

int main()
{
	const std::string str1 {"asdzdsa"};
	const std::string str2 {"asddsa"};
	const std::string str3 {"asdzesa"};
	const std::string str4 {"asdesa"};

	std::cout << std::boolalpha << isPalindrome(str1) << '\n';
	std::cout << std::boolalpha << isPalindrome(str2) << '\n';
	std::cout << std::boolalpha << isPalindrome(str3) << '\n';
	std::cout << std::boolalpha << isPalindrome(str4) << '\n';
}



true
true
false
false

Last edited on
I have to follow the exact format I had from the code above that I posted.
Here is the algorithm I'm trying to follow:

isPalindrome(someString: string): boolean
{
// Create an empty queue and stack
aQueue = new empty queue
aStack = new empty stack

// Add each character of string to both queue and stack
length = length of someString

for (i= 1 throught length)
{
nextChar = ith character of someString
aQueue.enqueue(nextChar)
aStack.push(nextChar)

}

charactersAreEqual=true

// Compare queue characters with stack characters
while (aQueue is not empty and charactersAreEqual)
{
queueFront = aQueue.peekFront();
stackTop = aStack.peek()

if(queueFront equals stackTop)
{
aQueue.dequeue();
aStack.pop()
}
else {
charactersAreEqual = false
}
return charcatersAreEqual = false
}
Last edited on
That code is not syntactically correct for C++ using the standard containers. eg. std::queue doesn't have a method '.peekFront()' It's .front(). My code above follows the specified/required logic.
Last edited on
For info, as 1 liner:

1
2
3
bool isPalindrome(const std::string& str) {
	return std::equal(str.begin(), str.begin() + str.size() / 2, str.rbegin());
}

Last edited on
Topic archived. No new replies allowed.