Remove and return the top element. Returns: The top element. Raises: IndexError: If the stack is empty.
(self)
| 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. |