Java Program to Implement Sorted Doubly Linked List

«
»
This is a Java Program to implement a Sorted Doubly Linked List. A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence. In a sorted doubly linked list each node has two links, one pointing to the next node and one pointing to the previous node and insertion of an element into the list is done in a sorted fashion.

Here is the source code of the Java program to implement Sorted Doubly Linked List. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  *  Java Program to Implement Sorted Doubly Linked List
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. /*  Class Node  */
  8. class Node
  9. {
  10.     protected int data;
  11.     protected Node next, prev;
  12.  
  13.     /* Constructor */
  14.     public Node()
  15.     {
  16.         next = null;
  17.         prev = null;
  18.         data = 0;
  19.     }
  20.     /* Constructor */
  21.     public Node(int d, Node n, Node p)
  22.     {
  23.         data = d;
  24.         next = n;
  25.         prev = p;
  26.     }
  27.     /* Function to set link to next node */
  28.     public void setLinkNext(Node n)
  29.     {
  30.         next = n;
  31.     }
  32.     /* Function to set link to previous node */
  33.     public void setLinkPrev(Node p)
  34.     {
  35.         prev = p;
  36.     }    
  37.     /* Funtion to get link to next node */
  38.     public Node getLinkNext()
  39.     {
  40.         return next;
  41.     }
  42.     /* Function to get link to previous node */
  43.     public Node getLinkPrev()
  44.     {
  45.         return prev;
  46.     }
  47.     /* Function to set data to node */
  48.     public void setData(int d)
  49.     {
  50.         data = d;
  51.     }
  52.     /* Function to get data from node */
  53.     public int getData()
  54.     {
  55.         return data;
  56.     }
  57. }
  58.  
  59. /* Class linkedList */
  60. class linkedList
  61. {
  62.     protected Node start;
  63.     public int size;
  64.  
  65.     /* Constructor */
  66.     public linkedList()
  67.     {
  68.         start = null;
  69.         size = 0;
  70.     }
  71.     /* Function to check if list is empty */
  72.     public boolean isEmpty()
  73.     {
  74.         return start == null;
  75.     }
  76.     /* Function to get size of list */
  77.     public int getSize()
  78.     {
  79.         return size;
  80.     }
  81.     /* Function to insert element */
  82.     public void insert(int val)
  83.     {
  84.         Node nptr = new Node(val, null, null);
  85.         Node tmp, ptr;        
  86.         boolean ins = false;
  87.         if(start == null)
  88.             start = nptr;
  89.         else if (val <= start.getData())
  90.         {
  91.             nptr.setLinkNext(start);
  92.             start.setLinkPrev(nptr);
  93.             start = nptr;
  94.         }
  95.         else
  96.         {
  97.             tmp = start;
  98.             ptr = start.getLinkNext();
  99.             while(ptr != null)
  100.             {
  101.                 if(val >= tmp.getData() && val <= ptr.getData())
  102.                 {
  103.                     tmp.setLinkNext(nptr);
  104.                     nptr.setLinkPrev(tmp);
  105.                     nptr.setLinkNext(ptr);
  106.                     ptr.setLinkPrev(nptr);
  107.                     ins = true;
  108.                     break;
  109.                 }
  110.                 else
  111.                 {
  112.                     tmp = ptr;
  113.                     ptr = ptr.getLinkNext();
  114.                 }
  115.             }
  116.             if(!ins)
  117.             {
  118.                 tmp.setLinkNext(nptr);
  119.                 nptr.setLinkPrev(tmp);
  120.  
  121.             }
  122.         }
  123.         size++;
  124.     }
  125.     /* Function to delete node at position */
  126.     public void deleteAtPos(int pos)
  127.     {        
  128.         if (pos == 1) 
  129.         {
  130.             if (size == 1)
  131.             {
  132.                 start = null;
  133.                 size = 0;
  134.                 return; 
  135.             }
  136.             start = start.getLinkNext();
  137.             start.setLinkPrev(null);
  138.             size--; 
  139.             return ;
  140.         }
  141.         if (pos == size) 
  142.         {
  143.             Node ptr = start;
  144.  
  145.             for (int i = 1; i < size - 1; i++)
  146.                 ptr = ptr.getLinkNext();
  147.             ptr.setLinkNext(null);            
  148.             size --;
  149.             return;
  150.         }
  151.         Node ptr = start.getLinkNext();
  152.         for (int i = 2; i <= size; i++)
  153.         {
  154.             if (i == pos)
  155.             {
  156.                 Node p = ptr.getLinkPrev();
  157.                 Node n = ptr.getLinkNext();
  158.  
  159.                 p.setLinkNext(n);
  160.                 n.setLinkPrev(p);
  161.                 size-- ;
  162.                 return;
  163.             }
  164.             ptr = ptr.getLinkNext();
  165.         }        
  166.     }    
  167.     /* Function to display status of list */
  168.     public void display()
  169.     {
  170.         System.out.print("Doubly Linked List = ");
  171.         if (size == 0) 
  172.         {
  173.             System.out.print("empty\n");
  174.             return;
  175.         }
  176.         if (start.getLinkNext() == null) 
  177.         {
  178.             System.out.println(start.getData() );
  179.             return;
  180.         }
  181.         Node ptr = start;
  182.         System.out.print(start.getData()+ " <-> ");
  183.         ptr = start.getLinkNext();
  184.         while (ptr.getLinkNext() != null)
  185.         {
  186.             System.out.print(ptr.getData()+ " <-> ");
  187.             ptr = ptr.getLinkNext();
  188.         }
  189.         System.out.print(ptr.getData()+ "\n");
  190.     }
  191. }
  192.  
  193. /* Class SortedDoublyLinkedList */
  194. public class SortedDoublyLinkedList
  195. {
  196.     public static void main(String[] args)
  197.     {             
  198.         Scanner scan = new Scanner(System.in);
  199.         linkedList list = new linkedList(); 
  200.         System.out.println("Sorted Doubly Linked List Test\n");          
  201.         char ch;
  202.         /*  Perform list operations  */
  203.         do
  204.         {
  205.             System.out.println("\nSorted Doubly Linked List Operations\n");
  206.             System.out.println("1. insert");
  207.             System.out.println("2. delete at position");
  208.             System.out.println("3. check empty");
  209.             System.out.println("4. get size");
  210.  
  211.             int choice = scan.nextInt();            
  212.             switch (choice)
  213.             {
  214.             case 1 : 
  215.                 System.out.println("Enter integer element to insert");
  216.                 list.insert( scan.nextInt() );                     
  217.                 break;                          
  218.             case 2 : 
  219.                 System.out.println("Enter position");
  220.                 int p = scan.nextInt() ;
  221.                 if (p < 1 || p > list.getSize() )
  222.                     System.out.println("Invalid position\n");
  223.                 else
  224.                     list.deleteAtPos(p);
  225.                 break;
  226.             case 3 : 
  227.                 System.out.println("Empty status = "+ list.isEmpty()+"\n");
  228.                 break;                   
  229.             case 4 : 
  230.                 System.out.println("Size = "+ list.getSize() +" \n");
  231.                 break;                         
  232.             default : 
  233.                 System.out.println("Wrong Entry \n ");
  234.                 break;   
  235.             }
  236.             /*  Display List  */ 
  237.             list.display();
  238.             System.out.println("\nDo you want to continue (Type y or n) \n");
  239.             ch = scan.next().charAt(0);            
  240.         } while (ch == 'Y'|| ch == 'y');               
  241.     }    
  242. }

