使用带有链表的堆栈数据结构将中缀转换为后缀

Converting infix to postfix using stack data structure with linked list

本文关键字:中缀 转换 后缀 数据结构 堆栈 链表      更新时间:2023-10-16

我正在尝试创建一个程序,使用结构 Node 在 c++ 中使用堆栈数据结构将中缀表达式转换为后缀表达式。相关函数用于从堆栈中推送、弹出值。程序可以编译,但是在输出窗口中输入中缀表达式示例"a+b"后,出现运行时错误。 这是完整的代码。

#include <iostream>
#include<cstring>
using namespace std;
struct Node{
char data;
struct Node * next;
}*top=NULL;
void Push(char x)
{   struct Node* p=new Node;
if(p==NULL)
cout<<"nStack Overflow";
else{
p->data=x;
p->next=top;
top=p;
}
}  
char Pop(){
int x=-1;
struct Node * p;
if(top==NULL)
cout<<"nStack is Empty";
else{
p=top;
x=p->data;
top = top->next;
delete p;
}
return x;
}
int isEmpty(){
return top?0:1;
}
int Pre(char ch){
if(top==NULL)
return 0;
else if(ch == '-' || ch == '+')
return 1;
else if(ch == '*' || ch == '/')
return 2;
else
return 3;
}
char * InToPost(char *infix){
char *postfix = new char[strlen(infix)+1];
int i=0,j=0;
while(infix[i]!='')
{
if(Pre(infix[i]>Pre(top->data)))
Push(infix[i++]);
else{
while(Pre(infix[i])<=Pre(top->data))
postfix[j++]= Pop();
}
}
while(!isEmpty())
postfix[j++]=Pop();
postfix[j] = '';
return postfix;
}
int main(){
char *infix = new char[30];
cin>>infix;
cout<<InToPost(infix);
}

错误的原因可能是您没有在 while 循环的 else 部分中增加"i"的值。这就是导致无限循环的原因。此外,您可能错误地将括号错误地放置在 while 循环的 if 条件中。 但是,即使您进行了这些更改,它也不会为您提供正确的结果。我认为你正在尝试一种错误的方法。 每个字母表都应该直接推入"后缀"数组中,而不是在堆栈中。我们必须只检查运算符的优先级,而不是字母表的优先级。 您可以尝试实现以下代码。我没有使用堆栈的链接实现,而是直接使用了 STL 中的堆栈,但您可以根据需要进行所需的更改。在这里,我将一个额外的字符"A"推入堆栈,以检查堆栈中是否没有运算符。

int prec(char ch){
if(ch == '^')
return 3;
if(ch == '*' or ch == '/')
return 2;
if(ch == '+' or ch == '-')
return 1;
return -1;
}
string infixToPostfix(string x)
{
stack<char> s;
s.push('A');
string ans;
int n = x.length();
for(int i = 0 ; i < n; i++){
if((x[i] >= 'a' and x[i] <= 'z') or (x[i] >= 'A' and x[i] <= 'Z'))
ans += x[i];
else if(x[i] == '(')
s.push(x[i]);
else if(x[i] == ')'){
while(s.top() != 'A' and s.top() != '('){
ans += s.top();
s.pop();
}
if(s.top() == '('){
s.pop();
}
}
else{
while(s.top() != 'A' and prec(x[i]) <= prec(s.top())){
ans += s.top();
s.pop();
}
s.push(x[i]);
}
}
while(s.top() != 'A'){
ans += s.top();
s.pop();
}
return ans;
}

试试这个。它应该有效! 它也适用于包含偏执的表达式,如 a+b*(c+d(+(f+g(

我已经修改了你的代码。

1( 代替 int x=-1;在 -> 字符 Pop(( 中声明 char x=-1

2(按照以下代码更改您的pre函数

int Pre(char ch({

if(ch == '-' || ch == '+') return 1;
if(ch == '*' || ch == '/') return 2;
else    return 0;

}

3( 在 InToPost(( 中更改 while 循环

while(infix[i]!='\0'(

{
if(Pre(infix[i])==0)   // if char is operand then push into postfix array
postfix[j++] = infix[i++];   // you were not using j++ with postfix array
else{
if(isEmpty()==1)  // if stack is empty then push into Stack
Push(infix[i++]);
else
{
if(Pre(infix[i])>=Pre(top->data))  // If coming operator has same or less predence then push into Stack
Push(infix[i++]);
else{
while(Pre(infix[i])<=Pre(top->data))
postfix[j++]= Pop();
}
}
}
}