C++ Programming Code Examples
C++ > Sorting Searching Code Examples
C++ Program to Perform Searching Using Self-Organizing Lists
/* C++ Program to Perform Searching Using Self-Organizing Lists
- Self-Organizing list updates on the basis of last searched item.
- The sequential searching approach is used.
- In general search, 80% time only specific 20% of data is accessed.
- This algorithm shifts the more important data to the beginning of the list.
- The time complexity of search is O(n).
- Create a linked list with the data element.
- Search the element in the list.
- If element found, delete it from the list and add at the beginning of the list.
- Exit.
C++ program to compare Binary and Sequential Search. */
#include<iostream>
using namespace std;
// A structure to representing a node.
struct node
{
int data;
node *next;
};
// A function to create a new node.
node* CreateNode(int data)
{
node *newnode = new node;
newnode->data = data;
newnode->next = NULL;
return newnode;
}
// A function to add the data at the end of the list.
node* InsertIntoList(node *head, int data)
{
node *temp = CreateNode(data);
node *t = new node;
t = head;
if(head == NULL)
{
head = temp;
return head;
}
else
{
// Traversing to the end of the list.
while(t->next != NULL)
t = t->next;
// Add the node at the end.
t->next = temp;
}
return head;
}
void Display(node *head)
{
node *temp = new node;
temp = head;
cout<<"\n The list state is :";
// Display the list.
while(temp->next != NULL)
{
cout<<"->"<<temp->data;
temp = temp->next;
}
}
node* SearchItem(node *head, int item)
{
int flag = 0;
node *temp = new node;
node *t = new node;
temp = head;
if(temp->data == item)
{
cout<<"\nItem found at head node";
flag = 5;
Display(head);
return head;
}
else
{
while((temp->next)->next != NULL)
{
if((temp->next)->data == item)
{
cout<<"\n item found";
flag = 5;
break;
}
temp = temp->next;
}
// Organising the list.
// Shifting the searched node to the head.
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 < 11; i++)
head = InsertIntoList(head, i);
Display(head);
up:
cout<<"\nEnter the Element to be searched: ";
cin>>n;
// Update the head of the list.
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;
}
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.
In C++, goto is a jump statement and sometimes also referred as unconditional jump statement. It can be used to jump from goto to a labeled statement within the same function. The target label must be within the same file and context. Please note that the use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making hard to understand and modify the program.
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc. Relational Operators are also called Comparison Operators.
Allocate storage space. Default allocation functions (single-object form). A new operator is used to create the object while a delete operator is used to delete the object. When the object is created by using the new operator, then the object will exist until we explicitly use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is not related to the block structure of the program.
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
The "goto statement" is used for transferring the control of a program to a given label. The syntax of goto statement looks like this: In a C++ program, we have any "number of goto"
Two dimensional array in C++, represented in the form of rows and columns, also suitable with matrix. For multi dimensional array, the element with indices i,j would have "address"