hey, you need to do this=
To use a stack TAD (Abstract Data Type) in Python, you do not need to compile the code, as Python is an interpreted language. However, you can implement a stack TAD in Python using a list. Here's a simple example of how you could do this:
class Pilha:
def __init__(self):
self.itens = []
def esta_vazia(self):
return len(self.itens) == 0
def empilhar(self, item):
self.itens.append(item)
def desempilhar(self):
if not self.esta_vazia():
return self.itens.pop()
else:
raise IndexError("A pilha está vazia")
def topo(self):
if not self.esta_vazia():
return self.itens[-1]
else:
raise IndexError("A pilha está vazia")
# Exemplo of use
pilha = Pilha()
print("A pilha está vazia?", pilha.esta_vazia())
pilha.empilhar(1)
pilha.empilhar(2)
pilha.empilhar(3)
print("Topo da pilha:", pilha.topo())
pilha.desempilhar()
print("Topo da pilha após desempilhar:", pilha.topo())