【问题标题】:I tried to solve string palindrome problem using stack and queues, but facing logical issues我尝试使用堆栈和队列解决字符串回文问题,但面临逻辑问题
【发布时间】:2020-07-23 23:40:10
【问题描述】:

问题:回文是向前和向后读取相同的字符串,例如,radar、toot 和 madam。接下来的挑战是编写一个算法,从左到右逐字读取一个句子,并确定它是否是回文。 以回文串为例。

  • “女士,我是亚当。”
  • “夏娃”

解决这个问题的算法比较简单,

  • 使用堆栈和队列。
  • 将令牌(单词)推送和入队到两种数据结构中。
  • 弹出和出列标记(单词)并进行比较。
  • 执行删除操作,直到其中一个数据结构为空。如果情况是两者都是空的,则该句子是回文。

你的任务是判断用户输入的字符串是否是回文。

#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;
}

【问题讨论】:

  • 这个完全相同的分配是另一个用户asked about yesterday。必须是同学。
  • 请解释具体是什么不起作用以及您如何尝试解决这个问题。
  • 我不明白如何在比较堆栈和队列中的项目时同时弹出和双端队列。

标签: c++ data-structures stack queue


【解决方案1】:

sizeof的魔力。

sizeof 有一个非常具体的含义(你可以查一下)。它不会神奇地为您提供任何您喜欢的大小,以您喜欢的任何方式定义。

这段代码

while (sizeof stack == 0 || sizeof queue == 0 )

应该是

while (stack.isEmpty() || queue.isEmpty())

你写了你的 isEmpty 方法,你应该使用它们。

但即使是那个代码也是错误的(因为逻辑问题)。我猜(但不太确定)你的意思是

while (!stack.isEmpty() && !queue.isEmpty())

简而言之,sizeof 在此代码中没有位置。如果您需要知道堆栈或队列的大小,您应该编写一个返回大小的方法(可能称为size)。

【讨论】:

  • 早安,@John 先生。我相信你指出的一切都是正确的。但我尝试使用 isempty 函数,但它们无法正常工作。如果你能解决这个逻辑问题,那对我有很大帮助,我已经尝试了所有我能用我的知识和互联网浏览的方法,但我无法解决这个问题。
  • @alizeykhan 看看你的指示。将标记(单词)弹出并出列并比较。您的代码中没有任何内容可以比较从堆栈和队列中删除的项目。如果删除的两个项目不相等,则它不是回文。我认为问题在于您不了解算法。你了解算法吗?在您了解算法之前,您无法编写代码。话虽如此,虽然它很简单,但我认为算法描述得不是很好
猜你喜欢
  • 2013-03-07
  • 2013-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多