In: Computer Science
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
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:
***************************************************************************************************************
I hope this helps you. Good luck.
Edit:
Screenshots of the code:
Screenshot 1:
Screenshot 2:
Screenshot 3:
Screenshot 4:
Screenshot 5: