Please do part B only in Java 23. Given the following class definition and a linked list
Fantastic news! We've Found the answer you've been seeking!
Question:
Please do part B only in Java
Transcribed Image Text:
23. Given the following class definition and a linked list with four values (10,20,30,40): class Node ( int value; Node next; public Node(int v, Node n) { this.value v; this.next = n; head 10 LEFEN } 0 (A) (4 points) Show how the linked list referenced by head would be after we perform the following operations. Assume that each operation starts with the initial list as shown above. Length contents 4 (10,20,30,40) الس 20 Example: Initial list head.next null; head.next head.next.next; head new Node (50, null); head new Node (50, head); (B)(8 points) We use the same Node structure to implement a sorted list in which all integers are stored in the ascending order. Complete the Java code below for add(int x), which accepts an integer x and adds it into the list pointed by head while keeping the collection sorted. If x is a duplicated value, x should be inserted right before the first occurrence of that value. Examples: starting from list above, add (25) updates the list to (10,20, 25, 30, 40); starting from list above addres 1103 1030 10-403 110, 30, 403 2 10 20 30 40 80 3 2503 250, 10 50 401 10.10.2010, 403 -3 OUT 040 80 10, 20, 30, 463 (B)(8 points) We use the same Node structure to implement a sorted list in which all integers are stored in the ascending order. Complete the Java code below for add (int x), which accepts an integer x and adds it into the list pointed by head while keeping the collection sorted. If x is a duplicated value, x should be inserted right before the first occurrence of that value. Examples: starting from list above, add(25) updates the list to (10,20,25,30,40); starting from list above, add(29) updates the list to (10,20,20,30,40). 40 Note: You may NOT modify Node class or method signature, you may not import anything, you may not use non-local variables other than head. public void add (int x) { //global variable head is pointing to current list head 23. Given the following class definition and a linked list with four values (10,20,30,40): class Node ( int value; Node next; public Node(int v, Node n) { this.value v; this.next = n; head 10 LEFEN } 0 (A) (4 points) Show how the linked list referenced by head would be after we perform the following operations. Assume that each operation starts with the initial list as shown above. Length contents 4 (10,20,30,40) الس 20 Example: Initial list head.next null; head.next head.next.next; head new Node (50, null); head new Node (50, head); (B)(8 points) We use the same Node structure to implement a sorted list in which all integers are stored in the ascending order. Complete the Java code below for add(int x), which accepts an integer x and adds it into the list pointed by head while keeping the collection sorted. If x is a duplicated value, x should be inserted right before the first occurrence of that value. Examples: starting from list above, add (25) updates the list to (10,20, 25, 30, 40); starting from list above addres 1103 1030 10-403 110, 30, 403 2 10 20 30 40 80 3 2503 250, 10 50 401 10.10.2010, 403 -3 OUT 040 80 10, 20, 30, 463 (B)(8 points) We use the same Node structure to implement a sorted list in which all integers are stored in the ascending order. Complete the Java code below for add (int x), which accepts an integer x and adds it into the list pointed by head while keeping the collection sorted. If x is a duplicated value, x should be inserted right before the first occurrence of that value. Examples: starting from list above, add(25) updates the list to (10,20,25,30,40); starting from list above, add(29) updates the list to (10,20,20,30,40). 40 Note: You may NOT modify Node class or method signature, you may not import anything, you may not use non-local variables other than head. public void add (int x) { //global variable head is pointing to current list head
Expert Answer:
Answer rating: 100% (QA)
Below is the Java code for the add method that adds an integer x to the sorted ... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Lissed 1) You have the following data: 12.4, 21.6, 13.8, 19.1, 17.2 Calculate the following: 3) b) n yi c) (y-y) i=1 i=1 n d) (y,-y) i=1 2) Suppose we have the following measurements for the numbers...
-
sin(2x) sin x cos(2x) = sec x COS X
-
answer the question clearly You are building a flight-control system for which a convincing safety case must be made. Would you assign the tasks of safety requirements engineering, test case...
-
What is the wavelength of light if its frequency is 1.009 106 Hz?
-
Repeat Exercise 11.4.14 when the spline has horns clamped boundary conditions.
-
Jackson Company adopts acceptable accounting for its defined benefit pension plan on January 1, 2024, with the following beginning balances: plan assets $200,000; projected benefit obligation...
-
A countercurrent extraction system extracts furfural from water into methyl-isobutyl ketone (MIBK) at \(25^{\circ} \mathrm{C}\). Aqueous feed contains 0.0011 wt. fraction furfural and has a feed rate...
-
Magnetic-Optical Corporation offers a variety of share-based compensation plans to employees. Under its restricted stock award plan, the company on January 1, 2011, granted 4 million of its $1 par...
-
The following information pertains to Thornton Manufacturing Company for March Year 3. Assume actual overhead equaled applied overhead. March 1 Inventory balances Raw materials Work in process...
-
3 Consider the following possible alternatives to the vector norms we encountered in the notes. For each, state whether the proposed definition satisfies the conditions of a norm or not. If it does,...
-
Jason Bradley decided to buy a home for $271,000. His bank requires a 25% down payment. has notified Jason that besides the 25% down payment there will be the following additional costs: Recording of...
-
Explain why expertise in security, data management, and automation is important for an organization to make the best use of cloud services.
-
Rangel Corporation issued $520,000 of 5%, 15-year bonds payable on March 31, 2022. The market interest rate at the date of issuance was 8%, and the bonds pay interest semiannually. Rangel...
-
State and explain one of the characteristics of resilience from Rodin's five characteristics of resilience that must be addressed by the country to achieve disaster resilience in the future
-
What are the potential implications of quantum computing on the future of fintech, particularly in areas such as cryptography, risk modeling, and algorithmic trading, and how are financial...
-
Black Bird Corp. is a US firm that is considering investing in a subsidiary in Canada. The subsidiary will generate 45M CAD per year forever. The spot exchange rate is 1.1 CAD/$. The current...
-
Extend the Smoluchowski model for diffusion into a reactive sphere of radius a, by assuming that the reaction rate at the spherical surface is proportional to concentration of incoming solute at the...
-
A sprinkler head malfunctions at midfield in an NFL football field. The puddle of water forms a circular pattern around the sprinkler head with a radius in yards that grows as a function of time, in...
-
John Fuji (age 37) moved from California to Washington in December 2011. He lives at 468 Cameo Street, Yakima, WA 98901. John's Social Security number is 571-78-5974 and he is single. His earnings...
-
Carl and Jenny adopt a Russian orphan. The adoption takes 2 years and two trips to Russia and is final in 2012. They pay $6,000 in 2011 and $7,500 in 2012 of qualified adoption expenses, and have AGI...
-
Ken (age 31) and Amy (age 28) Booth have brought you the following information regarding their income, expenses, and withholding for the year. They are unsure which of these items must be used to...
-
Figure 7.13 displays two groups of data points, given in Table 7.8. The convex hulls have also been plotted. It is possible to separate the two classes of points via a straight line. In fact, many...
-
In Example 4.12, we applied a principal component analysis (PCA) to the iris data, but refrained from classifying the flowers based on their feature vectors \(\boldsymbol{x}\). Implement a 1-nearest...
-
In Example 7.6 we used the feature map \(\boldsymbol{\phi}(\boldsymbol{x})=\left[x_{1}, x_{2}, x_{1}^{2}+x_{2}^{2} ight]^{\top}\) to classify the points. An easier way is to map the points into...
Study smarter with the SolutionInn App