Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help solving methods Implement a Stack ADT class by completing the provided skeleton code in the file stack_sll.py. You will use a chain of

need help solving methods

Implement a Stack ADT class by completing the provided skeleton code in the file stack_sll.py. You will use a chain of Singly-Linked Nodes (the provided SLNode) as the underlying storage for your Stack ADT. Be sure to review the Exploration on Stacks for an example. 2. Your Stack ADT implementation will include the following standard Stack methods: push() pop() top() 3. We will test your implementation with different types of objects, not just integers. We guarantee that all such objects will have correct implementations of methods __eq__(), __lt__(), __gt__(), __ge__(), __le__(), and __str__(). 4. The number of objects stored in the Stack at any given time will be between 0 and 1,000,000 inclusive. The stack must allow for the storage of duplicate objects. 5. RESTRICTIONS: You are not allowed to use ANY built-in Python data structures and/or their methods

from SLNode import SLNode class StackException(Exception): """ Custom exception to be used by Stack class DO NOT CHANGE THIS METHOD IN ANY WAY """ pass class Stack: def __init__(self) -> None: """ Initialize new stack with head node DO NOT CHANGE THIS METHOD IN ANY WAY """ self._head = None def __str__(self) -> str: """ Return content of stack in human-readable form DO NOT CHANGE THIS METHOD IN ANY WAY """ out = 'STACK [' if not self.is_empty(): node = self._head out = out + str(node.value) node = node.next while node: out = out + ' -> ' + str(node.value) node = node.next return out + ']' def is_empty(self) -> bool: """ Return True is the stack is empty, False otherwise DO NOT CHANGE THIS METHOD IN ANY WAY """ return self._head is None def size(self) -> int: """ Return number of elements currently in the stack DO NOT CHANGE THIS METHOD IN ANY WAY """ node = self._head length = 0 while node: length += 1 node = node.next

return length # ----------------------------------------------------------------------- def push(self, value: object) -> None: """ TODO: Write this implementation """ pass def pop(self) -> object: """ TODO: Write this implementation """ pass def top(self) -> object: """ TODO: Write this implementation """ pass # ------------------- BASIC TESTING ----------------------------------------- if __name__ == "__main__": print(" # push example 1") s = Stack() print(s) for value in [1, 2, 3, 4, 5]: s.push(value) print(s) print(" # pop example 1") s = Stack() try: print(s.pop()) except Exception as e: print("Exception:", type(e)) for value in [1, 2, 3, 4, 5]: s.push(value) for i in range(6): try: print(s.pop()) except Exception as e: print("Exception:", type(e)) print(" # top example 1") s = Stack() try: s.top() except Exception as e: print("No elements in stack", type(e)) s.push(10) s.push(20) print(s) print(s.top()) print(s.top())

print(s)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions