DS Ass5
DS Ass5
DATE:07 / 08 / 2024
AIM:
To Create C program to implement the stack and perform the conversion of infix
expression ro postfix expression
ALGORITHM:
1. Start
2. Initialize an empty stack `top`.
3. Read the infix expression as a string `a`.
4. For each character `ch` in the string `a`:
- If `ch` is an operand (i.e., a letter or a digit):
- Print `ch`.
- Else if `ch` is an open parenthesis `(`:
- Push `ch` onto the stack.
- Else if `ch` is a close parenthesis `)`:
- While the stack is not empty and the top of the stack is not an open parenthesis
`(`:
- Pop from the stack and print the popped operator.
- Pop the open parenthesis `(` from the stack.
- Else (it is an operator):
- While the stack is not empty and the precedence of the operator on the top of
the stack is greater than or equal to the precedence of `ch`:
- Pop from the stack and print the popped operator.
- Push `ch` onto the stack.
5. While the stack is not empty:
- Pop from the stack and print the popped operator.
6. End
SOURCE CODE :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char ope;
struct node *next;
} *top = NULL;
void push(char op) {
struct node* nn = (struct node*)malloc(sizeof(struct node));
nn->ope = op;
nn->next = top;
top = nn;
}
char peek() {
return top ? top->ope : '\0';
}
char pop() {
if (top == NULL) {
printf("Stack is empty!\n");
return '\0';
}
char op = top->ope;
struct node* temp = top;
top = top->next;
free(temp);
return op;
}
int main() {
char a[20];
printf("Enter the infix expression: ");
scanf("%s", a);
int l = strlen(a);
int i;
printf("\n");
return 0;
}
SAMPLE OUTPUT :
RESULT: