char ith datal tox (int3 Write your solution to this question on page 7. As best
ID: 3593619 • Letter: C
Question
char ith datal tox (int3 Write your solution to this question on page 7. As best you can, for partial credit, show all work 27. (10 points) The SortedDoubly LinkedList linked list data stracture is a doubly linked list with the added property that the values in the list are kept in sorted order. The value at the front of the list (position 0) is currently the smallest value in the list and the value at the end of the list (position size0-1) is currenaity the largest value in the list ive Write the implementation of a new method called Sorted Add for the SortedDoublyLinkedList class public void Sortedadd (AnyType newvalue) The method Sorted Add adds the newValue into the list in its appropriate sorted position within the sortesdl doubly linked list. Below is the part of the Sorted DoublytLinkedL ist class which you use for this question. You can' t use any other methods of the SortedDoublyLinkedList class. lic class SortedDoublylinkedList cAnytypes implements List private AnyType data; private NodecAnyType> prev private Node p, Node cAnyType» n) setData(d) setPrev (p) setNext (n): public AnyType getData) return data: public void setData (AnyType d) (data d: ) public Node getPrev() return prev public void setPrev (Node cAnyType> p) prev-p: public NodecAnyType> getNext) return next: public void setNext (NodecAnyType> n) next ) private int modCount private Node header private NodecAnyType> trailer Write your solution to this question on page 8. As best you can, for partial credit, shew all workExplanation / Answer
As per the question, the doubly link list is already sorted and it needs to add a new value to the sorted list.
Let us consider the example- values are 3,5,7,9,12
This is the values in a sorted manner for the doubly link list adding a new value 8 in the linked list and hence the new list becomes as 3,5,7,8,9,12
Code adding New method
public void SortedAdd(int data, Node head) {
Node newvalue = new Node();
newvalue.data=data;
//checking the value if the first position 0 is null then directly adding new value in the list
if(head == null)
System.out.println(newvalue);
// checking the value for new data and 1st position value
if(data<head.data){
newvalue.next=head;
head.prev=newvalue;
System.out.println(newvalue);
}
// searching the position where the new value will be added
Node prev=head;
for(;prev.next!=null;prev=prev.next){
if(prev.next.data>data)
break;
}
//adding the node after the prev node
newvalue.next=prev.next;
if(prev.next!=null){
prev.next.prev = newvalue;
}
prev.next=newvalue;
newvalue.prev=prev;
System.out.println(newvalue);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.