Palindrome Stack and Queue

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.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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

}
[Also here http://www.cplusplus.com/forum/beginner/278612/ ]

Well the code will work - but see my previous answers in the above thread.
Last edited on
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.)
Last edited on
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
Last edited on
In some examples of the queue I have found they use Empty(), enqueuer(), dequeuer(), and peekFront()

Time to write C++, not Java. None of those are standard library functions. Goodness knows where you got your "examples" from.

You missed the gist of both comments in my post.
Last edited on
Re lastchance's second point. The while test in L77 could be:

 
while (queue.size() > length / 2 && charactersEqual) {


to eliminate unnecessary comparisons. But as per my other post, the code can be 'simplified':

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 (const char 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 (const char 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())
			return false;

	return true;  // return characters that are equal
}


which slightly changes the algorithm.
Last edited on
Topic archived. No new replies allowed.