Implementacja metody pop dla stosu
W tym ćwiczeniu zaimplementujesz operację pop() dla stosu. pop() służy do usuwania elementu z wierzchołka stosu. Podobnie jak wcześniej, do śledzenia liczby elementów w stosie używamy atrybutu size.
Przypomnienie klasy Node():
class Node:
def __init__(self, data):
self.data = data
self.next = None
To ćwiczenie jest częścią kursu
Struktury danych i algorytmy w Pythonie
Interaktywne ćwiczenie praktyczne
Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.
class Stack:
def __init__(self):
self.top = None
self.size = 0
def pop(self):
# Check if there is a top element
if self.____ is None:
return None
else:
popped_node = self.top
# Decrement the size of the stack
self.size -= ____
# Update the new value for the top node
self.top = self.____
popped_node.next = None
return popped_node.data