스택의 pop 메서드 구현하기
이 연습 문제에서는 스택의 pop() 연산을 구현해 보겠습니다. pop()은 스택의 맨 위 요소를 제거할 때 사용합니다. 다시 한 번, 스택에 들어 있는 요소의 개수를 알기 위해 size 속성을 사용합니다.
Node() 클래스를 다시 살펴보세요:
class Node:
def __init__(self, data):
self.data = data
self.next = None
이 연습은 강의의 일부입니다
Python으로 배우는 자료구조와 알고리즘
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
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