我尝试创建前缀前缀并出现错误

时间:2019-07-25 06:48:20

标签: c++

我尝试使用Node创建前缀前缀。我收到一个错误,我认为这是关于将char转换为char还是我的编码错误?我非常困惑,请帮助我解决问题。首先尝试创建Node,然后尝试创建方法,然后我认为模板错误? 错误:无法将'stack_Node :: top'从'char(stack_Node ::)()'类型转换为'char'| 给出以下代码:

#include <iostream>
#include <string>
template <typename T>
struct stack_Node
{
    struct Node
    {
        T data;
        Node *next;
        Node():next(nullptr){}
        Node(const T &data):data(data),next(nullptr){}
    };
    Node* head=nullptr;
    Node* tail=nullptr;
    void push (const T &data)
    {
        Node *newNode=new Node(data);
        if (head == nullptr)
        {
            tail=head=newNode;
            return;
        }
        else
        {
            tail=tail->next=newNode;
            return;
        }
    }
    T top()
    {
        return tail->data;
    }
    bool isEmpty()
    {
        if (head != nullptr) return 1;
        else return 0;
    }
    void pop()
    {
        auto pre=head;
        for (auto cur=head;cur != nullptr;cur=cur->next)
        {
            if (cur == tail )
            {
                tail=pre;
                tail->next=nullptr;
                delete cur;
                cur=cur->next=nullptr;
                return;
            }
            pre=cur;
        }
    }
};
int priority(char c)
{
    switch(c)
    {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        case '^':
            return 3;
        default:
            return -1;

    }
}
bool isOperator(char c)
{
    switch(c)
    {
        case '+':
        case '-':
        case '*':
        case '/':
        case '^':
            return true;
        default:
            return false;

    }
}
std::string infixToPostfix (std::string infix)
{
    stack_Node<char> stack;
    std::string postfix="";
    for (size_t i =0;i< infix.length();i++)
    {
        //check space or ','
        if (infix[i] == ' ' || infix[i] == ',') continue;
        //if char is operator
        else if (isOperator(infix[i]))
        {
            while ( !stack.isEmpty() && stack.top() != '(' && priority(stack.top) > priority(infix[i]) ) //error from here 
            {
                postfix += stack.top();
                stack.pop();
            }
            stack.push(infix[i]);
        }
        //if char == operand
        else if (!isOperator(infix[i]))
        {
            postfix += stack.top();
            stack.pop();
        }
        else if (infix[i] == '(')
        {
            stack.push(infix[i]);
        }
        else if (infix[i] == ')')
        {
            while(stack.isEmpty() && stack.top() != '(')
            {
                postfix += stack.top();
                stack.pop();
            }
            stack.pop();
        }
    }
        while (!stack.isEmpty())
        {
            postfix += stack.top();
            stack.pop();
        }
        return postfix;

}

int main()
{
    return 0;
}

0 个答案:

没有答案