Answer to Question #269389 in Java | JSP | JSF for Hamza

Question #269389

Correct the following code make it run easily and explain each step that what is happening in the code.Also attach the picture of the code when you code successfully runs...


public class DLL {private Node start;private Node end;private int count;public DLL()

{start = end = null;count = 0;}

public void insertAtStart(Data d)

{Node tn = new Node(d, null, null);

if (count == 0) {start = end = tn;} else {tn.next = start;start.prev = tn;start = tn;}

count++;} 

public void insertAtEnd(Data d) {if (count == 0) {insertAtStart(d);}

else {Node tn = new Node(d, null, end);

end.next = tn;end = tn;count++;}}

public void insertAt(Data d, int index) {if (count == 0 || index == 0) {insertAtStart(d);} else if (index >= count) {insertAtEnd(d);} else {Node temp = start;for (int i = 0; i < index; i++, temp =temp.next);Node tn = new Node(d, temp, temp.prev);

temp.prev.next = tn;temp.prev = tn;}}}

public class Node {Data d;Node next;Node prev;public Node(Data d, Node n, Node p) {this.d = d;next = n;prev = p;}}


1
Expert's answer
2021-11-21T04:51:23-0500
class Data{
    
}
public class Main {
    private Node start;
    private Node end;
    private int count;
    public Main(){
        start = end = null;
        count = 0;
        
    }


    public void insertAtStart(Data d){
        Node tn = new Node(d, null, null);
    
        if (count == 0) {
            start = end = tn;
        } 
        else {
            tn.next = start;start.prev = tn;start = tn;
            
        }
    
    count++;
        
    } 
    
    public void insertAtEnd(Data d) {if (count == 0) {insertAtStart(d);}
    
    else {Node tn = new Node(d, null, end);
    
    end.next = tn;end = tn;count++;}}
    
    public void insertAt(Data d, int index) {if (count == 0 || index == 0) {insertAtStart(d);} else if (index >= count) {insertAtEnd(d);} else {Node temp = start;for (int i = 0; i < index; i++, temp =temp.next);Node tn = new Node(d, temp, temp.prev);
    
    temp.prev.next = tn;temp.prev = tn;}}}
    
    class Node {Data d;Node next;Node prev;public Node(Data d, Node n, Node p) {this.d = d;next = n;prev = p;}}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS