code hindi Information technologyCareer blogs Courses info Digital Marketing About

Stack kya hai?

Computer Science mein, "stack" ek linear data structure hota hai jo Last In First Out (LIFO) principle follow karta hai. Iska matlab hai ki jo element last mein stack mein insert hota hai, wahi pehla remove hota hai. Stack ka use kai algorithms aur applications mein hota hai.

Stack Operations
  • 1. Push : Stack mein ek new element add karna. Agar stack full hai (overflow condition), to push operation fail ho jata hai.
  • 2. Pop : Stack se ek element remove karna. Agar stack empty hai (underflow condition), to pop operation fail ho jata hai.
  • 3. Peek/Top : Stack ke top element ko dekhna bina usse remove kiye.
  • 4. isEmpty : Check karna ki stack empty hai ya nahi.
  • 5. isFull : Check karna ki stack full hai ya nahi (fixed size stacks ke liye).
Stack Applications
  • 1. Function Call Management : Recursive function calls ko manage karne ke liye stacks ka use hota hai. Jab bhi ek function call hota hai, call stack mein ek frame push hota hai, aur jab function return hota hai, frame pop hota hai.
  • 2. Expression Evaluation : Infix, prefix, aur postfix expressions ko evaluate karne ke liye stacks ka use hota hai.
  • 3. Syntax Parsing : Compilers aur interpreters stacks ka use syntax parsing aur expression evaluation ke liye karte hain.
  • 4. Undo Mechanism : Software applications mein undo operations ko implement karne ke liye stacks ka use hota hai.
  • 5. Balanced Parentheses : Strings mein balanced parentheses check karne ke liye stacks ka use hota hai.
  • 6. Depth-First Search (DFS) : Graph traversal algorithms jaise DFS mein stacks ka use hota hai.

Example Implementation

Here is a simple implementation of a stack in Python:

        
class Stack:
 def __init__(self):
    self.stack = []
       
 def is_empty(self):
    return len(self.stack) == 0
          
 def push(self, item):
    self.stack.append(item)
          
 def pop(self):
   if not self.is_empty():
       return self.stack.pop()
   else:
      return "Stack Underflow"
          
   def peek(self):
   if not self.is_empty():
     return self.stack[-1]
   else:
     return "Stack is empty"
          
  def size(self):
     return len(self.stack)
        
        
      

Example usage

        
          stack = Stack()
          stack.push(1)
          stack.push(2)
          stack.push(3)
          print(stack.pop())  # Outputs 3
          print(stack.peek())  # Outputs 2
          print(stack.is_empty())  # Outputs False
        
      

Is example mein, Stack class basic stack operations ko implement karti hai.

Server kya hota hai

Integrated Development Environment (IDE) kya hai