Skip to content

Added an add at position subroutiune to linked list #9020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added doctest and updates the else after checking if posiiton argumen…
…t less than 0 or not
  • Loading branch information
mendacium-a11y committed Sep 7, 2023
commit 4a32c1f47dfd29ca85c96f88cbaec025df817876
16 changes: 13 additions & 3 deletions data_structures/linked_list/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ def add(self, item: Any, position: int = 0) -> None:
Args:
item (Any): The item to add to the LinkedList.
position (int, optional): The position at which to add the item.
Defaults to 0.
Defaults to 0.

Raises:
ValueError: If the position is negative.
ValueError: If the position is Out of Bounds.
ValueError: If the position is negative or out of bounds.

>>> linked_list = LinkedList()
>>> linked_list.add(1)
Expand All @@ -42,6 +41,17 @@ def add(self, item: Any, position: int = 0) -> None:
>>> linked_list.add(4, 2)
>>> print(linked_list)
3 --> 2 --> 4 --> 1

# Test adding to a negative position
>>> linked_list.add(5, -3)
Traceback (most recent call last):
...
ValueError: Position must be non-negative

# Test adding to an out-of-bounds position
>>> linked_list.add(5, 4)
>>> print(linked_list)
3 --> 2 --> 4 --> 1 --> 5
"""
if position < 0:
raise ValueError("Position must be non-negative")
Expand Down