Data Structures and algorithms: Topic: Doubly and circular Linked Lists in java: Please solve this on urgent basis: Attach output's picture and explain every statement in commments: public class DoublyLinkedList { //Represent a node of the doubly linked list class Node{ int data; Node previous; Node next; public Node(int data) { this.data = data; } } //Represent the head and tail of the doubly linked list Node head, tail = null; //addNode() will add a node to the list public void addNode(int data) { //Create a new node Node newNode = new Node(data); //If list is empty if(head == null) { //Both head and tail will point to newNode head = tail = newNode; //head's previous will point to null head.previous = null; //tail's next will point to null, as it is the last node of the list tail.next = null; } else { //newNode will be added after tail such that tail's next will point to newNode tail.next = newNode; //newNode's previous will point to tail newNode.previous = tail; //newNode will become new tail tail = newNode; //As it is last node, tail's next will point to null tail.next = null; } } //display() will print out the nodes of the list public void display() { //Node current will point to head Node current = head; if(head == null) { System.out.println("List is empty"); return; } System.out.println("Nodes of doubly linked list: "); while(current != null) { //Prints each node by incrementing the pointer. System.out.print(current.data + " "); current = current.next; } } public static void main(String[] args) { DoublyLinkedList dList = new DoublyLinkedList(); //Add nodes to the list dList.addNode(1); dList.addNode(2); dList.addNode(3); dList.addNode(4); dList.addNode(5); //Displays the nodes present in the list dList.display(); } } Using the doubly linked list , implement above and solve following problem voidcalculateProduct(); In this function you should calculate product of every 3rd element and at the end it should return the product For example Linked List contains Data: 9,8,7,6,5,4,3,2,1,11 Output: 28 Explanation: 7*4*1 = 28
Data Structures and
Topic: Doubly and circular Linked Lists in java:
Please solve this on urgent basis:
Attach output's picture and explain every statement in commments:
public class DoublyLinkedList {
//Represent a node of the doubly linked list
class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
}
}
//Represent the head and tail of the doubly linked list
Node head, tail = null;
//addNode() will add a node to the list
public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);
//If list is empty
if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
}
//display() will print out the nodes of the list
public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while(current != null) {
//Prints each node by incrementing the pointer.
System.out.print(current.data + " ");
current = current.next;
}
}
public static void main(String[] args) {
DoublyLinkedList dList = new DoublyLinkedList();
//Add nodes to the list
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);
//Displays the nodes present in the list
dList.display();
}
}
Using the doubly linked list , implement above and solve following problem
voidcalculateProduct();
In this function you should calculate product of every 3rd element and at the end it should return the product
For example
Linked List contains Data: 9,8,7,6,5,4,3,2,1,11
Output: 28
Explanation: 7*4*1 = 28
Step by step
Solved in 3 steps with 1 images