C++ 字符串转换为链表,然后检查括号是否平衡
因此,作为一项任务,我必须从用户那里得到一个字符串,并将其转换为一个列表,然后检查括号是否平衡。然而,我不断得到“警告:控件到达非void函数的末尾[-Wreturn type]C++ 字符串转换为链表,然后检查括号是否平衡,c++,list,stack,C++,List,Stack,因此,作为一项任务,我必须从用户那里得到一个字符串,并将其转换为一个列表,然后检查括号是否平衡。然而,我不断得到“警告:控件到达非void函数的末尾[-Wreturn type] #include<iostream> #include<string> #include<stacks> class Node{ public: char data; Node *head; Node *tail; Node *next; Node *node; void addT
#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;
}
#包括
#包括
#包括
类节点{
公众:
字符数据;
节点*头;
节点*尾部;
节点*下一步;
节点*节点;
void addToList(char);
bool检查表();
void StringToList(字符串,节点*);
};
节点*添加列表(字符数据)
{
Node*newNode=新节点;
新建节点->数据=数据;
newNode->next=NULL;
返回newNode;
};
节点*字符串列表(字符串文本,节点*头)
{
head=addToList(文本[0]);
节点*当前节点=头部;
for(int i=1;i next=addToList(text[i]);
CurrentNode=CurrentNode->next;
}
回流头;
}
bool检查表(节点*头部)
{
字符c;
堆栈p;
int i=0;
节点*当前节点=头部;
while(CurrentNode!=NULL){
如果(“(“||”{“||”[”==CurrentNode->data){
p、 推送(当前节点->数据);
如果(')==CurrentNode->data){
c=p.top();
p、 pop();
如果(c=='{'| c=='['){
返回false;
}
}
else如果('}'==CurrentNode->data){
c=p.top();
p、 pop();
如果(c==”(“| | c==”[”){
返回false;
}
}
else如果('}'==CurrentNode->data){
c=p.top();
p、 pop();
如果(c==”(“| | c==”[”){
返回false;
}
}
}
}
CurrentNode=CurrentNode->next;
}
int main()
{
字符串text=“(检查)[”;
Node*head=NULL;
head=字符串列表(文本,head);
if(检查表(标题)){
cout检查表
声明返回一个bool
,但在while
循环和
CurrentNode = CurrentNode->next;
您没有return
语句。函数结束时没有返回值。如果对非void
返回类型的函数的调用结束时没有return
语句(或之前通过异常退出),则程序具有未定义的行为。编译器将警告您这种可能性
您需要在函数末尾添加一个return
语句,其中包含您想要返回的值。鉴于函数中的其他return
语句都返回false
,如果整个循环执行时没有返回false
,我假设您打算返回true
:
CurrentNode = CurrentNode->next;
return true;
你的职能:
bool CheckList(Node* head)
结尾没有返回值
:
}
CurrentNode = CurrentNode->next;
}
你必须归还一些东西:
}
CurrentNode = CurrentNode->next;
return /* an appropriate boolean value */;
}
警告非常擅长指出需要修复问题代码的行号。请始终在gcc/clang上使用-Wall-Wextra-pedantic
最小值进行编译,或在VS中使用/W3
(cl.exe
)在没有警告的情况下编译代码之前,不要接受代码。还要考虑添加<代码> Wshadow < /代码>来识别和遮蔽可能有问题的变量。你可以从编译器中学到很多东西(虽然STL警告SPEW有点多……)