From 27a602d8d2ca2494e8abfa19813f140a569ea93b Mon Sep 17 00:00:00 2001 From: Krishna Pradeep Dawalkar Date: Sun, 18 Oct 2020 22:21:12 +0530 Subject: [PATCH] Added Infix To Postfix Expression Converison using arrays --- STACKS/infixToPostfix_using_stacks.cpp | 160 +++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 STACKS/infixToPostfix_using_stacks.cpp diff --git a/STACKS/infixToPostfix_using_stacks.cpp b/STACKS/infixToPostfix_using_stacks.cpp new file mode 100644 index 0000000..eea4144 --- /dev/null +++ b/STACKS/infixToPostfix_using_stacks.cpp @@ -0,0 +1,160 @@ + +#include +#include +#include +using namespace std; + class Stack{ +typedef struct Node{ + int data; + Node *next; +}Node; + + Node *top; + public: + Stack(){ + top=NULL; + } + int isEmpty(); + + void push(int x); + int pop(); + int Top(); + +}; +int Stack::Top(){ + return top->data; +} +int Stack::isEmpty(){ + if (top==NULL){ + return 1; + }else{ + return 0; +}} +void Stack ::push(int b ){ + Node *newNode; + newNode = new Node; + newNode->data=b; + newNode->next=top; + top=newNode; + +} +int Stack ::pop( ){ + + int y; + Node *temp; + temp=top; + top=top->next; + y=temp->data; + + delete temp; + return y; + +} + +int priority(char ch){ + if(ch == '('|| ch==')'){ + return 0; + } + if(ch=='+'|| ch=='-'){ + return 1; + } + else if(ch=='*'|| ch=='/'){ + return 2; + }else if(ch=='^'){ + return 3; + }else{ + return 4; + } +} +void infixToPostfix(char infix[20] ){ + char x,tok; int i,j=0; + Stack s; + char post[20]; + for(i=0;infix[i]!='\0';i++){ + tok=infix[i]; + + if(isalnum(tok)){ + post[j++]=tok; + } + else{ + if(tok=='('){ + s.push(tok); + } + else{ + if(tok==')'){ + while((x=s.pop())!='('){ + post[j++]=x; + } + } + else{ + while(!s.isEmpty()&& priority(tok)<=priority(s.Top())){ + post[j++]=s.pop(); + } + s.push(tok); + } + + } + } + } + while(!s.isEmpty()){ + post[j++]=s.pop(); + } + post[j]='\0'; + cout<<"\nPostfix form is: "; + cout<=0;i--){ + tok=infix[i]; + + if(isalnum(tok)){ + pre[j++]=tok; + } + else{ + if(tok==')'){ + s.push(tok); + } + else{ + if(tok=='('){ + while((x=s.pop())!=')'){ + pre[j++]=x; + } + } + else{ + while(!s.isEmpty()&& priority(tok)=0;i--){ + cout<>inf; + infixToPostfix(inf); + infixToPrefix(inf); +return 0; +} + +