In class, we discussed the abstract data structure Stack. A stack is a collection of items...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 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
Expert Answer:
Answer rating: 100% (QA)
CODE class Node def initself value selfvalue value selfnext None def strself ... View the full answer
Related Book For
Posted Date:
Students also viewed these mathematics questions
-
We will assume that the fixed cost for the cleaning supply for the hospital is $10 and the cost of each cleaning crew hired is $5 per hour. Labor TP MP AP TFC TVC TC MC AFC AVC ATC 0 0 1 3 2 8 3 14 4...
-
In Figure particles 1 and 2 are fixed in place on an x axis, at a separation of L = 8.00cm. Their charges are q1 = + e and q2 = ?? 27e.Particle 3 with charge q3 = + 4e is to be placed on the line...
-
X is the 3-dimensional Gaussian random vector with expected value μX = [4 8 6] and covariance Calculate (a) The correlation matrix, RX, (b) The PDF of the first two components of X,...
-
KEA10. LO 16.3 Use the following excerpts from Fruitcake Company's financial records to determine net cash flows from investing activities. Acquired new plant assets Collected interest on investment...
-
A food processor sells canned vegetables to supermarket chains. The supermarkets consider dented cans to be nonconforming, since consumers are reluctant to buy them. The supermarkets would like to...
-
Chlorobenzene is produced by direct chlorination of benzene. However, a parallel reaction that produces dichlorobenzene hexachloride also occurs. The parallel reactions are shown here. The single...
-
Two water jets of equal size and speed strike each other as shown in Fig. P5.57. Determine the speed, \(V\), and direction, \(\theta\), of the resulting combined jet. Gravity is negligible. Figure...
-
True, False, or Uncertain: If both EBV and Owl have the same GVM, then the value multiple of Owl will be lower than the value multiple of EBV.
-
The attached code provides a program related to binary trees. Analyze the code to determine what it does. In a text box response, describe what the code does and write out both the trees that it...
-
Drag the labels onto the flowchart to indicate how the body uses food in cellular respiration. View Available Hint(s) ? After food is broken down in the digestive system, it is transported 10 cels...
-
Indicate how important each characteristic is to you. Answer according to your feelings about the most recent job you had or about the job you currently hold. Circle the number on the scale that...
-
In the local community hub where you work as a community service worker, your organization also provides a FV crisis service. Mandy says her husband married her because she comes from a wealthy...
-
How do trophic interactions and energy flow regulate the biodiversity and functioning of ecosystems ? Explain
-
A traveling wave's equation is given by the equation y ( x , t ) = 3 cos ( 4 . 5 x - 6 . 3 t ) What is the amplitude and wavelength of the wave?
-
As the enrollment at SFA declines, apartment complex owners and landlords in Nacogdoches find that they have higher vacancy rates. A consultant suggests that landlords should raise their rents to...
-
2. The costs associated with producing chemically treated vinyl rollers and fiber-impregnated rubber rollers are estimated. Determine which of the two types should be selected from ROR analysis....
-
Using the following table, identify the key benefits for each the 3 pest control strategies by outlining the risks associated with their respective use. and how to evaluate the results? strategy Risk...
-
Refer to the information from Exercise 22-19. Use the information to determine the (1) Weighted average contribution margin , (2) Break-even point in units, and (3) Number of units of each product...
-
Which of the following must every method have? a. A parameter list b. A header c. A return value d. All of the above
-
A control break field __________. a. Always is output prior to any group of records on a control break report b. Always is output after any group of records on a control break report c. Never is...
-
Explain why each of the following names does or does not seem like a good variable name to you.
-
The power spent in suction and exhaust strokes are known as: (a) Brake horse power (b) Indicated horse power (c) Pumping power (d) None of the above
-
The difference of total power produced and pumping power is known as: (a) Brake horse power (b) Indicated horse power (c) Net indicated horse power (d) None of the above
-
In an Otto cycle, air at 1 bar and \(300 \mathrm{~K}\) is compressed isentropically until the pressure rises to 16 bar. The heat is added at constant volume until the pressure rises to 30 bar....
Study smarter with the SolutionInn App