📜  使用单链表将堆栈实现为抽象数据类型,并使用此 ADT 将中缀表达式转换为后缀、前缀以及后缀和前缀表达式的求值. - Javascript代码示例

📅  最后修改于: 2022-03-11 15:03:01.408000             🧑  作者: Mango

代码示例1
#include
#include
#include
#define max 30
using namespace std;
struct node
   {
     char data;
     struct node *next;
   };
class stack
{
    node *top;
    char x;
  public:
      stack()
      {
         top= NULL;
      }
      int empty()
      {
         if(top==NULL)
         {
            return(1);
         }
         else
         {
            return(0);
         }
     }
     void push(char x)
     {
        node *p;
        p=new node;
        p->data=x;
        p->next=top;
        top=p;
     }
    char pop()
    {
      if(!empty())
      {
          node *p;
          p=new node;
          p=top;
          top=top->next;
          x=p->data;
          delete p;
          return(x);
     }
      else
      {
           cout<<"stack is empty"<next;
          x=p->data;
          //delete p;
          return(x);
     }
      else
      {
           cout<<"stack is empty"<>op;

switch(op)
{
case 1:
   cout<<"\nEnter the infix expression::"<>infix;
   infix_to_prefix(infix,prefix);
   cout<<"\nPrefix expression is "<>infix;
   infix_to_postfix(infix,postfix);
   cout<<"\nPostfix expression is "<=0;i--,j++)
   in1[j]=infix[i];
   in1[j]='\0';

  cout<<"\n\n After step 2...";
 for(i=0;i<=strlen(in1)-1;i++)
 {
  cout<=0;i--)
   {
     x=prefix[i];
     if(isalpha(x))
      {
         cout<<"\nEnter the value of "<>val;
         s.push(val);
      }
    else
     {
       op1=s.pop();
       op2=s.pop();
       val=evaluate(x,op1,op2);
       s.push(val);
     }
   }
val=s.pop();
cout<<"\nValue of expression is "<>val;
         s.push(val);
      }
    else
     {
       op2=s.pop();
       op1=s.pop();
       val=evaluate(x,op1,op2);
       s.push(val);
     }
   }
val=s.pop();
cout<<"\nValue of expression is "<