You are on page 1of 7

1.

Implementation of simple search and binary search using linked list


// C++ implementation of simple search using linked list.

#include <iostream>

using namespace std;

// Node structure for the linked list

struct Node {

int data;

Node* next;

};

// Function to search for an element in the linked list

bool search(Node* head, int key) {

Node* current = head;

while (current != nullptr) {

if (current->data == key) {

return true; // Element found

current = current->next;

return false; // Element not found

// Function to insert a new node at the beginning of the linked list

void insert(Node** head, int item) {

Node* newNode = new Node;

newNode->data = item;

newNode->next = *head;

*head = newNode;

}
// Function to display the linked list

void display(Node* head) {

Node* current = head;

while (current != nullptr) {

cout << current->data << " ";

current = current->next;

cout << endl;

int main() {

Node* head = nullptr;

// Insert elements into the linked list

insert(&head, 10);

insert(&head, 20);

insert(&head, 30);

insert(&head, 40);

insert(&head, 50);

// Display the linked list

cout<<"performing simple search on the linked list below"<<endl;

cout << "Linked List: ";

display(head);

// Search for elements in the linked list

cout<<"search for element 30 in the linked list"<<endl;

int key1 = 30;


if (search(head, key1)) {

cout << key1 << " is found in the linked list." << endl;

} else {

cout << key1 << " is not found in the linked list." << endl;

cout<<"search for element 60 in the linked list"<<

endl;

int key2 = 60;

if (search(head, key2)) {

cout << key2 << " is found in the linked list." << endl;

} else {

cout << key2 << " is not found in the linked list." << endl;

return 0;

c++ implementation of binary search using linked list

#include <iostream>

using namespace std;

// Node structure for the linked list

struct Node {

int data;

Node* next;

};

// Function to search for an element in the linked list using binary search

bool binarySearch(Node* head, int key) {

Node* start = head;


Node* end = nullptr;

// Get the length of the linked list

int length = 0;

while (start != nullptr) {

length++;

end = start;

start = start->next;

// Perform binary search

start = head;

int low = 0;

int high = length - 1;

while (low <= high) {

int mid = low + (high - low) / 2;

// Move the start pointer to the middle node

for (int i = 0; i < mid; i++) {

start = start->next;

if (start->data == key) {

return true; // Element found

} else if (start->data < key) {

low = mid + 1; // Search the right half

} else {

high = mid - 1; // Search the left half


}

start = head; // Reset the start pointer

return false; // Element not found

// Function to insert a new node at the beginning of the linked list

void insert(Node** head, int item) {

Node* newNode = new Node;

newNode->data = item;

newNode->next = *head;

*head = newNode;

// Function to display the linked list

void display(Node* head) {

Node* current = head;

while (current != nullptr) {

cout << current->data << " ";

current = current->next;

cout << endl;

int main() {
Node* head = nullptr;

// Insert elements into the linked list (assuming a sorted list)

insert(&head, 10);

insert(&head, 20);

insert(&head, 30);

insert(&head, 40);

insert(&head, 50);

cout<<"performing binary search on the linked list"<<endl;

// Display the linked list

cout << "Linked List: ";

display(head);

// Perform binary search on the linked list

cout<<"search for element 30 in the linked list above"<<endl;

int key1 = 30;

if (binarySearch(head, key1)) {

cout << key1 << " is found in the linked list." << endl;

} else {

cout << key1 << " is not found in the linked list." << endl;

cout<<"search for element 60 in the linked list above"<<endl;

int key2 = 60;

if (binarySearch(head, key2)) {

cout << key2 << " is found in the linked list." << endl;

} else {

cout << key2 << " is not found in the linked list." << endl;

return 0;
}

You might also like