Understanding Stack Applications : Infix to Postfix Conversion

Application of Stack (अनुप्रयोग)

1. Expression Evaluation And Conversion
(गणितीय अभिव्यक्ति का मूल्यांकन और रूपांतरण)

Mathematics और Programming में expression evaluation बहुत common है। Stack का use infix, postfix और prefix expressions को evaluate और convert करने के लिए होता है।

  • Infix Expression →  A + B
  • Postfix Expression →  AB+
  • Prefix Expression →  +AB

 Example :
अगर आपको (5 + 3) * 2 evaluate करना है, तो Stack operator और operand को store करके calculation को आसानी से manage करता है।

2. Function Call Handling (Recursion & Call Stack)

जब भी कोई Function दूसरे Function को Call करता है, तो Computer System को यह Track रखना होता है, कि Return कहाँ जाना है।
इसके लिए एक Special Stack Structure उपयोग में आता है — Call Stack

How it works :

  • जब कोई Function Call होता है, तो उसका Address और Local Variables Stack में Push किए जाते हैं।
  • Function Complete होने के बाद, वह Stack से Pop होकर Control वापस Calling Function को देता है।

Example: 

void A() {
B();
}
void B() {
C();
}

3. Undo/Redo Operations

MS Word, Notepad या Photoshop जैसे software में जब आप Undo दबाते हैं, तो पिछली activity stack से pop होकर restore हो जाती है।

How it works :

  • जब भी आप कोई Action करते हैं, तो वह एक Stack में Store होता है (Undo Stack)।
  • जब “Undo” करते हैं, तो वह Action Pop होकर Reverse हो जाता है।
  • “Redo” करने पर वही Action फिर से Push होकर Apply हो जाता है।

Example :

  • You typed “Computer” → (Action Pushed)
  • Undo → “Computer” Removed (Action Popped)
  • Redo → “Computer” Restored (Action Pushed Again)

4. Reversing a String or Number

Stack का एक Common और Easy Application है — किसी String या Number को Reverse करना।

Example :
Input : “HELLO”
Process : Push all characters → Pop all characters → Result = OLLEH

5. Memory Management (मेमोरी प्रबंधन)

Computer Memory को Efficiently Allocate और Deallocate करने में Stack का बहुत बड़ा Role है।

In System Programming :

  • Stack Memory में Temporary Variables Store होते हैं।
  • Function Execution खत्म होते ही Memory Automatically Free हो जाती है।
  • इससे Memory Leak की Problem नहीं होती।

Conversion of infix to postfix notation using stack
(Stack का उपयोग करके infix को postfix notation में रूपांतरित करना)

यह algorithm एक simple process पर आधारित है। हम Infix expression को बाएं (left) से दाएं (right) scan करते हैं, और हर character (operand, operator, parenthesis) को एक-एक करके process करते हैं।

यहाँ कुछ rules follow करते हैं :

  1. Precedence of Operators :
    • Exponentiation ( ^ ) की सबसे ज्यादा priority होती है।
    • Multiplication ( * ) और Divide ( / ) की medium priority होती है।
    • Add ( + ) और Subtraction ( – ) की सबसे कम priority होती है।
  2. Associativity of Operators :
    • Exponentiation ( ^ ) right-to-left associative है।
    • बाकी सभी operators ( * , / , + , – ) left-to-right associative हैं।

Step-by-Step Algorithm :

  1. Start
  2. एक Empty Stack बनाएं (Operators को Store करने के लिए)
  3. Expression को Left से Right Read करें
  4. यदि Character Operand (A-Z / 0-9) है → Postfix Expression में Add करें
  5. यदि Character ‘(’ है → Stack में Push करें
  6. यदि Character ‘)’ है → Stack से ‘(’ मिलने तक सभी Operators को Pop करके Postfix में Add करें
  7. यदि Character Operators (+, -, , /, ^) है :
    • Stack के Top Operator की Precedence Check करें
    • यदि Stack खाली है या Top ‘(’ है → Push करें
    • यदि Stack के Top की Precedence ≥ Current Operator की Precedence है → Pop करें और Postfix में Add करें
    • फिर Current Operator को Stack में Push करें
  8. सभी Characters Process होने के बाद → Stack में बचे हुए सभी Operators को Postfix में Add करें
  9. End

Example – Infix   :  A + B * C

Steps :

  • Scan A → Operand → Postfix = A
  • Scan + → Push to stack → Stack = +
  • Scan B → Operand → Postfix = A B
  • Scan * → Higher precedence than + → Push → Stack = + *
  • Scan C → Operand → Postfix = A B C

अब stack से pop करो → * फिर +
Final Postfix = A B C * +

Evaluation of postfix expression
(Satck का उपयोग करके postfix expression का मूल्यांकन करना)

Stack इस Process की Backbone है। हम Stack का उपयोग Operands को Store करने के लिए करते हैं। जब कोई Operator आता है, तो Stack से Required Operands को Pop करते हैं, Operation Perform करते हैं, और Result फिर से Stack में Push कर देते हैं।

Step-by-Step Algorithm

  1. Start
  2. एक Empty Stack बनाइए
  3. Expression को Left से Right Read कीजिए
  4. अगर Symbol Operand (Number या Variable) है → Stack में Push कीजिए
  5. अगर Symbol Operator (+, -, *, /, ^) है :
    • Stack से दो Operands Pop कीजिए
    • Operation Perform कीजिए (Operand2 Operator Operand1)
    • Result को Stack में Push कीजिए
  6. Step 3-5 को Expression के अंत तक Repeat कीजिए
  7. Expression खत्म होने पर Stack के Top पर Final Result होगा
  8. End

Example – 6 2 3 + – 3 8 2 / + * 2 ^

Step by Step :

  1. Push 6 → Stack : [6]
  2. Push 2 → [6, 2]
  3. Push 3 → [6, 2, 3]
  4. Operator + → Pop(3,2) → 2+3=5 → Push 5 → [6, 5]
  5. Operator – → Pop(5,6) → 6-5=1 → Push 1 → [1]
  6. Push 3 → [1,3]
  7. Push 8 → [1,3,8]
  8. Push 2 → [1,3,8,2]
  9. Operator / → Pop(2,8) → 8/2=4 → Push 4 → [1,3,4]
  10. Operator + → Pop(4,3) → 3+4=7 → Push 7 → [1,7]
  11. Operator * → Pop(7,1) → 1*7=7 → Push 7 → [7]
  12. Push 2 → [7,2]
  13. Operator ^ → Pop(2,7) → 7^2=49 → Push 49 → [49]

 Final Result = 49

error: Content is protected !!