Skip to content

Add reverse method to linear data structures #664

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

Closed
arvinder004 opened this issue Mar 20, 2025 · 3 comments · May be fixed by #665
Closed

Add reverse method to linear data structures #664

arvinder004 opened this issue Mar 20, 2025 · 3 comments · May be fixed by #665

Comments

@arvinder004
Copy link
Contributor

Description of the problem

I propose adding a reverse method to linear data structures in the pydatastructs library, such as Array, LinkedList, Stack, and Queue. This method would reverse the order of elements in the data structure, enhancing the library's functionality for sequence manipulation. The addition of this method would provide a convenient and intuitive way for users to invert sequences, which is a common operation in algorithmic problem-solving and data processing tasks.

The implementation should:

  • Be in-place where possible (e.g., for Array and LinkedList) to optimize space complexity.
  • Handle edge cases such as empty structures or single-element structures gracefully.
  • Maintain consistency with the library's design (e.g., using snake_case naming conventions).

Proposed scope:

  • Array: Swap elements from start to end.
  • LinkedList: Reverse the direction of pointers.
  • Stack: Pop and push elements in reverse order (possibly returning a new stack).
  • Queue: Reverse the order of elements (possibly returning a new queue).

Example of the problem

from pydatastructs import Array, LinkedList

# Example with Array
arr = Array([1, 2, 3, 4])
arr.reverse()
print(arr)  # Expected output: [4, 3, 2, 1]

# Example with LinkedList
ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
ll.reverse()
print(ll)  # Expected output: 3 -> 2 -> 1
@prakrutipanchal
Copy link

@arvinder004 Are you currently working on this problem to set the reverse mechanism?

@arvinder004
Copy link
Contributor Author

yes I have made a PR in which i have added the reverse array method...waiting for review...after that I will push for linked lists, stack and queue as well

@prakrutipanchal
Copy link

prakrutipanchal commented Mar 25, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants