Skip to content

Added an add at position subroutiune to linked list - #9020

Merged
tianyizheng02 merged 14 commits into
TheAlgorithms:masterfrom
mendacium-a11y:linkedList
Sep 8, 2023
Merged

Added an add at position subroutiune to linked list#9020
tianyizheng02 merged 14 commits into
TheAlgorithms:masterfrom
mendacium-a11y:linkedList

Conversation

@mendacium-a11y

Copy link
Copy Markdown
Contributor

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:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@mendacium-a11ymendacium-a11y changed the title Linked listAdded an add at position subroutiune to linked listAug 30, 2023
>>> linked_list.add(3)
>>> linked_list.add_at_position(10, 1)
True

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ifcurrent: # Check if current is not None
ifcurrentisnotNone:

Comment on lines +60 to +61
else:
return False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
returnFalse
returnFalse

Comment on lines +47 to +48
if position < 0:
return False

Copy link
Copy Markdown
Contributor

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?

Suggested change
ifposition<0:
returnFalse
if0>position:
returnFalse

This also looks a bit more readable

self.head = Node(item, self.head)
self.size += 1

def add_at_position(self, item: Any, position: int) -> bool:

Copy link
Copy Markdown
Contributor

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?

Comment threaddata_structures/linked_list/__init__.py
@algorithms-keeperalgorithms-keeperBot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Sep 5, 2023
@algorithms-keeperalgorithms-keeperBot removed the tests are failing Do not merge until tests pass label Sep 6, 2023
@mendacium-a11y

Copy link
Copy Markdown
ContributorAuthor

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

@algorithms-keeperalgorithms-keeperBot added the tests are failing Do not merge until tests pass label Sep 6, 2023
@algorithms-keeperalgorithms-keeperBot removed the tests are failing Do not merge until tests pass label Sep 6, 2023
Comment threadCONTRIBUTING.md Outdated
@@ -1,4 +1,4 @@
# Contributing guidelines
<!-- # Contributing guidelines -->

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
ContributorAuthor

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!--# Contributing guidelines-->
# Contributing guidelines

Then please uncomment this line

Comment threaddata_structures/linked_list/__init__.py Outdated
Comment threaddata_structures/linked_list/__init__.py
@mendacium-a11y

Copy link
Copy Markdown
ContributorAuthor

added the doctest and updated the else code after checking the position is 0 or not

Comment threaddata_structures/linked_list/__init__.py
Comment threadCONTRIBUTING.md Outdated
@@ -1,4 +1,4 @@
# Contributing guidelines
<!-- # Contributing guidelines -->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!--# Contributing guidelines-->
# Contributing guidelines

Then please uncomment this line

@mendacium-a11y

Copy link
Copy Markdown
ContributorAuthor

fixed contributing.md and added the doctest for checing out of bounds posiiton, both positive and negative

@tianyizheng02
tianyizheng02 merged commit c9b4b80 into TheAlgorithms:masterSep 8, 2023
@tianyizheng02

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

@mendacium-a11y
mendacium-a11y deleted the linkedList branch September 9, 2023 07:02
@isidroasisidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviewsThis PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@mendacium-a11y@tianyizheng02@CaedenPH