Self Organizing List : Count Method
Last Updated :
09 Dec, 2023
Self Organizing list is a list that re-organizes or re-arranges itself for better performance. In a simple list, an item to be searched is looked for in a sequential manner which gives the time complexity of O(n). But in real scenario not all the items are searched frequently and most of the time only few items are searched multiple times.Â
So, a self organizing list uses this property (also known as locality of reference) that brings the most frequent used items at the head of the list. This increases the probability of finding the item at the start of the list and those elements which are rarely used are pushed to the back of the list.Â
In Count Method, the number of time each node is searched for is counted (i.e. the frequency of search is maintained). So an extra storage is associated with each node that is incremented every time a node is searched. And then the nodes are arranged in non-increasing order of count or frequency of its searches. So this ensures that the most frequently accessed node is kept at the head of the list.Â
Examples:
Input : list : 1, 2, 3, 4, 5
searched : 4
Output : list : 4, 1, 2, 3, 5
Input : list : 4, 1, 2, 3, 5
searched : 5
searched : 5
searched : 2
Output : list : 5, 2, 4, 1, 3
Explanation : 5 is searched 2 times (i.e. the
most searched) 2 is searched 1 time and 4 is
also searched 1 time (but since 2 is searched
recently, it is kept ahead of 4) rest are not
searched, so they maintained order in which
they were inserted.
Implementation:
CPP
#include <iostream>
using namespace std;
struct self_list {
int value;
int count;
struct self_list* next;
};
self_list *head = NULL, *rear = NULL;
void insert_self_list( int number)
{
self_list* temp = (self_list*) malloc ( sizeof (self_list));
temp->value = number;
temp->count = 0;
temp->next = NULL;
if (head == NULL)
head = rear = temp;
else {
rear->next = temp;
rear = temp;
}
}
bool search_self_list( int key)
{
self_list* current = head;
self_list* prev = NULL;
while (current != NULL) {
if (current->value == key) {
current->count = current->count + 1;
if (current != head) {
self_list* temp = head;
self_list* temp_prev = NULL;
while (current->count < temp->count) {
temp_prev = temp;
temp = temp->next;
}
if (current != temp) {
prev->next = current->next;
current->next = temp;
if (temp == head)
head = current;
else
temp_prev->next = current;
}
}
return true ;
}
prev = current;
current = current->next;
}
return false ;
}
void display()
{
if (head == NULL) {
cout << "List is empty" << endl;
return ;
}
self_list* temp = head;
cout << "List: " ;
while (temp != NULL) {
cout << temp->value << "(" << temp->count << ")" ;
if (temp->next != NULL)
cout << " --> " ;
temp = temp->next;
}
cout << endl
<< endl;
}
int main()
{
insert_self_list(1);
insert_self_list(2);
insert_self_list(3);
insert_self_list(4);
insert_self_list(5);
display();
search_self_list(4);
search_self_list(2);
display();
search_self_list(4);
search_self_list(4);
search_self_list(5);
display();
search_self_list(5);
search_self_list(2);
search_self_list(2);
search_self_list(2);
display();
return 0;
}
|
Java
import java.util.*;
public class SelfList {
static class Node {
int value;
int count;
Node next;
Node( int value) {
this .value = value;
this .count = 0 ;
this .next = null ;
}
}
static Node head = null ;
static Node rear = null ;
static void insertSelfList( int number) {
Node temp = new Node(number);
if (head == null )
head = rear = temp;
else {
rear.next = temp;
rear = temp;
}
}
static boolean searchSelfList( int key) {
Node current = head;
Node prev = null ;
while (current != null ) {
if (current.value == key) {
current.count++;
if (current != head) {
Node temp = head;
Node tempPrev = null ;
while (current.count < temp.count) {
tempPrev = temp;
temp = temp.next;
}
if (current != temp) {
prev.next = current.next;
current.next = temp;
if (temp == head)
head = current;
else
tempPrev.next = current;
}
}
return true ;
}
prev = current;
current = current.next;
}
return false ;
}
static void display() {
if (head == null ) {
System.out.println( "List is empty" );
return ;
}
Node temp = head;
System.out.print( "List: " );
while (temp != null ) {
System.out.print(temp.value + "(" + temp.count + ")" );
if (temp.next != null )
System.out.print( " --> " );
temp = temp.next;
}
System.out.println( "\n" );
}
public static void main(String[] args) {
insertSelfList( 1 );
insertSelfList( 2 );
insertSelfList( 3 );
insertSelfList( 4 );
insertSelfList( 5 );
display();
searchSelfList( 4 );
searchSelfList( 2 );
display();
searchSelfList( 4 );
searchSelfList( 4 );
searchSelfList( 5 );
display();
searchSelfList( 5 );
searchSelfList( 2 );
searchSelfList( 2 );
searchSelfList( 2 );
display();
}
}
|
Python3
class self_organize_list( object ):
def __init__( self ):
self .__list = list ()
self .__size = 0
def __init__( self , lst):
self .__list = [ [x, 0 ] for x in lst ]
self .__size = len (lst)
def display( self ):
print ( self .__list)
def search( self , key):
index = - 1
for i in range ( self .__size):
if self .__list[i][ 0 ] = = key:
index = i
break
if index = = - 1 :
return False
self .__list[index][ 1 ] = self .__list[index][ 1 ] + 1
new_pos = index
for i in range (index - 1 , - 1 , - 1 ):
if self .__list[index][ 1 ] > = self .__list[i][ 1 ]:
new_pos = i
bkp = self .__list[index]
self .__list.pop(index)
self .__list.insert(new_pos, bkp)
return True
def insert( self , key):
self .__list.append([key, 0 ])
self .__size = self .__size + 1
if __name__ = = "__main__" :
lst = [ 1 , 2 , 3 , 4 ]
sol = self_organize_list(lst)
sol.insert( 5 )
sol.display()
sol.search( 4 )
sol.search( 2 )
sol.display()
sol.search( 4 )
sol.search( 4 )
sol.search( 5 )
sol.display()
sol.search( 5 )
sol.search( 2 )
sol.search( 2 )
sol.search( 2 )
sol.display()
|
C#
using System;
public class SelfList
{
public int Value { get ; set ; }
public int Count { get ; set ; }
public SelfList Next { get ; set ; }
}
class Program
{
static SelfList head = null , rear = null ;
static void InsertSelfList( int number)
{
SelfList temp = new SelfList
{
Value = number,
Count = 0,
Next = null
};
if (head == null )
{
head = rear = temp;
}
else
{
rear.Next = temp;
rear = temp;
}
}
static bool SearchSelfList( int key)
{
SelfList current = head;
SelfList prev = null ;
while (current != null )
{
if (current.Value == key)
{
current.Count++;
if (current != head)
{
SelfList temp = head;
SelfList tempPrev = null ;
while (current.Count < temp.Count)
{
tempPrev = temp;
temp = temp.Next;
}
if (current != temp)
{
prev.Next = current.Next;
current.Next = temp;
if (temp == head)
head = current;
else
tempPrev.Next = current;
}
}
return true ;
}
prev = current;
current = current.Next;
}
return false ;
}
static void Display()
{
if (head == null )
{
Console.WriteLine( "List is empty" );
return ;
}
SelfList temp = head;
Console.Write( "List: " );
while (temp != null )
{
Console.Write($ "{temp.Value}({temp.Count})" );
if (temp.Next != null )
Console.Write( " --> " );
temp = temp.Next;
}
Console.WriteLine( "\n" );
}
static void Main()
{
InsertSelfList(1);
InsertSelfList(2);
InsertSelfList(3);
InsertSelfList(4);
InsertSelfList(5);
Display();
SearchSelfList(4);
SearchSelfList(2);
Display();
SearchSelfList(4);
SearchSelfList(4);
SearchSelfList(5);
Display();
SearchSelfList(5);
SearchSelfList(2);
SearchSelfList(2);
SearchSelfList(2);
Display();
}
}
|
Javascript
class SelfList {
constructor(value) {
this .value = value;
this .count = 0;
this .next = null ;
}
}
let head = null ;
let rear = null ;
function insertSelfList(number) {
const temp = new SelfList(number);
if (head === null )
head = rear = temp;
else {
rear.next = temp;
rear = temp;
}
}
function searchSelfList(key) {
let current = head;
let prev = null ;
while (current !== null ) {
if (current.value === key) {
current.count++;
if (current !== head) {
let temp = head;
let tempPrev = null ;
while (current.count < temp.count) {
tempPrev = temp;
temp = temp.next;
}
if (current !== temp) {
prev.next = current.next;
current.next = temp;
if (temp === head)
head = current;
else
tempPrev.next = current;
}
}
return true ;
}
prev = current;
current = current.next;
}
return false ;
}
function display() {
if (head === null ) {
console.log( "List is empty" );
return ;
}
let temp = head;
let result = "List: " ;
while (temp !== null ) {
result += `${temp.value}(${temp.count})`;
if (temp.next !== null )
result += " --> " ;
temp = temp.next;
}
console.log(result + "\n" );
}
( function main() {
insertSelfList(1);
insertSelfList(2);
insertSelfList(3);
insertSelfList(4);
insertSelfList(5);
display();
searchSelfList(4);
searchSelfList(2);
display();
searchSelfList(4);
searchSelfList(4);
searchSelfList(5);
display();
searchSelfList(5);
searchSelfList(2);
searchSelfList(2);
searchSelfList(2);
display();
})();
|
Output
List: 1(0) --> 2(0) --> 3(0) --> 4(0) --> 5(0)
List: 2(1) --> 4(1) --> 1(0) --> 3(0) --> 5(0)
List: 4(3) --> 5(1) --> 2(1) --> 1(0) --> 3(0)
List: 2(4) --> 4(3) --> 5(2) --> 1(0) --> 3(0)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...