Java Program to Implement Self Organising List

This is a Java Program to implement Self Organizing List. A self-organizing list is a list that reorders its elements based on some self-organizing heuristic to improve average access time. The aim of a self-organizing list is to improve efficiency of linear search by moving more frequently accessed items towards the head of the list. A self-organizing list achieves near constant time for element access in the best case. A self-organizing list uses a reorganizing algorithm to adapt to various query distributions at runtime. The following program uses Count Method to reorder the list. In this technique, the number of times each node was searched for is counted i.e. every node keeps a separate counter variable which is incremented every time it is called. The nodes are then rearranged according to decreasing count. Thus, the nodes of highest count i.e. most frequently accessed are kept at the head of the list. The primary advantage of this technique is that it generally is more realistic in representing the actual access pattern.

Here is the source code of the Java program to implement Self Organizing 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 Self organizing List
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. /* Class SelfOrganizingList */
  8. class SelfOrganizingList
  9. {
  10.     private int[] list;
  11.     private int[] count;
  12.     private int size;
  13.  
  14.     /* Constructor */
  15.     public SelfOrganizingList(int listSize)
  16.     {
  17.         list = new int[listSize];
  18.         count = new int[listSize];
  19.         size = 0;        
  20.     }
  21.     /* Function to check is empty */
  22.     public boolean isEmpty()
  23.     {
  24.         return size == 0;
  25.     }
  26.     /* Function to check if full */
  27.     public boolean isFull()
  28.     {
  29.         return size == list.length;
  30.     }
  31.     /* Function to make list empty */
  32.     public void makeEmpty()
  33.     {
  34.         int l = list.length;
  35.         list = new int[l];
  36.         count = new int[l];
  37.         size = 0;        
  38.     }
  39.     /* Function to get size of list */
  40.     public int getSize()
  41.     {
  42.         return size;
  43.     }
  44.     /* Function to insert element */
  45.     public void insert(int val)
  46.     {
  47.         if (isFull() )
  48.         {
  49.             System.out.println("Error : List full!");
  50.             return;
  51.         }
  52.         list[size] = val;
  53.         count[size] = 0;
  54.         size++;
  55.     }
  56.     /* Function to remove element */
  57.     public void remove(int pos)
  58.     {
  59.         pos-- ;
  60.         if (pos < 0 || pos >= size )
  61.         {
  62.             System.out.println("Invalid position ");
  63.             return;
  64.         }
  65.         for (int i = pos; i < size - 1; i++)
  66.         {
  67.             list[i] = list[i + 1];
  68.             count[i] = count[i + 1];
  69.         }
  70.         size--;        
  71.     }
  72.     /* Function to search for an element */
  73.     public boolean search(int x)
  74.     {
  75.         boolean searchResult = false;
  76.         int pos = -1;
  77.         for (int i = 0; i < size; i++)
  78.         {
  79.             if (list[i] == x)
  80.             {
  81.                 searchResult = true;
  82.                 pos = i;
  83.                 break;
  84.             }
  85.         }
  86.         if (searchResult)
  87.         {
  88.             count[pos]++;
  89.             int c = count[pos];
  90.             for (int i = 0; i < pos; i++)
  91.             {
  92.                 if (count[pos] > count[i])
  93.                 {
  94.                     for (int j = pos; j > i; j--)
  95.                     {
  96.                         list[j] = list[j - 1];
  97.                         count[j] = count[j - 1];
  98.                     }
  99.                     list[i] = x;
  100.                     count[i] = c;
  101.                     break;
  102.                 }
  103.             }
  104.         }
  105.         return searchResult;        
  106.     }
  107.     /* Function to print list */
  108.     public void printList()
  109.     {
  110.         System.out.print("\nList = ");
  111.         for (int i = 0; i < size; i++)
  112.             System.out.print(list[i] +" ");
  113.         System.out.print("\nCount = ");
  114.         for (int i = 0; i < size; i++)
  115.             System.out.print(count[i] +" ");
  116.     }
  117. }
  118.  
  119. /*  Class SelfOrganizingListTest  */
  120. public class SelfOrganizingListTest
  121. {    
  122.     public static void main(String[] args)
  123.     {             
  124.         Scanner scan = new Scanner(System.in);
  125.         System.out.println("SelfOrganizingList Test\n"); 
  126.  
  127.         /* Creating object of class SelfOrganizingList */
  128.         System.out.println("Enter size of list ");
  129.         SelfOrganizingList list = new SelfOrganizingList(scan.nextInt() ); 
  130.  
  131.         char ch;
  132.         /*  Perform list operations  */
  133.         do    
  134.         {
  135.             System.out.println("\nSelfOrganizingList Operations\n");
  136.             System.out.println("1. insert ");
  137.             System.out.println("2. delete at position");
  138.             System.out.println("3. search");
  139.             System.out.println("4. check empty");
  140.             System.out.println("5. check full"); 
  141.             System.out.println("6. get size");            
  142.             int choice = scan.nextInt();            
  143.             switch (choice)
  144.             {
  145.             case 1 : 
  146.                 System.out.println("Enter integer element to insert");
  147.                 list.insert( scan.nextInt() );                     
  148.                 break;                          
  149.             case 2 : 
  150.                 System.out.println("Enter position to delete");
  151.                 list.remove(scan.nextInt() );                     
  152.                 break;                         
  153.             case 3 : 
  154.                 System.out.println("Enter integer element to search");
  155.                 System.out.println("Search Result : "+ list.search(scan.nextInt() ));
  156.                 break;                                          
  157.             case 4 : 
  158.                 System.out.println("Empty status = "+ list.isEmpty());
  159.                 break;     
  160.             case 5 : 
  161.                 System.out.println("Full status = "+ list.isFull());
  162.                 break;                     
  163.             case 6 :  
  164.                 System.out.println("Size = "+ list.getSize() +" \n");
  165.                 break;                         
  166.             default : 
  167.                 System.out.println("Wrong Entry \n ");
  168.                 break;    
  169.             }
  170.             /*  Display List  */  
  171.             list.printList();
  172.  
  173.             System.out.println("\nDo you want to continue (Type y or n) \n");
  174.             ch = scan.next().charAt(0);                        
  175.         } while (ch == 'Y'|| ch == 'y');               
  176.     }
  177. }

SelfOrganizingList Test
 
Enter size of list
5
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
1
Enter integer element to insert
7
 
List  = 7
Count = 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
1
Enter integer element to insert
3
 
List  = 7 3
Count = 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
1
Enter integer element to insert
1
 
List  = 7 3 1
Count = 0 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
1
Enter integer element to insert
9
 
List  = 7 3 1 9
Count = 0 0 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
1
Enter integer element to insert
6
 
List  = 7 3 1 9 6
Count = 0 0 0 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
3
Enter integer element to search
6
Search Result : true
 
List  = 6 7 3 1 9
Count = 1 0 0 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
3
Enter integer element to search
9
Search Result : true
 
List  = 6 9 7 3 1
Count = 1 1 0 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
3
Enter integer element to search
1
Search Result : true
 
List  = 6 9 1 7 3
Count = 1 1 1 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
3
Enter integer element to search
1
Search Result : true
 
List  = 1 6 9 7 3
Count = 2 1 1 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
3
Enter integer element to search
9
Search Result : true
 
List  = 1 9 6 7 3
Count = 2 2 1 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
3
Enter integer element to search
6
Search Result : true
 
List  = 1 9 6 7 3
Count = 2 2 2 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
3
Enter integer element to search
6
Search Result : true
 
List  = 6 1 9 7 3
Count = 3 2 2 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
2
Enter position to delete
3
 
List  = 6 1 7 3
Count = 3 2 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
4
Empty status = false
 
List  = 6 1 7 3
Count = 3 2 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
1
Enter integer element to insert
7
 
List  = 6 1 7 3 7
Count = 3 2 0 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
5
Full status = true
 
List  = 6 1 7 3 7
Count = 3 2 0 0 0
Do you want to continue (Type y or n)
 
y
 
SelfOrganizingList Operations
 
1. insert
2. delete at position
3. search
4. check empty
5. check full
6. get size
6
Size = 5
 
 
List  = 6 1 7 3 7
Count = 3 2 0 0 0
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.