Menu

[Solved]Question 5 7 Marks Stack Datatype Holds Collection Elements Three Main Operations Pop Remo Q37248239

Python
Question 5. (7 marks) A stack is a datatype that holds a collection of elements and has three main operations: pop, which remQuestion 5. (7 marks) A stack is a datatype that holds a collection of elements and has three main operations: pop, which removes and returns the first element of the collection, peek, which returns the first element (without modifying the stack) and push, which adds an element to the front of the collection (i.e., adds a new first element) Implement a stack as a linked lists by writing the following methods, according to the docstrings specifications: class Node: def init (self): Initialize a Node”’ self.cargo None self.next None class Stack: def init (self) Initialize a Stack”’ self. length 0 # the number of elements in the stack self.head None a) [1 mark] def is_empty (self) (Stack)> bool output: Return vrue if the stack is empty and False othervise. b) [2 marks] def peek(self) (Stack) -> the type of the cargo or NoneType Output: Return the cargo of the element at the front of the Stack or None if the Stack is empty Show transcribed image text Question 5. (7 marks) A stack is a datatype that holds a collection of elements and has three main operations: pop, which removes and returns the first element of the collection, peek, which returns the first element (without modifying the stack) and push, which adds an element to the front of the collection (i.e., adds a new first element) Implement a stack as a linked lists by writing the following methods, according to the docstrings specifications: class Node: def init (self): Initialize a Node”’ self.cargo None self.next None class Stack: def init (self) Initialize a Stack”’ self. length 0 # the number of elements in the stack self.head None a) [1 mark] def is_empty (self) (Stack)> bool output: Return vrue if the stack is empty and False othervise. b) [2 marks] def peek(self) (Stack) -> the type of the cargo or NoneType Output: Return the cargo of the element at the front of the Stack or None if the Stack is empty

Expert Answer


Answer to Question 5. (7 marks) A stack is a datatype that holds a collection of elements and has three main operations: pop, whic… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *