Java Program to Implement Sorted Circular Doubly Linked List

«
»
This is a Java Program to implement a Sorted Circular 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 circular doubly linked list each node has two links, one pointing to the next node and one pointing to the previous node and last node’s ‘next link’ points to first node and first node’s ‘previous link points to last 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 Circular 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 Circular 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, end;
  63.     public int size;
  64.  
  65.     /* Constructor */
  66.     public linkedList()
  67.     {
  68.         start = null;
  69.         end = null;
  70.         size = 0;
  71.     }
  72.     /* Function to check if list is empty */
  73.     public boolean isEmpty()
  74.     {
  75.         return start == null;
  76.     }
  77.     /* Function to get size of list */
  78.     public int getSize()
  79.     {
  80.         return size;
  81.     }
  82.     /* Function to insert element */
  83.     public void insert(int val)
  84.     {
  85.         Node nptr = new Node(val, null, null);
  86.         Node tmp, ptr;        
  87.         boolean ins = false;
  88.         if (start == null)
  89.         {            
  90.             nptr.setLinkNext(nptr);
  91.             nptr.setLinkPrev(nptr);
  92.             start = nptr;
  93.             end = start;            
  94.         }
  95.         else if (val <= start.getData())
  96.         {
  97.             nptr.setLinkPrev(end);
  98.             end.setLinkNext(nptr);
  99.             start.setLinkPrev(nptr);
  100.             nptr.setLinkNext(start);
  101.             start = nptr;
  102.         }
  103.         else if (val >= end.getData())
  104.         {
  105.             end.setLinkNext(nptr);
  106.             nptr.setLinkPrev(end);
  107.             nptr.setLinkNext(start);
  108.             start.setLinkPrev(nptr);
  109.             end = nptr;
  110.         }
  111.         else
  112.         {
  113.             tmp = start;
  114.             ptr = start.getLinkNext();
  115.             while (ptr != null)
  116.             {
  117.                 if (val >= tmp.getData() && val <= ptr.getData())
  118.                 {
  119.                     tmp.setLinkNext(nptr);
  120.                     nptr.setLinkPrev(tmp);
  121.                     nptr.setLinkNext(ptr);
  122.                     ptr.setLinkPrev(nptr);
  123.                     ins = true;
  124.                     break;
  125.                 }
  126.                 else
  127.                 {
  128.                     tmp = ptr;
  129.                     ptr = ptr.getLinkNext();
  130.                 }
  131.             }
  132.             if (!ins)
  133.             {
  134.                 tmp.setLinkNext(nptr);
  135.                 nptr.setLinkPrev(tmp);
  136.  
  137.             }
  138.         }
  139.         size++;
  140.     }
  141.     /* Function to delete node at position */
  142.     public void deleteAtPos(int pos)
  143.     {        
  144.         if (pos == 1) 
  145.         {
  146.             if (size == 1)
  147.             {
  148.                 start = null;
  149.                 end = null;
  150.                 size = 0;
  151.                 return; 
  152.             }
  153.             start = start.getLinkNext();
  154.             start.setLinkPrev(end);
  155.             end.setLinkNext(start);
  156.             size--; 
  157.             return ;
  158.         }
  159.         if (pos == size)
  160.         {
  161.             end = end.getLinkPrev();
  162.             end.setLinkNext(start);
  163.             start.setLinkPrev(end);
  164.             size-- ;
  165.         }
  166.         Node ptr = start.getLinkNext();
  167.         for (int i = 2; i <= size; i++)
  168.         {
  169.             if (i == pos)
  170.             {
  171.                 Node p = ptr.getLinkPrev();
  172.                 Node n = ptr.getLinkNext();
  173.  
  174.                 p.setLinkNext(n);
  175.                 n.setLinkPrev(p);
  176.                 size-- ;
  177.                 return;
  178.             }
  179.             ptr = ptr.getLinkNext();
  180.         }        
  181.     }    
  182.     /* Function to display status of list */
  183.     public void display()
  184.     {
  185.         System.out.print("Sorted Circular Doubly Linked List = ");
  186.         Node ptr = start;
  187.         if (size == 0) 
  188.         {
  189.             System.out.print("empty\n");
  190.             return;
  191.         }
  192.         if (start.getLinkNext() == start) 
  193.         {
  194.             System.out.print(start.getData()+ " <-> "+ptr.getData()+ "\n");
  195.             return;
  196.         }
  197.         System.out.print(start.getData()+ " <-> ");
  198.         ptr = start.getLinkNext();
  199.         while (ptr.getLinkNext() != start) 
  200.         {
  201.             System.out.print(ptr.getData()+ " <-> ");
  202.             ptr = ptr.getLinkNext();
  203.         }
  204.         System.out.print(ptr.getData()+ " <-> ");
  205.         ptr = ptr.getLinkNext();
  206.         System.out.print(ptr.getData()+ "\n");
  207.     }
  208. }
  209.  
  210. /* Class SortedCircularDoublyLinkedList */
  211. public class SortedCircularDoublyLinkedList
  212. {
  213.     public static void main(String[] args)
  214.     {             
  215.         Scanner scan = new Scanner(System.in);
  216.         linkedList list = new linkedList(); 
  217.         System.out.println("Sorted Circular Doubly Linked List Test\n");          
  218.         char ch;
  219.         /*  Perform list operations  */
  220.         do
  221.         {
  222.             System.out.println("\nSorted Circular Doubly Linked List Operations\n");
  223.             System.out.println("1. insert");
  224.             System.out.println("2. delete at position");
  225.             System.out.println("3. check empty");
  226.             System.out.println("4. get size");
  227.  
  228.             int choice = scan.nextInt();            
  229.             switch (choice)
  230.             {
  231.             case 1 : 
  232.                 System.out.println("Enter integer element to insert");
  233.                 list.insert( scan.nextInt() );                     
  234.                 break;                          
  235.             case 2 : 
  236.                 System.out.println("Enter position");
  237.                 int p = scan.nextInt() ;
  238.                 if (p < 1 || p > list.getSize() )
  239.                     System.out.println("Invalid position\n");
  240.                 else
  241.                     list.deleteAtPos(p);
  242.                 break;
  243.             case 3 : 
  244.                 System.out.println("Empty status = "+ list.isEmpty()+"\n");
  245.                 break;                   
  246.             case 4 : 
  247.                 System.out.println("Size = "+ list.getSize() +" \n");
  248.                 break;                         
  249.             default : 
  250.                 System.out.println("Wrong Entry \n ");
  251.                 break;   
  252.             }
  253.             /*  Display List  */ 
  254.             list.display();
  255.             System.out.println("\nDo you want to continue (Type y or n) \n");
  256.             ch = scan.next().charAt(0);            
  257.  
  258.         } while (ch == 'Y'|| ch == 'y');               
  259.     }
  260. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn