Java Program to Implement Sorted Circularly Singly Linked List

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

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

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.