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
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...
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...
Create a student record management system With JAVA using linked list and queue using Java language...
Create a student record management system With JAVA using linked list and queue using Java language and (oracle or any) database to save files and GUI Java swing to create background The program will have the following properties: A. Register students ( assume each students has ID, first name, last name and middle name) B. Register students with courses ( course no ,course title chr ) C. Able to maintain grade on which course they are registered D. Searches students...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. NOT USE your language's Library List . You will receive zero points. Write a LinkedList class. Include...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT