Java Program to Implement Circular Singly Linked List

«
»
This is a Java Program to implement a 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 circular singly linked list each node has only one link which points to the next node in the list and the last node of the list points to the first element of the list.

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

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
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 | Youtube | Instagram | Facebook | Twitter