Prefix to Postfix Stack Emplementation

It’s been long time. sorry to them those who want some explanation from me. was bit busy can’t give enough time for blogging. but now i have sorted out that “i will always be busy”. so if i want to continue blogging then i have to write something on the fly.

For them who have got some help “Prefix to Postfix using Binary Teer”. Here same code is implemented using stack. But keep in mind that if case of implementation using stack binary tree is not used at all.

Here is a pdf file discribing the algorithm. Prefix to Postfix using Stack Algorithm

Here is the code written in C++

#include
#include
using namespace std;

#define MAX 100
#define LEFT_DONE ‘#’

bool isOperator(char ch){
return ch==’+’ || ch==’-‘ || ch == ‘*’ ||
ch == ‘/’ || ch == ‘$’ || ch == ‘%’;
}

int main(){

//e input expression and p output expression
char e[MAX],p[MAX];
stack stk;

cout << "Enter a prefix : "; cin >> e;

int j=0;
//until the end of expression riches
for (int i=0;e[i];i++){

if(isOperator(e[i])){
stk.push(e[i]);
}
else {
p[j++]=e[i];

while(!stk.empty() && stk.top()==LEFT_DONE){
stk.pop();
p[j++]=stk.top();
stk.pop();
}
stk.push(LEFT_DONE);
}
}
p[j]=0;
cout << "Postfix expression is :" << p << endl; return 0; }[/sourcecode]

Advertisements

16 thoughts on “Prefix to Postfix Stack Emplementation

  1. hello tank you for your help to slove my problam i life in iran of asia an tank ypu very much because your blog slove my problam

    tank to god and with you for every time and every plice

  2. can some one get the postfix for the following infix -+abc through this alg….

    i got it wrongly So as you guys…..try it out…

  3. @shashank: could you give an example, where this “do not workkkkkkkkkkkk”?

    @dhina: i tried with -+abc and it produces ab+c- could you please explain who it is wrong?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s