Question

USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...

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).

Homework Answers

Answer #1

***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

------------------------------------------------------------------------------------------------------------------------------------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write C language code for a function that takes a doubly linked list as a parameter...
Write C language code for a function that takes a doubly linked list as a parameter and deletes all the nodes in even positions from the first to the last after displaying the content of each node to the console.
1. Create a linked list using the Node class that contains the first million prime numbers...
1. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options: 1 = Search for a Number (let the user enter a number to search for) 2 = Add a new Number (let the user enter a number and add it to the head of the list) 3 = Delete a Number (let the user enter a number and delete it if found) 4 = Exit...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to be implemented by you. Write a menu driven program that implements the following doubly linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
1. Build a double linked list with 5 in the first node following the instructions: Insert...
1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node with 3 Delete the node with 10 3. Print the resulting list forward...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...