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;}}
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;}}
Comments
Leave a comment