advertisement
Sorted Doubly Linked List Test
 
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
1
Enter integer element to insert
24
Doubly Linked List = 24
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
1
Enter integer element to insert
6
Doubly Linked List = 6 <-> 24
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
1
Enter integer element to insert
19
Doubly Linked List = 6 <-> 19 <-> 24
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
1
Enter integer element to insert
1
Doubly Linked List = 1 <-> 6 <-> 19 <-> 24
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
1
Enter integer element to insert
7
Doubly Linked List = 1 <-> 6 <-> 7 <-> 19 <-> 24
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
1
Enter integer element to insert
94
Doubly Linked List = 1 <-> 6 <-> 7 <-> 19 <-> 24 <-> 94
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
4
Size = 6
 
Doubly Linked List = 1 <-> 6 <-> 7 <-> 19 <-> 24 <-> 94
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
2
Enter position
1
Doubly Linked List = 6 <-> 7 <-> 19 <-> 24 <-> 94
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
2
Enter position
3
Doubly Linked List = 6 <-> 7 <-> 24 <-> 94
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
2
Enter position
4
Doubly Linked List = 6 <-> 7 <-> 24
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
2
Enter position
1
Doubly Linked List = 7 <-> 24
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
2
Enter position
2
Doubly Linked List = 7
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
2
Enter position
1
Doubly Linked List = empty
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
4
Size = 0
 
Doubly Linked List = empty
 
Do you want to continue (Type y or n)
 
y
 
Sorted Doubly Linked List Operations
 
1. insert
2. delete at position
3. check empty
4. get size
3
Empty status = true
 
Doubly Linked List = empty
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.