2-Maruza. Mt va Algoritmlar


Download 2.59 Mb.
Pdf ko'rish
bet5/5
Sana17.10.2023
Hajmi2.59 Mb.
#1705620
1   2   3   4   5
Bog'liq
2-Maruza. MT va Algoritmlar

Ikki asosiy operatsiya:
Push: stekning yuqori qismiga element qo‘shish.
Pop: Yuqori elementni stekdan olib tashlash.


STACK lar
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
raise IndexError("Pop from an empty stack")
def peek(self):
if not self.is_empty():
return self.items[-1]
def size(self):
return len(self.items)
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print("Top element:", stack.peek()) # Output: 3
print("Stack size:", stack.size()) # Output: 3
while not stack.is_empty():
print("Popped:", stack.pop())
# OUTPUT:
# POPPED: 3
# POPPED: 2
# POPPED: 1

Download 2.59 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling