Stack Data Structure: LIFO Principle, Postfix, Expression Evaluation, and Recursion

0

🏗️ Stack and Applications (Stack and its Applications)

Stack ek linear data structure hai jo ek specific order mein operations ko follow karta hai. Is order ko LIFO (Last-In, First-Out) kaha jaata hai. Imagine karo ki yeh plates ka ek tower hai—jis plate ko aapne aakhri mein rakha (Last-In), use hi aap sabse pehle uthaenge (First-Out).

Stack ke Mukhya Operations (Main Operations of Stack)

Stack do mukhya operations par kaam karta hai:

  1. PUSH: Stack mein upar se ek naya item add karna. Jab bhi koi naya item aata hai, Top pointer ek position upar chala jaata hai.

  2. POP: Stack mein sabse upar (Top) waale item ko remove karna. Item remove hone par Top pointer ek position neeche aa jaata hai.

  3. PEEK (ya TOP): Bina remove kiye stack ke sabse upar waale item ko dekhna.

Stack ki Applications (Applications of Stack)

Stack ka upyog computer science mein kayi important kaamo mein hota hai, jinme se teen pramukh hain:

  1. Expression Evaluation aur Conversion

  2. Recursion

  3. Function Call Management


1. Postfix Notation (Post-fix Notation)

Hum generally jo expressions likhte hain, woh Infix Notation mein hote hain (jaise: A + B). Stack ka sabse aam upyog expressions ko Infix se Postfix ya Prefix mein convert karne aur unhe evaluate (hal karne) mein hota hai.

Teen Tarah ki Notations

NotationFormatExampleDescription
InfixOperand Operator OperandA + BIsme operator do operands ke beeche aata hai. Yeh humari aam likhne ki style hai.
PrefixOperator Operand Operand+ A BIsme operator do operands se pehle aata hai.
PostfixOperand Operand OperatorA B +Isme operator do operands ke baad mein aata hai.

Postfix ka Fayda

Computer Postfix notation ko aasaani se aur tezi se solve kar sakta hai (evaluate kar sakta hai) kyunki isme Operator Precedence (Kiski Priority Pehle Hai) aur Parentheses (Brackets) ke rules ko yaad rakhne ki zaroorat nahi padti.


2. Expression Evaluation (Postfix)

Stack ka upyog Postfix expressions ko solve karne (evaluate) ke liye kiya jaata hai.

Postfix Evaluation ka Algorithm

  1. Expression ko left se right scan karna shuru karein.

  2. Agar scanned element ek Operand (number) hai, toh use Stack mein Push kar dein.

  3. Agar scanned element ek Operator (+, -, *, /) hai, toh:

    • Stack se do Operands ko Pop karein (maana ki yeh Op2 aur Op1 hain).

    • Operation ko perform karein: Result = Op1 Operator Op2.

    • Result ko wapas Stack mein Push kar dein.

  4. Jab poora expression scan ho jaaye, toh Stack mein sirf ek hi value bachegi. Wohi final result hai.

Example: Postfix Expression: 2 3 1 * + 9 -

Scanned ItemStack ActionStack Content (Bottom to Top)
2Push 2[2]
3Push 3[2, 3]
1Push 1[2, 3, 1]
*Pop 1, Pop 3. Calculate $3 * 1 = 3$. Push 3.[2, 3]
+Pop 3, Pop 2. Calculate $2 + 3 = 5$. Push 5.[5]
9Push 9[5, 9]
-Pop 9, Pop 5. Calculate $5 - 9 = -4$. Push -4.[-4]

Final Result: Stack mein bacha hua element -4 hai.


3. Recursion (Punravritti)

Recursion tab hoti hai jab ek function apne aap ko call karta hai. Stack ka upyog computer system Recursion ko manage karne ke liye karta hai.

Function Calls aur Activation Record

Jab bhi koi function call hota hai (chahe woh doosra function ho ya khud wahi function ho – yani Recursive Call), toh system ek Activation Record (ya Stack Frame) bana kar use Call Stack mein Push karta hai.

Activation Record mein hota hai:

  • Function ke local variables.

  • Return Address (kahan se function ko wapas aana hai).

  • Function ke arguments/parameters.

Recursion Mein Stack ki Bhumika

  • Har recursive call ek naya Activation Record banata hai aur use Stack par Push karta hai.

  • Jab ek recursive call khatam ho jaati hai (jab Base Condition poori ho jaati hai), toh uske Activation Record ko Stack se Pop kar diya jaata hai, aur program Pichle Return Address par wapas chala jaata hai.

Stack yeh ensure karta hai ki har function call ke independent variables aur return points alag-alag aur sahi order mein store ho. Isi wajah se Recursion successfully execute ho pata hai.

Tags

Post a Comment

0Comments
Post a Comment (0)