简体   繁体   English

我尝试使用堆栈和队列解决字符串回文问题,但面临逻辑问题

[英]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.

相关问题 如何解决此逻辑运算符问题? - How do I solve this logical operator problem? C ++-使用堆栈确定C样式字符串是否是回文 - C++ - Using a stack to determine if a C style string is a palindrome Palindrome检测器使用堆栈和队列C ++ - Palindrome detector using stacks and queues C++ 使用2个队列实现堆栈 - Implementing Stack using 2 Queues 使用堆栈和队列的回文程序 - Palindrome program using stack and queue 我没有使用string.h编写了C ++ Palindrome。 它在没有Do-While循环的情况下工作正常。 但是当我尝试进行循环时,它会多次打印结果 - I wrote a C++ Palindrome without using string.h. It works fine without the Do-While loop. But as i tried make a loop it prints the result many times 使用消息队列进行堆栈粉碎 - Stack Smashing Using Message Queues 带堆栈和队列的字符串回文(C ++) - String Palindrome with stack and queue (C++) 我正在尝试解决 timus reverse root (1001) 问题并面临以下问题 - I am trying to solve timus reverse root (1001) problem and facing the following problem 我正在尝试使用回溯解决 N 皇后问题,但在编译时它给出了运行时错误(动态堆栈缓冲区溢出) - I am trying to solve the N queen problem using backtracking but on compilation its giving the runtime error (dynamic-stack-buffer-overflow)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM