Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In class, we discussed the abstract data structure Stack. A stack is a collection of items where items are added to and removed from
In class, we discussed the abstract data structure Stack. A stack is a collection of items where items are added to and removed from the top (LIFO). Use the Node class (an object with a data field and a pointer to the next element) to implement the stack data structure with the following operations: 6 4 2 Stack() creates a new stack that is empty. It needs no parameters and returns nothing push(item) adds a new Node with value=item to the top of the stack. It needs the item and returns nothing. pop()) removes the top Node from the stack. It needs no parameters and returns the value of the Node removed from the stack. Modifies the stack. peek() returns the value of the top Node from the stack but does not remove it. It needs no parameters. The stack is not modified. isEmpty() tests to see whether the stack is empty. It needs no parameters and returns a boolean value. len() returns the number of items on the stack. It needs no parameters and returns an integer. (You can add count in the Stack s constructor) EXAMPLE >>> x=Stack() >>> x.pop() Stack is empty >>> x.push (2) >>> x.push (4) >>> x.push (6) >>> X Top: Node (6) Stack: >>> x.pop() 6 >>> X Top: Node (4) Stack: 4 2 >>> len (x) 2 >>> x.isEmpty() False >>> x.push (15) >>> X Top: Node (15) Stack: 15 4 2 >>> x. peek () 15 >>> X Top: Node (15) Stack: 15 4 2 NOTE: To grade this assignment, the grading script will perform a series of mixed stack operations and compare the final status of your stack. Verify that all your methods work correctly when mixed together. Tips: - Make sure you update the top pointer according to the operation performed Starter code contains the special methods str and repr__, stack operations are updating the elements in the stack correctly use them to ensure the When a method is asking to return the value of a node, make sure you are returning node.value and not a Node object
Step by Step Solution
★★★★★
3.38 Rating (154 Votes )
There are 3 Steps involved in it
Step: 1
CODE class Node def initself value selfvalue value selfnext None def strself ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started