Skip to content
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

Implement Stacks using Linked Lists following the OOPS paradigm for coding #491

Open
raxvab opened this issue Oct 20, 2024 · 1 comment
Open

Comments

@raxvab
Copy link

raxvab commented Oct 20, 2024


Implement Stacks using Linked Lists following the OOPS paradigm for coding

Description:

We need to implement a Stack data structure using a Linked List in Python, adhering to the Object-Oriented Programming (OOP) paradigm. The implementation should encapsulate the behavior of a stack and the operations it supports while leveraging the flexibility of linked lists for dynamic memory management.

Requirements:

  • Create a Node class to represent each element in the linked list, with:
    • data (the value of the node)
    • next (pointer to the next node)
  • Implement the Stack class, which internally manages the stack using nodes.
    • Methods to implement:
      1. push(data) – Adds an element to the top of the stack.
      2. pop() – Removes and returns the top element from the stack. Should handle underflow (empty stack) properly.
      3. peek() – Returns the top element without removing it.
      4. is_empty() – Checks if the stack is empty and returns a boolean.
  • Ensure that the stack operations work in constant time, i.e., O(1) for both push and pop.

Additional Guidelines:

  • Follow OOP principles such as encapsulation and abstraction.
  • The code should be easy to read and maintain, with clear and concise comments explaining key sections.
  • Handle edge cases such as popping or peeking from an empty stack.

Acceptance Criteria:

  • The stack should function correctly for a series of push and pop operations.
  • Ensure that no errors occur when performing operations on an empty stack.
  • Write basic test cases to verify functionality.

Example Usage:

stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)

print(stack.peek())  # Should output 30
print(stack.pop())   # Should remove and output 30
print(stack.pop())   # Should remove and output 20
print(stack.is_empty())  # Should return False

Resources:

  • You may refer to the concept of linked lists and stacks if needed, but the final implementation should reflect a solid understanding of OOP principles.

@raxvab
Copy link
Author

raxvab commented Oct 20, 2024

Hi @Ayush7-BIT I've raised one PR for this feature
Create Stacks.py #492
Please merge this PR

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

No branches or pull requests

1 participant