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
-
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?
-
When a client does not adequately segregate duties, the possibility of cash being stolen before it is recorded is increased. If the auditor suspects that this type of defalcation is possible, what...
-
Boxes A and B in FIGURE Q6.13 both remain at rest. Is the friction force on A larger than, smaller than, or equal to the friction force on B? Explain. FIGURE Q6.13:
-
On April 1, 2006, US Ultracom issued 7\%, 10-year bonds payable with maturity value of \(\$ 400,000\). The bonds pay interest on March 31 and September 30, and US Ultracom amortizes premium and...
-
Mark Ventura has just purchased an annuity to begin payment at the end of 2016 (that is the date of the first payment). Assume it is now the beginning of the year 2014. The annuity is for $8,000 per...
-
Describe briefly all the basic types of retirement plans and taxation of them.
-
Which of the following is a positive reason for learning chemistry? (a) Chemistry is relevant to daily life (b) Chemistry offers career opportunities (c) Chemistry studies interesting topics (d)...
-
On the moon, the acceleration due to gravity is 1 . 6 3 meters per second . Suppose you are standing on the moon and see a rock falling. At the instant you see it , the rock is 3 0 meters above the...
-
How much the bonds will sell for and prepare the journal entries. Effective / market rate 1 1 % Contract rate 1 2 % Descartes Alarms Co . issued $ 1 0 , 0 0 0 , 0 0 0 of a five year, 1 2 % bonds with...
-
Bargain Surplus made cash sales during the month of October of $ 3 7 3 , 6 5 0 . The sales are subject to a 6 % sales tax that was also collected and included in the summary. How much is the Sales...
-
Beth decides to save $ 2 5 0 every month for the next five years so she can start a small baking business in her home. She is investing the money and can earn 6 % on her investment. How much will...
-
In 2 0 2 0 Sam and Janet are both 6 7 years old and file a joint tax return. What will be their credit for the elderly if their AGI is $ 1 6 , 9 0 0 ?
-
Joe Barker is the production manager of Auto Parts Company ( APC ) and has been asked to prepare 2 0 2 0 ' s ending finished goods budget for the S 2 2 2 product. Based on historical figures, Joe...
-
Decision-making skills assessment Your company offers an online promotion for a one-month free trial of Acomba accounting Software. A customer has one remaining day prior the end of their trial, and...
-
A fast-food restaurant averages 150 customers per hour. The average processing time per customer is 90 seconds. a. Determine how many cash registers the restaurant should have if it wishes to...
-
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...
-
Consider a bivariate time series \(\boldsymbol{z}_{t}\), where \(z_{1 t}\) is the change in monthly U.S. treasury bills with maturity 3 months and \(z_{2 t}\) is the inflation rate, in percentage, of...
-
Consider the growth rates, in percentages, of the quarterly real GDP of United Kingdom, Canada, and the United States used in the chapter. Fit a VAR(4) model to the series, simplify the model by...
-
Consider the U.S. quarterly gross private saving (GPSAVE) and gross private domestic investment (GPDI) from first quarter of 1947 to the third quarter of 2012. The data are from the Federal Reserve...
Study smarter with the SolutionInn App