Question: package Exercise2; public class DoublyLinkedList { class Node { int data; Node prev; Node next; public Node(int data) { this.data = data; this.prev = null;

package Exercise2;

public class DoublyLinkedList {

class Node {

int data;

Node prev;

Node next;

public Node(int data) {

this.data = data;

this.prev = null;

this.next = null;

}

}

Node head;

Node tail;

public void addNode(int data) {

Node newNode = new Node(data);

if (head == null) {

head = newNode;

tail = newNode;

} else {

tail.next = newNode;

newNode.prev = tail;

tail = newNode;

}

}

public void concatenate(DoublyLinkedList L2) {

tail.next = L2.head;

L2.head.prev = tail;

tail = L2.tail;

}

public void printList() {

Node current = head;

while (current != null) {

System.out.print(current.data + " ");

current = current.next;

}

System.out.println();

}

public static void main(String[] args) {

DoublyLinkedList L1 = new DoublyLinkedList();

L1.addNode(1);

L1.addNode(2);

L1.addNode(3);

DoublyLinkedList L2 = new DoublyLinkedList();

L2.addNode(4);

L2.addNode(5);

L2.addNode(6);

L1.concatenate(L2);

L1.printList();

}

}

Can you add comments on my code please? Like //This function does this.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!