Skip to content

Add reverse method to linear data structures #664

Closed
@arvinder004

Description

@arvinder004

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions