Skip to main content

Posts

Showing posts with the label stack

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...

Stack

A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the top of the stack. Since the items are inserted and deleted from one end, stack is sometime called a last-in-first-out or LIFO list. Operations on stack: PUSH : Push operation is used to insert an item at the top of the stack. POP : Pop operation is used to deleted an item from the top of the stack. Implementations of Stack: Array Implementation of Stack Stack Implementation using Structure Stack to store different data types Link List Implementation of Stack Applications of Stack Applications of Stack: Parenthesis matching without using Stack Postfix Expression Evaluation Prefix Expression Evaluation Infix to Postfix Conversion Postfix to Prefix Conversion Prefix to Postfix Conversion Postfix to Infix Conversion Prefix to Infix Conversion Implementation of Stack

Link List implementation of Stack

In this post, we will implement the stack using link list. First we define a node for link list as below: struct node{ int info; struct node *next; }; typedef struct node NODE; In the above code info hold the value stored in stack and next is pointer to next node in the list.   Push Operation: Now we will implement the push operation on the stack above defined. Let us see the code for push operation: void push(NODE **start, int n){ NODE *ptr = getNode(); ptr->info = n; ptr->next = *start; *start = ptr; }   Pop Operation: Now we will implement the pop operation on the stack above defined. Let us see the code for push operation: int pop(NODE **start){ NODE *temp = *start; int info; if(*start == NULL){ printf("\nNo element exists in the stack\n"); } else{ *start = (*start)->next; info = temp->info; free(temp); return info; } }   Program to implement stack using link list: Here is the complete implementation of stack usin...

Implementation of Stack to store different data types

Here we will see how to implement stack using structure and union to store different datatypes in stack. First we define the basic structure for elements/items to be stored in the stack. struct Items{ int ele_type; union { int ivalue; float fvalue; char *strvalue; } stack_element; }; In the above code, we have defined a Items structure to store the info about the type of items in ele_type variable and a union variable named stack_element to store the actual value of stack items. In union the, ivalue is used to store the integer value, fvalue to store floating value and strvalue to store the string value. If you wish to add other datatype, you can add any datatype within the union. Now we will define the structure to implement the stack. The code below: struct Stack{ int top; struct Items element[MAX]; }; In the above code, struct Stack is defined to hold the top of stack and the array of Items structure named element to hold the actual Items defined above. MA...

Stack implementation using structure

In this post, we will implement the stack using structure. First we define a Stack using structure as below: struct Stack{ int top; int elements[MAX]; }; In the above code top hold the value of top of stack and elements array is used to store the stack elements and MAX is the maximum number of element that the stack can hold before overflowing.   Push Operation: Now we will implement the push operation on the stack above defined. Let us see the code for push operation: void push(struct Stack *s, int item){ if(s->top == MAX-1){ printf("Stack Overflow\n"); } else{ s->elements[++(s->top)] = item; } }   Pop Operation: Now we will implement the pop operation on the stack above defined. Let us see the code for push operation: int pop(struct Stack *s){ if(s->top == -1){ printf("Stack Underflow\n"); } else{ return s->elements[(s->top)--]; } }   Program to implement stack using structure: Here is the complete implementation of ...

Array implementation of Stack

Here we see how to implement the stack using C array data type: #include <stdio.h> #define MAX 5 //maximum size of stack int top = -1; //top of stack int stack[MAX]; // stack int pop(); void push(int); void display(); // for displaying stack elements int main(){ push(1); push(3); push(5); display(); pop(); pop(); display(); pop(); pop(); return 0; } void push(int item){ if(top == MAX-1){ //check for overflow printf("Stack overflow\n"); } else{ stack[++top] = item; printf("Pushed item = %d\n", item); } } int pop(){ if(top == -1){ // check for underflow printf("Stack underflow\n"); } else{ return stack[top--]; } } void display(){ printf("The stack elements are: "); for( int i = 0; i <= top; i++){ printf("%d ", stack[i]); } printf("\n"); } Output: Pushed item = 1 Pushed item = 3 Pushed item = 5 The stack elements are: 1 3 5 The stack elements are: 1 Stack underflow