MCPcopy
hub / github.com/keon/algorithms / pop

Method pop

algorithms/data_structures/stack.py:170–184  ·  view source on GitHub ↗

Remove and return the top element. Returns: The top element. Raises: IndexError: If the stack is empty.

(self)

Source from the content-addressed store, hash-verified

168 self._top += 1
169
170 def pop(self) -> object:
171 """Remove and return the top element.
172
173 Returns:
174 The top element.
175
176 Raises:
177 IndexError: If the stack is empty.
178 """
179 if self.is_empty():
180 raise IndexError("Stack is empty")
181 value = self.head.value
182 self.head = self.head.next
183 self._top -= 1
184 return value
185
186 def peek(self) -> object:
187 """Return the top element without removing it.

Callers 1

Calls 1

is_emptyMethod · 0.45

Tested by 1