USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function.
Create a menu that contains the following operations :
1. Add new node to DLL. ( as a METHOD )
2. Delete a node from DLL. ( as a METHOD )
3. Show how many nodes in DLL. ( as a METHOD )
4. Print all data in the DLL. ( as a METHOD )
5. Exit.
Important notes :
1. Write the operations from the pseudocode.
2. The user should insert all data even in the first node.
3. The menu should be repeated each time until the user 5 (Exit).
***For deleting node function I have implemented delete of first node as in your it was not mentioned.
***If you want to modify it you can comment so that I can update it.
***And If it works please like it. It takes lots oof effort to coding Question.
-------------------------------------------------------------------------------------------------------------------------------
Code:
import java.util.*;
public class Main {
Node head;
class Node {
int data;
Node prev;
Node next;
Node(int d) { data = d; }
}
public void
push(int nd)
{
Node nn = new Node(nd);
nn.next = head;
nn.prev = null;
if (head != null)
head.prev =
nn;
head = nn;
}
void deleteNode(Node dd)
{
if (head == null || dd == null) {
return;
}
if (head == dd) {
head = dd.next;
}
if (dd.next != null) {
dd.next.prev = dd.prev;
}
if (dd.prev != null) {
dd.prev.next = dd.next;
}
return;
}
public void
printlist(Node nn)
{
System.out.println("Elements in DLL
: ");
while (nn != null) {
System.out.print(nn.data + " ");
nn =
nn.next;
}
System.out.println(" ");
}
public int countlist(Node nn)
{
int c = 0;
while (nn != null) {
c++;
nn =
nn.next;
}
return c;
}
public static void main(String[] args)
{
Main dll = new Main();
boolean flag = true;
while(flag){
int x;
System.out.println("1. Add new node
to DLL.\n2. Delete a node from DLL.\n3. Show how many nodes in
DLL.\n4. Print all data in the DLL.\n5. Exit.");
Scanner sc = new
Scanner(System.in);
x = sc.nextInt();
switch(x)
{
case 1:
System.out.println("Enter the data
to be inserted: ");
int y1 = sc.nextInt();
dll.push(y1);
System.out.println("Data
inserted!");
System.out.println(" ");
break;
case 2:
dll.deleteNode(dll.head);
System.out.println("Data
Deleted!");
System.out.println(" ");
break;
case 3:
System.out.println("Number of nodes
in DLL: " + dll.countlist(dll.head));
System.out.println(" ");
break;
case 4:
System.out.println("Created DLL is:
");
dll.printlist(dll.head);
System.out.println(" ");
break;
case 5:
flag = false;
break;
default:
System.out.println("Wrong
Input!");
System.out.println(" ");
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------------
Output screenshots and Code screenshots
------------------------------------------------------------------------------------------------------------------------------------
Get Answers For Free
Most questions answered within 1 hours.