1. Learn
  2. /
  3. Courses
  4. /
  5. Data Structures and Algorithms in Python

Exercise

Implementing the pop method for a stack

In this exercise, you will implement the pop() operation for a stack. pop() will be used to remove an element from the top of the stack. Again, we will consider the size attribute to know the number of elements in the stack.

Recall the Node() class:

class Node:
  def __init__(self, data):
    self.data = data
    self.next = None

Instructions 1/2

undefined XP
    1
    2
  • Check if there is a top element in the stack.
  • Decrement the size of the stack by one if there isn't a top element.
  • Update the new value for the top node.