使用堆栈n队列方法进行回文检查时遇到很多错误 [英] I got many errors on doing palindrome checking using stack n queue methods

查看:71
本文介绍了使用堆栈n队列方法进行回文检查时遇到很多错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <string>
#include <stack>
#include <queue>

using namespace std;

class Stack
{
    Node*top;
    int count;

public:
    Stack();
    void push(char);
    char topGet();
    char pop();
    void display();
    bool isEmpty();
    int size();
};

class Queue
{
    Node *tail;
    Node *head;
    int count;
    char data;

public:
    Queue();
    void Dequeue();
    void Enqueue(char);
    bool isEmpty();
    bool isFull();
    void display();
    int size();
    char topGet();
};

class Node
{
    Node *next;
    char data;

public:
    Node();
    void setNext(Node *);
    void setData(char);
    Node* getNext();
    char getData();
};

bool isPallindrome(string);
int main()
{
    string input;
    bool flag = true;
    while (flag) {
        cout << "Enter word to see PALINDROME OR NOT, Press x for Quit" << endl;
        cout << "> ";
        getline(cin, input);
        if (input == "x" || input=="X")
        {
            flag = false;
            return 0;

        }

        if (isPallindrome(input))
        {
            cout << input << " is aPallindrome" << endl;
        }
        else
        {

            cout << input << " is not aPallindrome" << endl;
        }


    }
}

bool isPallindrome(string str)
{
    Stack *s;
    Queue *q;

    while (true) {
        s = new Stack();
        q = new Queue();
        for (int i = 0; i < str.length(); i++) {
            s->push(str[i]);
            q->Enqueue(str[i]);
        }
        bool eq = true;
        if (s->size() == q->size()) {
            while (!s->isEmpty()) {
                if (s->topGet() == q->topGet())
                {
                    eq = true;
                }
                else
                {
                    eq=false;
                }
                s->pop();
                q->Dequeue();

            }
        }
        else {
            eq = false;
        }
        return eq;
    }
}

Stack::Stack()
{
    top = NULL;
    count = 0;
}
void Stack::push(char s)
{
    Node*newNode = new Node();
    newNode->setData(s);
    newNode->setNext(top);
    top = newNode;
    count++;
}

bool Stack::isEmpty()
{
    if (count == 0)
        return true;
    return false;
}
char Stack::topGet()
{
    if (count == 0)
        throw "Stack is Empty ";
    char tempS;
    tempS = top->getData();
    return tempS;
}
char Stack::pop()
{

    if (isEmpty())
        return NULL;
    Node *temp;
    char tempS;
    tempS = top->getData();

    temp = top;
    top = top->getNext();
    delete temp;
    count--;
    return tempS;
}
void Stack::display()
{
    cout << topGet() << " ";
}
int Stack::size()
{
    return count;
}

Queue::Queue()
{
    head = new Node();
    tail = new Node;
    data = ' ';
    count = 0;
}

void Queue::Dequeue()
{
    Node *temp;
    if (isEmpty())
    {
        throw "There is Nothing in front to Queue ";
    }
    temp = head;
    data = temp->getData();
    head = head->getNext();
    temp = NULL;
    delete temp;
    count--;

}

void Queue::Enqueue(char val)
{
    Node *newNode = new Node();
    data = val;
    newNode->setData(val);
    newNode->setNext(NULL);
    if (head->getNext() == NULL)
    {
        head->setNext(newNode);
        head->setData(val);
        tail->setNext(newNode);
        tail = head;
    }
    else
    {
        tail->setData(val);
        tail->setNext(newNode);
        tail = newNode;
    }
    count++;
}

bool Queue::isEmpty()
{
    if (count == 0)
    {
        return true;
    }
    return false;

}
void Queue::display()
{
    cout << data << " ";
}
bool Queue::isFull()
{
    if (count == 10)
    {
        return true;
    }
    return false;
}
int Queue::size()
{
    return this->count;
}
char Queue::topGet()
{
    if (count == 0)
        throw "Stack is Empty ";
    return head->getData();
}

Node::Node()
{
    next = NULL;
    data = NULL;

}
void Node::setNext(Node*next)
{
    this->next = next;
}
void Node::setData(char data)
{
    this->data = data;
}
Node*Node::getNext()
{
    return this->next;
}
char Node::getData()
{
    return  this->data;
}





我尝试过:



我不知道我在哪里做错了..太久了我还没碰到c ++



What I have tried:

i'm not sure where did i do wrong..its been for soo long i haven't touch c++

推荐答案

[更新]首先,你忘了在编译时告诉错误,所以我的建议与问题不符。 [/ Update]



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候看,你接近一个bug。



---------------------

使用堆栈和队列检查回文是否过于复杂。

您应该考虑这个问题:

一切都应该尽可能简单,但是并不简单。阿尔伯特爱因斯坦



尝试用最简单的方法手工检查回文,看看你是怎么做的。你需要一个堆栈还是一个队列?我想不是。
[Update] In first, you forgot to tell that the errors where at compile time, so my advice don't match the problem. [/Update]

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

---------------------
Using stack and queue to check if palindrome is overly complicating things.
You should think about this:
"Everything should be made as simple as possible, but no simpler." Albert Einstein

Try to check for palindrome by hand the simplest way, and see how you do. Do you need a stack or a queue ? I think not.


这篇关于使用堆栈n队列方法进行回文检查时遇到很多错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