# C++ Program to Perform Searching Using Self-Organizing Lists

C++Server Side ProgrammingProgramming

Self-Organizing list basically updates the list of given range of items on the basis of last searched item. In this method, the sequential searching approach is used. This algorithm shifts the more important data to the beginning of the list. The time complexity of this search technique is O(n).

## Algorithm

Begin
Function FibonacciSearch().
Calculate the mid value using ‘start+fib[index-2]’ expression.
If the chosen item is equal to the value at mid index, print result and return to main.
If it is lesser than the value at mid index, proceed with the left sub-array.
If it is more than the value at mid index, proceed with the right sub-array.
If the calculated mid value is equal to either start or end then the item is not found in the array.
End

## Example Code

#include<iostream>
using namespace std;
struct node {
int d;
node *next;
};
node* CreateNode(int d) {
node *newnode = new node;
newnode->d = d;
newnode->next = NULL;
return newnode;
}
node* InsertIntoList(node *head, int d) {
node *temp = CreateNode(d);
node *t = new node;
} else {
while(t->next != NULL)
t = t->next;
t->next = temp;
}
}
node *temp = new node;
cout<<"\n The list state is :";
while(temp->next != NULL) {
cout<<"->"<<temp->d;
temp = temp->next;
}
}
node* SearchItem(node *head, int item) {
int flag = 0;
node *temp = new node;
node *t = new node;
if(temp->d == item) {
flag = 5;
} else {
while((temp->next)->next != NULL) {
if((temp->next)->d == item) {
cout<<"\nItem found";
flag = 5;
break;
}
temp = temp->next;
}
t = (temp->next)->next;
temp->next = t;
if(flag == 5)
else
}
}
int main() {
int i, n;
char ch;
for(i = 1; i < 20; i++)
up:
cout<<"\nEnter the Element to be searched: ";
cin>>n;
cout<<"\n\n\tDo you want to search more...enter choice(y/n)?";
cin>>ch;
if(ch == 'y' || ch == 'Y')
goto up;
return 0;
}

## Output

The list state is :->1->2->3->4->5->6->7->8->9->10->11->12->13->14->15->16->17->18
Enter the Element to be searched: 7
Item found
The list state is :->7->1->2->3->4->5->6->8->9->10->11->12->13->14->15->16->17->18
Do you want to search more...enter choice(y/n)?y
Enter the Element to be searched: 20
Do you want to search more...enter choice(y/n)?n