Java Program to Implement Triply Linked List

This is a Java Program to implement Triply Linked List. Triply linked list is a list in which each node has 3 pointers pointing to three other nodes. Here a binary search tree is implemented using a triply linked list.

Here is the source code of the Java program to implement Triply 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 Triply Linked List
  3.  */
  4.  
  5. import java.util.Scanner;    
  6.  
  7. /* class TLLNode */
  8. class TLLNode
  9. {
  10.     TLLNode left, right, middle;
  11.     int data;
  12.  
  13.     /* Constructor */
  14.     public TLLNode(int x)
  15.     {
  16.         data = x;
  17.         left = null;
  18.         right = null;
  19.         middle = null;
  20.     }
  21. }
  22.  
  23. /* class TriplyLinkedList */
  24. class TriplyLinkedList
  25. {
  26.     TLLNode root, tmp;
  27.  
  28.     /* Constructor */
  29.     public TriplyLinkedList()
  30.     {
  31.         root = null;
  32.         tmp = null;
  33.     }
  34.     /* function to check if empty */
  35.     public boolean isEmpty()
  36.     {
  37.         return root == null;
  38.     }
  39.     /* function to clear list */
  40.     public void makeEmpty()
  41.     {
  42.         root = null;
  43.         tmp = null;
  44.     }
  45.     /* function to insert */
  46.     public void insert(int x)
  47.     {
  48.         root = insert(root, x);        
  49.     }
  50.     /* function to insert element */
  51.     public TLLNode insert(TLLNode r, int x)
  52.     {
  53.         if (r == null)
  54.         {    
  55.             r = new TLLNode(x);
  56.             r.middle = tmp;
  57.         }
  58.         else
  59.         {
  60.             tmp = r;
  61.             if (r.data >= x)
  62.                 r.left = insert(r.left, x);
  63.             else
  64.                 r.right = insert(r.right, x);
  65.         }
  66.         return r;
  67.     }    
  68.     /* Function for print */
  69.     public void printList()
  70.     {
  71.         printList(root);
  72.     }
  73.     private void printList(TLLNode r)
  74.     {
  75.         if (r != null)
  76.         {
  77.             printList(r.left);
  78.             System.out.print(r.data +" ");
  79.             printList(r.right);
  80.         }
  81.     }
  82. }
  83.  
  84. /* class TriplyLinkedListTest */
  85. public class TriplyLinkedListTest
  86. {
  87.     public static void main(String[] args) 
  88.     {
  89.         Scanner scan = new Scanner(System.in);
  90.         System.out.println("Triply Linked List Test\n");  
  91.         TriplyLinkedList tll = new TriplyLinkedList();
  92.  
  93.         char ch;
  94.         /*  Perform list operations  */
  95.         do    
  96.         {
  97.             System.out.println("\nTriply Linked List Operations\n");
  98.             System.out.println("1. insert ");
  99.             System.out.println("2. check empty");
  100.             System.out.println("3. make empty"); 
  101.  
  102.             int choice = scan.nextInt();            
  103.             switch (choice)
  104.             {
  105.             case 1 : 
  106.                 System.out.println("Enter integer element to insert");
  107.                 tll.insert( scan.nextInt() );                     
  108.                 break;                          
  109.             case 2 :  
  110.                 System.out.println("Empty status = "+ tll.isEmpty());
  111.                 break;   
  112.             case 3 :  
  113.                 System.out.println("\nList Cleared\n");
  114.                 tll.makeEmpty();
  115.                 break;            
  116.             default : 
  117.                 System.out.println("Wrong Entry \n ");
  118.                 break;   
  119.             }
  120.             /*  Display list */ 
  121.             System.out.print("\nList : ");
  122.             tll.printList();
  123.  
  124.             System.out.println("\nDo you want to continue (Type y or n) \n");
  125.             ch = scan.next().charAt(0);                        
  126.         } while (ch == 'Y'|| ch == 'y');  
  127.     }
  128. }

Triply Linked List Test
 
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
1
Enter integer element to insert
97
 
List : 97
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
1
Enter integer element to insert
24
 
List : 24 97
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
1
Enter integer element to insert
6
 
List : 6 24 97
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
1
Enter integer element to insert
19
 
List : 6 19 24 97
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
1
Enter integer element to insert
94
 
List : 6 19 24 94 97
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
1
Enter integer element to insert
57
 
List : 6 19 24 57 94 97
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
1
Enter integer element to insert
23
 
List : 6 19 23 24 57 94 97
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
2
Empty status = false
 
List : 6 19 23 24 57 94 97
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
3
 
List Cleared
 
 
List :
Do you want to continue (Type y or n)
 
y
 
Triply Linked List Operations
 
1. insert
2. check empty
3. make empty
2
Empty status = true
 
List :
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.