Question

In: Computer Science

Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...

Exercise 2:
Write a program in Java to manipulate a Double Linked List:

1. Create Double Linked List
2. Display the list
3. Count the number of nodes
4. Insert a new node at the beginning of a Double Linked List.
5. Insert a new node at the end of a DoubleLinked List
6. Insert a new node after the value 5 of Double Linked List
7. Delete the node with value 6.
8. Search an existing element in a Double linked list (the element of search is given by the user)
9. Call all methods above in main method with the following data:

Test Data :
Input the number of nodes : 4
Input data for node 1 : 5
Input data for node 2 : 6
Input data for node 3 : 7

Input data for node 4 : 9

Solutions

Expert Solution

Here is you full complete java code to manipulate Doubly Linked List:

*****************************************************************************************

import java.util.Scanner;

class node
{
   int data;
   node next;
   node prev;
   node(int x)
   {
       data = x;
   }
  
}


public class DoublyLinkedList {
   node head=null;
   void insert_end(node newnode)
   {
       if(head==null)
       {
           head= newnode;
           head.next=null;
           head.prev=null;
       }
       else
       {
           node temp = head;
           node prev = head;
           while(temp!=null)
           {
               prev= temp;
               temp=temp.next;
           }
           prev.next = newnode;
           newnode.prev = prev;
           newnode.next = null;
       }
   }
   void insert_begin(node newnode)
   {
       newnode.next = head;
       head.prev= newnode;
       newnode.prev=null;
       head = newnode;
      
   }
   void insert_after(node newnode,int after)
   {
       node temp = head;
       node prev = head;
       while(prev.data != after && temp!=null)
       {
           prev= temp;
           temp=temp.next;
       }
       prev.next = newnode;
       newnode.prev = prev;
       newnode.next = temp;
       if(temp!=null)
       temp.prev =newnode;
   }
   void display()
   {
       System.out.println();
       System.out.println("**********LIST IS********");
       node temp = head;
       while(temp!=null)
       {
           System.out.print(temp.data+"->");
           temp=temp.next;
       }
       System.out.println("end");
       System.out.println();
   }
   int get_length()
   {
       int count=0;
       node temp = head;
       while(temp!=null)
       {
           count++;
           temp=temp.next;
       }
       return count;
   }
   void search(int search_this)
   {
       node temp = head;
       int i=0;
       while(temp!=null)
       {
           if(temp.data == search_this)
           System.out.println(temp.data+" at "+(i)+" index");
          
           temp = temp.next;
           i++;
       }
   }
   void delete(int delete_this)
   {
       node temp = head;
       node prev= head;
       int i=0;
       if(temp.data == delete_this)
       {
           temp.next.prev = null;
           head = temp.next;
           return;
       }
      
       while(temp!=null)
       {
          
           if(temp.data == delete_this)
           {
               prev.next = temp.next;
               temp.next.prev = prev;
               return;
           }
           prev= temp;
           temp= temp.next;
           i++;
       }
       System.out.println("Node not found");
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       Scanner cin = new Scanner(System.in);
       System.out.println("Enter the number of nodes");
       int n = cin.nextInt();
       int x,i=0;
       DoublyLinkedList list = new DoublyLinkedList();
       while(i!=n)
       {
           System.out.println("Enter data for "+(i+1)+" node: ");
           x = cin.nextInt();
           node newnode = new node(x);
           list.insert_end(newnode);
          
          
          
           i++;
       }
       list.display();
       System.out.println("Length of the list is: "+ list.get_length());
      
       System.out.println("Enter data to insert at begin: ");
       x = cin.nextInt();
       node newnode = new node(x);
       list.insert_begin(newnode);
       list.display();
      
       System.out.println("Enter data to insert in between: ");
       x= cin.nextInt();
       newnode = new node(x);
       System.out.println("after which data? : ");
       int after = cin.nextInt();
       list.insert_after(newnode,after);
       list.display();
      
       System.out.println("-----now deleting node with data 6----");
       list.delete(6);
       list.display();
      
       System.out.println("Enter data to search for: ");
       int search_this = cin.nextInt();
       list.search(search_this);
   }

}
*************************************************************************************************

Output:

Screenshot 1:

Screenshot 2:

*************************************************************************************************************

Note:

  1. The above program has two classes one Node which defines the structure or properties of a node of doubly linked list and the other class is DoublyLinkedList which has head pointer as a member and other utility functions which manages the doubly linked list.
  2. Main() of the class DoublyLinkedList is the driver for the above code which includes all the test data mentioned in the problem plus additional test data added by me to verify other functions/operations on doubly linked list.
  3. Searching of an element gives you output position of an element on basis of 0- indexing.  

***************************************************************************************************************

I hope this helps you. Good luck.

Edit:

Screenshots of the code:

Screenshot 1:

Screenshot 2:

Screenshot 3:

Screenshot 4:

Screenshot 5:


Related Solutions

Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program 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 options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT