"A collection of essential coding algorithms and popular code solutions. Optimized for interviews and competitive programming, covering a wide range of topics from sorting to dynamic programming.”
26
stars
277
forks
source link
Implement Stacks using Linked Lists following the OOPS paradigm for coding #491
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:
push(data) – Adds an element to the top of the stack.
pop() – Removes and returns the top element from the stack. Should handle underflow (empty stack) properly.
peek() – Returns the top element without removing it.
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.
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:
Node
class to represent each element in the linked list, with:data
(the value of the node)next
(pointer to the next node)push(data)
– Adds an element to the top of the stack.pop()
– Removes and returns the top element from the stack. Should handle underflow (empty stack) properly.peek()
– Returns the top element without removing it.is_empty()
– Checks if the stack is empty and returns a boolean.Additional Guidelines:
Acceptance Criteria:
Example Usage:
Resources: