स्टैक के लिए pop मेथड इम्प्लीमेंट करना
इस अभ्यास में, आप स्टैक के लिए pop() ऑपरेशन इम्प्लीमेंट करेंगे। pop() स्टैक के टॉप से एक एलिमेंट हटाने के लिए इस्तेमाल होगा। फिर से, हम स्टैक में एलिमेंट्स की संख्या जानने के लिए size एट्रिब्यूट पर विचार करेंगे।
Node() क्लास याद करें:
class Node:
def __init__(self, data):
self.data = data
self.next = None
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Data Structures और Algorithms
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
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