🐍 2.4: Python ile Stack Pratiği

Listeler ve Sarmalayıcı Sınıflar (Wrapper Classes)

1. En Kolay Yol: Python Listesi

Python'da list veri yapısı zaten bir Stack'tir.

2. Wrapper Class

Python Kodu
class Stack:
    def __init__(self):
        self._items = []
        
    def push(self, item):
        self._items.append(item)
        print(f"-> Push: {item}")
        
    def pop(self):
        if self.is_empty(): return "Boş!"
        item = self._items.pop()
        print(f"<- Pop: {item}")
        return item
        
    def peek(self):
        if self.is_empty(): return None
        return self._items[-1]
        
    def is_empty(self):
        return len(self._items) == 0

s = Stack()
s.push("Kitap 1")
s.push("Kitap 2")
print(f"En üst: {s.peek()}")
s.pop()
s.pop()
Çıktı bekleniyor...