Skip to main content

Posts

Showing posts with the label stack applications

Prefix to Infix Conversion

With a given Prefix Expression, we will see how to convert Prefix Expression into Infix Expression using stack.   Algorithm to convert Prefix Expression to Infix Expression: In this algorithm, we will use stack to store operands during the conversion. The step are as follows: Read the prefix string While the end of prefix string scanned from right to left symb = the current character If symb is an operator poped_sym1 = pop the stack poped_sym2 = pop the stack concat the string  STR = ( poped_sym1 )+ ( operator )+( poped_sym2 ) push the string STR into stack Else push the operand symb into stack End If End While infix_str = pop the stack   Function to convert Prefix Expression to Infix Expression: void prefix_to_infix(char prefix[], char infix[]){ char op[2]; //operator string char poped1[MAX]; char poped2[MAX]; char temp[MAX]; int i = strlen(prefix); op[1] = '\0'; while(--i != -1){ if(prefix[i] == ' '){ continue; } if(isoper...

Postfix to Infix Conversion

With a given Postfix Expression, we will see how to convert Postfix Expression into Infix Expression using stack.   Algorithm to convert Postfix expression to Infix expression: In this algorithm, we will use stack to store operands during the conversion. The step are as follows: Read the postfix string While the end of postfix string symb = the current character If symb is an operator poped_sym1 = pop the stack poped_sym2 = pop the stack concat the string  STR = ( poped_sym2 )+( operator )+( poped_sym1 ) push the string STR into stack Else push the operand symb into stack End If End While infix_str = pop the stack   Function to convert Postfix expression to Infix expression: void postfix_to_infix(char postfix[], char infix[]){ char op[2]; //operator string char poped1[MAX]; char poped2[MAX]; char temp[MAX]; int i = -1; op[1] = '\0'; while(postfix[++i]){ if(postfix[i] == ' '){ continue; } if(isoperator(postfix[i])){ pop(po...

Prefix to Postfix Conversion

With a given Prefix Expression, we will see how to convert Prefix Expression into Postfix Expression using stack.   Algorithm to convert Prefix Expression to Postfix Expression: In this algorithm, we will use stack to store operands during the conversion. The step are as follows: Read the prefix string While the end of prefix string scanned from right to left symb = the current character If symb is an operator poped_sym1 = pop the stack poped_sym2 = pop the stack concat the string  STR = ( poped_sym1 )+( poped_sym2 )+ ( operator ) push the string STR into stack Else push the operand symb into stack End If End While postfix_str = pop the stack   Function to convert Prefix Expression to Postfix Expression: void prefix_to_postfix(char prefix[], char postfix[]){ char op[2]; //operator string char poped1[MAX]; char poped2[MAX]; char temp[MAX]; int i = strlen(prefix); op[1] = '\0'; while(--i != -1){ if(prefix[i] == ' '){ continue; }...

Postfix to Prefix conversion

With a given Postfix Expression, we will see how to convert Postfix Expression into Prefix Expression using stack.   Algorithm to convert Postfix expression to Prefix expression: In this algorithm, we will use stack to store operands during the conversion. The step are as follows: Read the postfix string While the end of postfix string symb = the current character If symb is an operator poped_sym1 = pop the stack poped_sym2 = pop the stack concat the string  STR = ( operator )+( poped_sym2 )+( poped_sym1 ) push the string STR into stack Else push the operand symb into stack End If End While prefix_str = pop the stack   Function to convert Postfix Expression to Prefix Expression: void postfix_to_prefix(char postfix[], char prefix[]){ char op[2]; //operator string char poped1[MAX]; char poped2[MAX]; char temp[MAX]; int i = -1; op[1] = '\0'; while(postfix[++i]){ if(postfix[i] == ' '){ continue; } if(isoperator(postfix[i])){ ...

Infix to Postfix Expression Conversion

With a given Infix Expression, we will see how to convert Infix Expression into Postfix Expression using stack.   Algorithm to convert Infix expression to Postfix expression: In this algorithm, we will use operatorStack to store operators during the conversion. The step are as follows: Initialize empty  operatorStack While the end of input infix string symbol = next input character If symbol is an operand add symbol to the postfix string Else While( operatorStack is not empty && precedence of top character of operatorStack is higher than symbol ) topsymbol = pop the operatorStack add topsymbol to the postfix string End While If ( operatorStack is empty || symbol is not equal to '(' ) push symbol into operatorStack Else pop the operatorStack End If End If While the operatorStack is not empty topsymbol = pop the operatorStack add topsymbol to the postfix string End While     Function to convert Infix expression to Postf...

Prefix Expression Evaluation

With a given Prefix Expression, we will show you how to evaluate a prefix expression using stack.   Algorithm to evaluate Prefix Expression: In this algorithm, we will use stack to store operands. The step are as follows: Get the Prefix Expression String While the end of Pretfix string read from right to left If the current character is operand Push it into Stack End If If the current character is operator Pop stack for first_operand Pop stack for second_operand Evaluate the expression (first_operand)(operator)(second_operand) and push the result into stack End If End While Pop stack for result and return it     Function to evaluate Prefix Expression double evaluate_prefix_exp(char *prestr){ STACK stk; int i = strlen(prestr); double op1, op2; stk.top = -1; while(--i != -1){ if(prestr[i] == ' '){ //escape space continue; } if(isdig(prestr[i])){ push( &stk, (double)(prestr[i]-'0')/*char to int*/ ); } else{ op1 = pop(...

Postfix Expression Evaluation

With a given Postfix Expression, we will show you how to evaluate a postfix expression using stack.   Algorithm to evaluate postfix expression: In this algorithm, we will use stack to store operands. The step are as follows: Get the Postfix Expression String While the end of Postfix Expression string If the current character is operand Push it into Stack End If If the current character is operator Pop stack for second_operand Pop stack for first_operand Evaluate the expression (first_operand)(operator)(second_operand) and push the result into stack End If End While Pop stack for result and return it       Function to Evaluate Postfix Expression: double evaluate_postfix_exp(char *poststr){ STACK stk; //stack for pushing operands int i = -1; double op1, op2; stk.top = -1; //top of stack while(poststr[++i]){ if(poststr[i] == ' '){ //escape space continue; } if(isdig(poststr[i])){ push( &stk, (double)(poststr[i]-'0')/*char to int...

Parenthesis matching without using stack

If we have an expression in which only parenthesis '( )' is used and wanted to check that it has matching pairs, we can use this simple Technic to find out whether the expression is correct or not.   Algorithm for parenthesis matching without using stack: In this algorithm, we will use COUNTER to count the difference of count of opening and closing parenthesis. The step are as follows: Set COUNTER = 0 and VALID = true Get the input string While the end of string If the current character is left parenthesis Increment counter i.e. ++COUNTER If the current character is right parenthesis Decrement counter i.e. - -COUNTER If at any step closing parenthesis occur having no opening parenthesis i.e. COUNTER < 0 VALID = false Return  COUNTER == 0 &&  VALID Limitations: Can check only one type of bracket Function to check matching parenthesis without using stack: int check_parenthesis(char *str){ int counter = 0, i = -1, valid = 1; while(str[++i]){ if(s...