Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please do part B only in Java 23. Given the following class definition and a linked list with four values (10,20,30,40): class Node ( int
Please do part B only in Java
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Below is the Java code for the add method that adds an integer x to the sorted ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started