🏗️ 2.2: Numpy ile Stack Kodlama

Donanım Seviyesinde Stack Mantığı

📌 Ön Bilgi: Bu Sayfada Ne Öğreneceğiz?

Bu sayfada sabit boyutlu bir stack nasıl oluşturulur göreceğiz. Numpy kullanmamızın sebebi, C dilindeki diziler gibi sabit boyutlu, bitişik bellekte tutulan bir yapı elde etmektir.

Neden Sabit Boyutlu Stack?

Not: Pratikte Python'da stack için list kullanmak daha yaygındır (bir sonraki ders).

Stack Pointer (Top) Görselleştirme

💻 Tam Kod Implementasyonu

Python Kodu
import numpy as np

class NumpyStack:
    def __init__(self, capacity):
        self.capacity = capacity
        self.data = np.zeros(capacity, dtype=int)
        self.top = 0
        
    def push(self, value):
        if self.top == self.capacity:
            print("❌ Dolu!")
            return
        self.data[self.top] = value
        self.top += 1
        print(f"Push({value}) -> {self.data} (Top: {self.top})")
        
    def pop(self):
        if self.top == 0:
            print("❌ Boş!")
            return None
        self.top -= 1
        val = self.data[self.top]
        self.data[self.top] = 0 
        print(f"Pop() -> {val}")
        return val

# Test
s = NumpyStack(3)
s.push(10)
s.push(20)
s.push(30)
s.push(40) # Hata!
s.pop()
s.push(99)
Çıktı bekleniyor...