Posts

Showing posts from January, 2022

all_operation_on_singly_linked_list

  // all basic operation retleted to singly linked list #include <bits/stdc++.h> using namespace std ; class node {     public:     int data;     node * next;     node ( int val ){         data = val ;         next = NULL ;     } }; void insertAtTail ( node * & head , int val ){     node * n = new node ( val );     node * temp = head ;     if ( head == NULL ){         head = n;         return ;     }     while (temp->next != NULL ){         temp = temp->next;     }     temp->next = n; } void display ( node * head ){     node * temp = head ;     while (temp != NULL ){         cout << temp->data << "->" ;         temp = temp->next;     }     cout << "NULL" << endl ; } void insertAtHead ( node * & head , int val ){     node * n = new node ( val );     node * temp = head ;     if (temp == NULL ){         head = n;         return ;     }     n->next = head ;     head = n; } void insertAtAnyw

Binary_Search

  #include <bits/stdc++.h> using namespace std ; // A recursive binary search function. It returns // location of x in given array arr[l..r] is present, // otherwise -1 void sorting ( int arr [], int n ) {     int temp = 0 ;     for ( int i = 0 ; i < n ; i ++ )     {         for ( int j = i + 1 ; j < n ; j ++ )         {             if ( arr [i] > arr [j])             {                 temp = arr [i];                 arr [i] = arr [j];                 arr [j] = temp;             }         }     } } int binarySearch ( int arr [], int l , int r , int x ) {     if ( r >= l ) {         int mid = l + ( r - l ) / 2 ;         // If the element is present at the middle         // itself         if ( arr [mid] == x )             return mid;         // If element is smaller than mid, then         // it can only be present in left subarray         if ( arr [mid] > x )             return binarySearch ( arr , l , mid - 1 , x );         //