Reimplement the bag class from Figure 4.17 so that the items of the bag are stored with
Question:
Reimplement the bag class from Figure 4.17 so that the items of the bag are stored with a new technique. Here’s the idea: Each node of the noefw linked list contains two integers. The first integer is called the count, and the second integer is called the data. As an example, if a node has a count of 6 and data of 10, then this means that the bag has six copies of the number 10.
The nodes of the linked list should be kept in order from the smallest data (at the head of the list) to the largest (at the tail of the list). You should never have two different nodes with the same data, and if the count in a node drops to zero (meaning there are no copies of that node’s data), then the node should be removed from the linked list.
The public member functions of your new class should be identical to those in Figure 4.17.
Figure 4.17
// FILE: IntLinkedBag.java from the package edu.colorado.collections // Documentation is available in Figure 4.12 on page 216 or from the IntLinkedBag link at // http://www.cs.colorado.edu/-main/docs/. package edu.colorado.collections; import edu.colorado.nodes.IntNode; public class IntLinkedBag implements Cloneable { // INVARIANT for the Bag ADT: // 1. The elements in the Bag are stored in a linked list. // 2. The head reference of the list is in the instance variable head. // 3. The total number of elements in the list is in the instance variable manyNodes. private IntNode head; private int manyNodes; public IntlinkedBag( ) { head = null; manyNodes = 0; public void add(int element) { head = new IntNode (element, head); manyNodes++; } public void addA11 (IntlinkedBag addend) IntNode[ ] copyInfo; if (addend - null) throw new IllegalArgumentException("addend is null."); if (addend.manyNodes > 0) { copyInfo = IntNode.listCopyWithTail(addend.head); copyInfo[1].setLink (head); // Link the tail of the copy to my own head.. head = copyInfo[0]; manyNodes += addend.manyNodes; // and set my own head to the head of the copy.
Step by Step Answer:
Here is the reimplemented version of the bag class based on the description provided include include ...View the full answer
Students also viewed these Computer science questions
-
Suppose that you want to implement a bag class to hold non-negative integers, and you know that the biggest number in the bag will never be more than a few thousand. One approach for implementing...
-
A Chunklist is like a regular linked list, except each node contains a little fixed size array of elements instead of just a single element. Each node also contains its own "size" int to know how...
-
A number of health organizations, including the World Health Organization (WHO), have recommended consumers . . . to reduce daily fat intake below 30 per cent of total calories; . . . to limit intake...
-
Generics, Inc. is a U. S. GAAP reporter that manufactures and sells generic drugs and has a December 31 year-end. On March 1, 2014, it began selling a drug, Anocyn, which is a generic of Dicital....
-
Data Set 2 in Appendix B: 22 body temperatures for supine position Refer to the data set in Appendix B and find the standard deviation.
-
Prepare a worksheet for Moore Co. from the following trial balance and additional data. Additional Data 1. Inventory physical count $13 2. Store supplies on hand 4 3. Depreciation on store equipment...
-
To the right of z = 0.46 Find the indicated area under the standard normal curve. If convenient, use technology to find the area.
-
At the end of the current year, the accounts receivable account of Glenns Nursery Supplies has a debit balance of $390,000. Credit sales are $2,800,000. Record the end-of-period adjusting entry on...
-
Choose one of the four basic leader behavior styles suggested by Hersey and Blanchard. Discuss the specifics of the task and relationship behaviors of the chosen style. Provide a realistic example of...
-
Derive an expression for the thermal resistance through a hollow spherical shell of inside radius ri and outside radius ro having a thermal conductivity k.
-
Use a circular linked list to run a simple simulation of a card game called Crazy 8s. You can use any online rules that you find. The circular linked list is to hold the players (who sit in a circle...
-
Implement a generic class for a sequence of Java objects. You can store the objects in an array (as in Section 3.3) or in a linked list (as in Section 4.5). The class should also implement the...
-
Hoobastink Mfg. is considering a rights offer. The company has determined that the ex-rights price would be 52. The current price is 55 per share, and there are 5 million shares outstanding. The...
-
How is the key consumer benefit employed in an IMC campaign?
-
What are the five visual options available to art directors? How is each suited to varied mediums?
-
What strengths does public relations have when used as a marketing tool?
-
How can television be successfully integrated into the media mix?
-
Why is radio considered handicapped when compared to other media vehicles within the promotional mix?
-
Paul has the following long-term capital gains and losses for 2016: $62,000 28% gain, $21,000 28% loss, $18,000 25% gain, and $64,000 0%/15%/20% gain. He also has a $53,000 short-term loss and a...
-
Why are stocks usually more risky than bonds?
-
Why do we want to use BST?
-
Write a note on linear probing and chaining without replacement?
-
Discuss collision resolution techniques with examples.
-
A bank can either invest money for three months at 4.00% or for nine months at 4.50%. Ignoring actual/360 day count adjustments for the purpose of this question, the three against nine FRA quote the...
-
Shuggy Otis, an executive at Slapfish Corp. (SC) intends to retire in 11 years. SC just announced that it will start depositing $500.00 at the end of each quarter into each of its workers' retirement...
-
On January 1, the Hanover Beverage Company replaced the palletizing machine on one of its juice lines. The cost of the machine was $195,000. The machine's expected life is five years or 480,000...
Study smarter with the SolutionInn App