作为一项任务,我需要从用户那里获取一个字符串,然后将其转换为列表,接着检查括号平衡性。但是,我一直得到警告:`warning: control reaches end of non-void function [-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;
}
任何帮助都将不胜感激。再次感谢您的时间。另外,如果我的代码看起来有点凌乱,请见谅,因为我对堆栈和列表还比较陌生。
-Wall -Wextra -pedantic
进行编译,或者在VS(cl.exe
)上使用/W3
,并且在没有警告的情况下不接受代码。还可以考虑添加-Wshadow
以识别可能有问题的变量和被遮蔽的变量。您可以从编译器中学到很多东西(尽管STL警告信息有点多...)。 - David C. Rankin