[英]I tried to solve string palindrome problem using stack and queues, but facing logical issues
Question: A palindrome is a string that reads the same forward and backward, for example, radar, toot, and madam.问题:回文是前后读相同的字符串,例如radar、toot 和madam。 The challenge then is to write an algorithm that will read a sentence word-by-word from left to right and decide whether it is a palindrome.接下来的挑战是编写一个算法,从左到右逐字读取一个句子,并确定它是否是回文。 Palindrome strings as an example.以回文串为例。
- “Madam, I'm Adam." “夫人,我是亚当。”
- “Eve." “前夕。”
The algorithm to solve this problem is relatively straightforward,解决这个问题的算法比较简单,
- Use a stack and a queue.使用堆栈和队列。
- Push and Enqueue tokens (words) into both data structures.将令牌(单词)推送和入队到两个数据结构中。
- Pop and Dequeue the tokens (words) and compare.将标记(单词)弹出并出列并进行比较。
- Perform remove operation until one of the data structure is empty.执行删除操作,直到其中一个数据结构为空。 If the case is that both are empty, the sentence is a palindrome.如果情况是两者都是空的,则该句子是回文。
Your task is to figure out whether the string entered by the user is a palindrome or not.您的任务是确定用户输入的字符串是否为回文。
#include<iostream>
#include<string>
#include<stack>
#include<queue>
using namespace std;
#define MAX 1000
class Stack
{
int top;
public:
string myStack[MAX];
Stack() { top = -1; }
bool push(string item)
{
if (top >= (MAX - 1)) {
cout << "Stack is full";
return false;
}
else {
myStack[++top] = item;
cout << item << endl;
return true;
}
}
string pop()
{
if (top < 0) {
cout << "Stack Underflow!!";
return 0;
}
else {
string item = myStack[top--];
return item;
}
}
bool isEmpty()
{
if (top == -1)
return true;
else
return false;
}
};
class Queue
{ public:
string myqueue[MAX];
int front, rear;
Queue()
{
front = -1;
rear = -1;
}
bool isFull() {
if (front == 0 && rear == MAX - 1) {
return true;
}
return false;
}
bool isEmpty() {
if (front == -1)
return true;
else
return false;
}
void enQueue(string item) {
if (isFull()) {
cout << endl << "Queue is full!!";
}
else {
if (front == -1) front = 0;
rear++;
myqueue[rear] = item;
cout << item << " "<<endl;
}
}
string deQueue() {
string value;
if (isEmpty()) {
cout << "Queue is empty!!" << endl;
return false;
}
else {
value = myqueue[front];
if (front >= rear)
{
front = -1;
rear = -1;
}
else {
front++;
}
return(value);
}
}
};
int main()
{
bool palindrome = true;
Stack stack;
stack.push("m");
stack.push("a");
stack.push("d");
stack.push("a");
stack.push("m");
Queue queue;
queue.enQueue("m");
queue.enQueue("a");
queue.enQueue("d");
queue.enQueue("a");
queue.enQueue("m");
while (sizeof stack == 0 || sizeof queue == 0 )
{
stack.pop();
queue.deQueue();
};
if (sizeof stack == sizeof queue )
{
cout << "The given word is palindrome." << endl;
}
else
{
cout << "The given word is not a palindrome." << endl;
}
system("pause");
return 0;
}
Ah the magic of sizeof
.啊sizeof
的魔力。
sizeof
has a very specific meaning (which you can look up). sizeof
有一个非常具体的含义(你可以查一下)。 It does not magically get you the size of anything you like, defined in whatever way you prefer.它不会神奇地让您获得您喜欢的任何大小,以您喜欢的任何方式定义。
This code这段代码
while (sizeof stack == 0 || sizeof queue == 0 )
should be应该
while (stack.isEmpty() || queue.isEmpty())
You wrote your isEmpty
methods, you should use them.你写了你的isEmpty
方法,你应该使用它们。
But even that code is wrong (because of logical issues).但即使该代码也是错误的(由于逻辑问题)。 I guessing (but not really sure) that you meant我猜(但不太确定)你的意思是
while (!stack.isEmpty() && !queue.isEmpty())
In short sizeof
has no place in this code.总之sizeof
在这段代码中没有位置。 If you need to know the size of your stacks or queues you should write a method (called size
maybe) that returns the size.如果您需要知道堆栈或队列的大小,您应该编写一个返回大小的方法(可能称为size
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.