
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Perform Searching Using Self-Organizing Lists
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; t = head; if(head == NULL) { head = temp; return head; } else { while(t->next != NULL) t = t->next; t->next = temp; } return head; } void Display(node *head) { node *temp = new node; temp = head; 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; temp = head; if(temp->d == item) { cout<<"\nItem found at head node"; flag = 5; Display(head); return head; } 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)->next = head; head = temp->next; temp->next = t; if(flag == 5) Display(head); else cout<<"\nItem not found."; } return head; } int main() { int i, n; char ch; node *head = new node; head = NULL; for(i = 1; i < 20; i++) head = InsertIntoList(head, i); Display(head); up: cout<<"\nEnter the Element to be searched: "; cin>>n; head = SearchItem(head, 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 Item not found. Do you want to search more...enter choice(y/n)?n
- Related Articles
- What are Lists and Self-Organizing lists in compiler design?
- C++ Program to Perform Searching Based on Locality of Reference
- How can you perform self join on two tables using MySQL in Python?
- C# program to perform Quick sort using Recursion
- 8085 Program to perform sorting using selection sort
- 8085 Program to perform sorting using bubble sort
- C++ Program to Perform Sorting Using B-Tree
- File Searching using Python
- File Searching using C#
- C++ program to implement Run Length Encoding using Linked Lists
- C++ Program to Perform Addition Operation Using Bitwise Operators
- C++ Program to Perform String Matching Using String Library
- C++ Program to Perform Optimal Paranthesization Using Dynamic Programming
- Python program to find common elements in three lists using sets
- C++ Program to Implement a Binary Search Tree using Linked Lists

Advertisements