Java Program to Implement Singly Linked List

This is a Java Program to implement a 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 singly linked list each node has only one link which points to the next node in the list.

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

Singly Linked List Test
 
 
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
 
Singly Linked List = empty
 
Do you want to continue (Type y or n)
 
y
 
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
5
 
Singly Linked List = 5
 
Do you want to continue (Type y or n)
 
y
 
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
7
 
Singly Linked List = 7->5
 
Do you want to continue (Type y or n)
 
y
 
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
4
 
Singly Linked List = 7->5->4
 
Do you want to continue (Type y or n)
 
y
 
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
2
 
Singly Linked List = 7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
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
9
 
Singly Linked List = 9->7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
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
3
Enter position
3
 
Singly Linked List = 9->7->3->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
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
2
Enter position
2
 
Singly Linked List = 9->2->7->3->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
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 = 7
 
 
Singly Linked List = 9->2->7->3->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
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
4
 
Singly Linked List = 9->2->7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
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
 
Singly Linked List = 9->7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
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
 
Singly Linked List = 7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
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
 
Singly Linked List = 7->5->2
 
Do you want to continue (Type y or n)
 
y
 
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
 
Singly Linked List = 5->2
 
Do you want to continue (Type y or n)
 
y
 
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
 
Singly Linked List = 5
 
Do you want to continue (Type y or n)
 
y
 
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
 
Singly Linked List = empty
 
Do you want to continue (Type y or n)
 
y
 
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
 
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.

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.