-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Added an add at position subroutiune to linked list #9020
Conversation
>>> linked_list.add(3) | ||
>>> linked_list.add_at_position(10, 1) | ||
True | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here there should be a test displaying linked_list
current = current.next | ||
counter += 1 | ||
|
||
if current: # Check if current is not None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if current: # Check if current is not None | |
if current is not None: |
else: | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else: | |
return False | |
return False |
if position < 0: | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should raise an error?
if position < 0: | |
return False | |
if 0 > position: | |
return False |
This also looks a bit more readable
@@ -25,6 +25,41 @@ def add(self, item: Any) -> None: | |||
self.head = Node(item, self.head) | |||
self.size += 1 | |||
|
|||
def add_at_position(self, item: Any, position: int) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about insert_at_position
?
updated the the code by dropping the add_at_position, instead updated the add function to take an optional position argument to specify of we want to insert the node at a specific position in the linked list |
CONTRIBUTING.md
Outdated
@@ -1,4 +1,4 @@ | |||
# Contributing guidelines | |||
<!-- # Contributing guidelines --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't edit the contributing guidelines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems to have happened by mistake, didn't intend to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<!-- # Contributing guidelines --> | |
# Contributing guidelines |
Then please uncomment this line
added the doctest and updated the else code after checking the position is 0 or not |
CONTRIBUTING.md
Outdated
@@ -1,4 +1,4 @@ | |||
# Contributing guidelines | |||
<!-- # Contributing guidelines --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<!-- # Contributing guidelines --> | |
# Contributing guidelines |
Then please uncomment this line
fixed contributing.md and added the doctest for checing out of bounds posiiton, both positive and negative |
Thanks for your contribution! |
* added addAtPosition to simple linked list * added addAtPosition to simple linked list * modified the add function to take an optional position command * fixed type safety errors: * fixed type safety errors: * fixed type safety errors: * fixed type safety errors: * fixed size error * fixed size error * added doctest and updates the else after checking if posiiton argument less than 0 or not * added doctest and updates the else after checking if posiiton argument less than 0 or not * fixed the contributing.md mistake * added doctest for out of bounds position value, both negative and positive
Describe your change:
This pull request adds the
add_at_position
function to the linked list implementation. This function allows inserting an item at a specified position within the linked list.Checklist: