【问题标题】:String turned into a linked list then checking parentheses balance字符串变成一个链表然后检查括号平衡
【发布时间】:2020-04-18 11:28:54
【问题描述】:

因此,作为一项任务,我必须从用户那里获取一个字符串并将其转换为一个列表,然后检查括号平衡。但是,我不断收到`警告:控制到达非无效函数的结尾 [-Wreturn-type]

#include<iostream>
#include<string>
#include<stacks>
class Node{
public:
char data;
Node *head;
Node *tail;
Node *next;
Node *node;

void addToList(char);
bool CheckList();
void StringToList(string, Node*);
};
Node* addToList(char data)
    {
        Node* newNode = new Node;
        newNode->data = data;
        newNode->next = NULL;
        return newNode;
};

Node* StringToList(string text, Node* head)
{
    head = addToList(text[0]);
    Node *CurrentNode = head;
    for (int i = 1; i <text.size(); i++){
        CurrentNode->next = addToList(text[i]);
        CurrentNode = CurrentNode->next;
    }
    return head;
}
bool CheckList(Node* head)
{
    char c;

    stack <char> p;
    int i = 0;
    Node* CurrentNode = head;
    while(CurrentNode != NULL){

        if('(' || '{' || '['== CurrentNode->data){
            p.push(CurrentNode->data);

                 if(')' == CurrentNode->data){
                c= p.top();
                p.pop();
                if (c == '{' || c == '['){
                    return false;

            }
        }
            else if('}' == CurrentNode->data){
                c= p.top();
                p.pop();
                if (c == '(' || c == '['){
                    return false;

            }
        }
            else if('}' == CurrentNode->data){
                c= p.top();
                p.pop();
                if (c == '(' || c == '['){
                    return false;

                }
        }



            }
    }
    CurrentNode = CurrentNode->next;


}

int main()
{
    string text = "(check)[";
    Node *head = NULL;

    head = StringToList(text, head);
    if(CheckList(head)){
        cout<<"MAMA MIA IT WORKED-A!";
    }
    else
        cout<<"IT'S-A STILL WORKING!!!";

    return 0;

}

任何帮助将不胜感激。再次感谢您的宝贵时间。另外,很抱歉,如果我的代码看起来有点乱,但我对堆栈和列表有点陌生。

【问题讨论】:

  • 警告非常适合指出问题代码需要修复的行号。始终在 gcc/clang 上使用 -Wall -Wextra -pedantic 最低值进行编译,或者在 VS (cl.exe) 中使用 /W3,并且在编译时没有警告之前不要接受代码。还可以考虑添加-Wshadow 来识别和隐藏可能有问题的变量。你可以从你的编译器中学到很多东西(虽然 STL 的警告有点多……)

标签: c++ list stack


【解决方案1】:

CheckList 声称返回 bool,但在 while 循环之后和

CurrentNode = CurrentNode->next;

您没有return 声明。该函数只是结束而不返回值。如果对具有非void 返回类型的函数的调用在没有return 语句(或之前通过异常退出)的情况​​下结束,则程序具有未定义的行为。编译器会警告您这种可能性。

您需要在函数末尾添加一个return 语句,其中包含您要返回的值。鉴于函数中的其他return 语句都返回false,我假设您打算返回true,如果整个循环执行而不返回false

CurrentNode = CurrentNode->next;
return true;

【讨论】:

    【解决方案2】:

    你的功能:

    bool CheckList(Node* head)
    

    末尾没有return

        }
        CurrentNode = CurrentNode->next;
    
    
    }
    

    你必须返回一些东西:

        }
        CurrentNode = CurrentNode->next;
    
        return /* an appropriate boolean value */;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 2018-06-23
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      相关资源
      最近更新 更多