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 OperationsExample 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.