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 Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
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)
Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse...
Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse list method in the subclass to reverse the order of the linked list. Print the original linked list and the reverse ordered linked list at the end of program. You can use the gamescore.txt to test the reverse method. _____________________________________________________________________________________________________________________________________________________ /** Source code example for "A Practical Introduction to Data     Structures and Algorithm Analysis, 3rd Edition (Java)"     by Clifford A. Shaffer     Copyright 2008-2011 by...
c++ example of a double linked list of chars I need to create a double linked...
c++ example of a double linked list of chars I need to create a double linked list of chars. this should be a class that allows you to input a single character at a time, list the resulting characters, find any character and delete the first example of a character.
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
**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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT