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.
-
The engine hoist is used to support the 200-kg engine. Determine the force acting in the hydraulic cylinder AB, the horizontal and vertical components of force at the pin C, and the reactions at the...
-
Smith, the lessee, signs an 8-year lease agreement of a floor of a building on December 31, 2020 that requires annual payments of \(\$ 70,000\), beginning immediately. The residual value is...
-
Flowers Associates is evaluating the performance of three divisions: Daisies, Pansies, and Tulips. Using the data that follow, compute the return on investment and residual income for each division,...
-
1. Cost of preferred stock: Determine the cost for the following preferred stock The cost of preferred stock is ___________%?(Round to two decimal? places.) 2. Cost of common stock equity?? R...
-
As a regional sales manager, Nora Burke travels frequently and relies on her cell phone to keep up to date with clients. She has tried three different service providers, Airway, Bellular, and CyCom,...
-
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...
-
Several factors are involved in the creation of a confidence interval. Among them are the sample size, the level of confidence, and the margin of error. Which statements are true? a) For a given...
-
Journalizing credit sales Show the general journal entries for the following sale transactions for Martindale Company from June 2009. Sales tax equals 8% of sales price. June 2 Sold merchandise on...
-
Employer's payroll obligation Match each of the following terms A through H with the appropriate definitions 1 through 8. A. EFTPS B. Form 941 C. Merit rating D. FICA taxes E. Form 940 F. Form W-2 G....
-
Computing employer taxes An employee earned \($62,500\) during the year working for an employer. The FICA tax for Social Security is 6.2% and the FICA tax for Medicare is 1.45%. The current FUTA tax...
-
Record employer payroll taxes Major Co. has five employees, each of whom earns \($2,500\) per month and has been employed since January 1. FICA Social Security taxes are 6.2% of the first \($94,200\)...
-
Computing and reporting unemployment taxes Ideal Systems employees had the following earnings records at the close of the first quarter: The state unemployment tax is 5.4%, but Ideal Systems pays...
-
The following data are available for a product manufactured and sold by Logan Company: Maximum Capacity with present facilities: ...........40,000 units Total fixed cost per...
-
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 manufacturer of microwave ovens exports its products to another country at a price lower than the cost of production in the home country in order to quickly gain market share. This practice known...
-
[ads] Meghan is creating an ad campaign targeting college students. She is deciding between two school newspapers to display her ad. She wants to use the most efficient media vehicle. The Alligator...
-
Question 29 As per our discussion in Lecture, the total percentage of exports that Canada trades with the United States is? Question 29 options: 65% 70% 76% 78% 80% Question 30 A key issue in trade...
Study smarter with the SolutionInn App