From 0ae6b3ad343a0eec2d97c6811df2f57f88e63832 Mon Sep 17 00:00:00 2001 From: Kashish Juneja <69794677+KashishJuneja101003@users.noreply.github.com> Date: Sun, 20 Oct 2024 13:33:29 +0530 Subject: [PATCH] Added postfix to prefix --- Postfix to Prefix/code.c | 95 +++++++++++++++++++++++++++++++++++++ Postfix to Prefix/readme.md | 40 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 Postfix to Prefix/code.c create mode 100644 Postfix to Prefix/readme.md diff --git a/Postfix to Prefix/code.c b/Postfix to Prefix/code.c new file mode 100644 index 00000000..8fd26af5 --- /dev/null +++ b/Postfix to Prefix/code.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include + +// Define the maximum size for the stack and expression +#define MAX 100 + +// Stack structure to store strings (expressions) +struct Stack { + int top; + char* items[MAX]; +}; + +// Function to initialize the stack +void initStack(struct Stack* stack) { + stack->top = -1; +} + +// Function to push an item onto the stack +void push(struct Stack* stack, char* str) { + if (stack->top >= MAX - 1) { + printf("Stack overflow\n"); + return; + } + stack->items[++(stack->top)] = strdup(str); // strdup duplicates the string +} + +// Function to pop an item from the stack +char* pop(struct Stack* stack) { + if (stack->top == -1) { + printf("Stack underflow\n"); + return NULL; + } + return stack->items[(stack->top)--]; +} + +// Function to check if a character is an operator +int isOperator(char ch) { + return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^'); +} + +// Function to convert a Postfix expression to Prefix +void postfixToPrefix(char* postfix) { + struct Stack stack; + initStack(&stack); + + int length = strlen(postfix); + + // Traverse the Postfix expression from left to right + for (int i = 0; i < length; i++) { + // If the character is an operand, push it onto the stack + if (isalnum(postfix[i])) { + char operand[2] = {postfix[i], '\0'}; // Create a string for the operand + push(&stack, operand); + } + // If the character is an operator + else if (isOperator(postfix[i])) { + // Pop the top two operands from the stack + char* operand1 = pop(&stack); + char* operand2 = pop(&stack); + + // Create a new string: (operator + operand2 + operand1) + char* expression = (char*)malloc(strlen(operand1) + strlen(operand2) + 2); + sprintf(expression, "%c%s%s", postfix[i], operand2, operand1); + + // Push the resulting expression back onto the stack + push(&stack, expression); + + // Free dynamically allocated memory + free(operand1); + free(operand2); + } + } + + // The final result is the remaining element in the stack + char* result = pop(&stack); + printf("Prefix Expression: %s\n", result); + + // Free the final result memory + free(result); +} + +int main() { + char postfix[MAX]; + + // Input the Postfix expression + printf("Enter a Postfix expression: "); + scanf("%s", postfix); + + // Convert and display the Prefix expression + postfixToPrefix(postfix); + + return 0; +} diff --git a/Postfix to Prefix/readme.md b/Postfix to Prefix/readme.md new file mode 100644 index 00000000..9cd183e5 --- /dev/null +++ b/Postfix to Prefix/readme.md @@ -0,0 +1,40 @@ +# Postfix to Prefix Conversion + +## Description: +This project provides a tool for converting arithmetic expressions from Postfix notation (Reverse Polish Notation) to Prefix notation (Polish Notation). Postfix notation places operators after their operands, while Prefix notation places operators before their operands. This conversion allows for more intuitive evaluation of expressions and easier parsing in systems that do not rely on operator precedence. + +## Problem Definition: +Given an arithmetic expression in Postfix form (e.g., A B + C *), the objective is to convert it to Prefix form (e.g., * + A B C) using an algorithm that handles the structure correctly. + +## Key Elements: +- Operands: Variables or numbers. +- Operators: (+, -, *, /, ^). + +The output of converting a postfix expression to a prefix expression should: +1. Correctly place operators before their operands. +2. Avoid ambiguity by ensuring operators are placed with the right precedence. + +## Algorithm +1. **Initialize a Stack:** Create an empty stack to store the operands and intermediate expressions. +2. **Read the Postfix Expression:** Start reading the postfix expression from left to right. +3. **Process Each Symbol:** + - **If the Symbol is an Operand:** + - Push it onto the stack. + - **If the Symbol is an Operator:** + - Pop the top two operands from the stack. Let’s call them operand1 and operand2. + - Form a new prefix expression in the format: (operator operand2 operand1) (since postfix is reverse, operands are taken in reverse order). + - Push this new string back onto the stack. +4. **Repeat:** Continue this process until all symbols in the postfix expression are processed. +5. **Final Result:** The stack will contain one element at the end, which is the required prefix expression. + +## Example +For the Postfix expression: A B + C * +- **Step 1:** A is an operand, push it → Stack: [A] +- **Step 2:** B is an operand, push it → Stack: [A, B] +- **Step 3:** + is an operator, pop B and A, form + A B, push it → Stack: [+ A B] +- **Step 4:** C is an operand, push it → Stack: [+ A B, C] +- **Step 5:** * is an operator, pop C and + A B, form * + A B C, push it → Stack: [* + A B C] +**Final Prefix output:** * + A B C + +## Time Complexity +The algorithm performs a single scan of the postfix expression, making it **O(n)** in time complexity, where n is the number of characters in the input expression. Each operand and operator is pushed and popped from the stack at most once, ensuring linear complexity. \ No newline at end of file