Self Organizing List : Move to Front Method

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 Move to Front Method, the recently searched item is moved to the front of the list. So, this method is quite easy to implement but it also moves in-frequent searched items to front. This moving of in-frequent searched items to the front is a big disadvantage of this method because it affects the access time.


Examples:

Input :   list : 1, 2, 3, 4, 5, 6
          searched: 4 
Output :  list : 4, 1, 2, 3, 5, 6

Input :   list : 4, 1, 2, 3, 5, 6
          searched : 2
Output :  list : 2, 4, 1, 3, 5, 6
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to implement self-organizing list
// using move to front method
#include <iostream>
using namespace std;
  
// structure for self organizing list
struct self_list {
    int value;
    struct self_list* next;
};
  
// head and rear pointing to start and end of list resp.
self_list *head = NULL, *rear = NULL;
  
// function to insert an element
void insert_self_list(int number)
{
    // creating a node
    self_list* temp = (self_list*)malloc(sizeof(self_list));
  
    // assigning value to the created node;
    temp->value = number;
    temp->next = NULL;
  
    // first element of list
    if (head == NULL)
        head = rear = temp;
  
    // rest elements of list
    else {
        rear->next = temp;
        rear = temp;
    }
}
  
// function to search the key in list
// and re-arrange self-organizing list
bool search_self_list(int key)
{
    // pointer to current node
    self_list* current = head;
  
    // pointer to previous node
    self_list* prev = NULL;
  
    // searching for the key
    while (current != NULL) {
  
        // if key found
        if (current->value == key) {
  
            // if key is not the first element
            if (prev != NULL) {
  
                /* re-arranging the elements */
                prev->next = current->next;
                current->next = head;
                head = current;
            }
            return true;
        }
        prev = current;
        current = current->next;
    }
  
    // key not found
    return false;
}
  
// function to display the list
void display()
{
    if (head == NULL) {
        cout << "List is empty" << endl;
        return;
    }
  
    // temporary pointer pointing to head
    self_list* temp = head;
    cout << "List: ";
  
    // sequentially displaying nodes
    while (temp != NULL) {
        cout << temp->value;
        if (temp->next != NULL)
            cout << " --> ";
  
        // incrementing node pointer.
        temp = temp->next;
    }
    cout << endl  << endl;
}
  
// Driver Code
int main()
{
    /* inserting five values */
    insert_self_list(1);
    insert_self_list(2);
    insert_self_list(3);
    insert_self_list(4);
    insert_self_list(5);
  
    // Display the list
    display();
  
    // search 4 and if found then re-arrange
    if (search_self_list(4))
        cout << "Searched: 4" << endl;
    else
        cout << "Not Found: 4" << endl;
  
    // Display the list
    display();
  
    // search 2 and if found then re-arrange
    if (search_self_list(2))
        cout << "Searched: 2" << endl;
    else
        cout << "Not Found: 2" << endl;
    display();
  
    return 0;
}

chevron_right


Output:

List: 1 --> 2 --> 3 --> 4 --> 5

Searched: 4
List: 4 --> 1 --> 2 --> 3 --> 5

Searched: 2
List: 2 --> 4 --> 1 --> 3 --> 5

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.