Posts

Showing posts from November, 2021

nested map and imp ques

  #include <bits/stdc++.h> using namespace std ; #define shailesh ios :: sync_with_stdio ( false ); cin. tie ( 0 ); cout. tie ( 0 ); #define ll long long int #define vi vector < int > #define pii pair < int , int > #define vii vector < pii > #define rep ( i , a , b ) for ( int i = a; i < b; ++ i) #define ff first #define ss second int main (){     shailesh         map < pair < string , string > , vector < int >> m;     int n;    cin >> n;     rep (i, 0 , n){         string fn, ln;         int ct;        cin >> fn >> ln >> ct;         rep (j, 0 , ct){             int x;            cin >> x;            m [ {fn, ln} ] . push_back (x);        }    }     for ( auto & pr: m){         auto & full_name = pr.first;         auto & list = pr.second;        cout << full_name.first << " " << full_name.second << endl ;        cout << list. size (

vector of vector again stl

  #include <bits/stdc++.h> using namespace std ; #define shailesh ios :: sync_with_stdio ( false ); cin. tie ( 0 ); cout. tie ( 0 ); #define ll long long int #define vi vector < int > #define pii pair < int , int > #define vii vector < pii > #define rep ( i , a , b ) for ( int i = a; i < b; i ++ ) #define ff first #define ss second void printVec ( vector < int > & v ){     cout << "size : " << v . size () << " " << endl ;     for ( int i = 0 ; i < v . size (); i ++ )     {         cout << v [ i ] << " " ;     }     cout << endl ;     } int main (){     shailesh     vector < vector < int >> v;     int N;    cin >> N;     for ( int i = 0 ; i < N; i ++ )    {         int n;        cin >> n;         vector < int > temp;         for ( int j = 0 ; j < n; j ++ )        {             int x;            cin >

vector of vector(stl)

  #include <bits/stdc++.h> using namespace std ; #define shailesh ios :: sync_with_stdio ( false ); cin. tie ( 0 ); cout. tie ( 0 ); #define ll long long int #define vi vector < int > #define pii pair < int , int > #define vii vector < pii > #define rep ( i , a , b ) for ( int i = a; i < b; i ++ ) #define ff first #define ss second void printVec ( vector < int > & v ){     cout << "size : " << v . size () << " " << endl ;     for ( int i = 0 ; i < v . size (); i ++ )     {         cout << v [ i ] << " " ;     }     cout << endl ;     } int main (){     shailesh     int N;    cin >> N;     vector < int > v[N];     for ( int i = 0 ; i < N; i ++ )    {         int n;        cin >> n;         for ( int j = 0 ; j < n; j ++ )        {             int x;            cin >> x;            v[i]. push_back (x);        }

linked list imp and rep , traversing, searching , insertion and deletion

#include <iostream> using namespace std ; // searching and traversing in linked list class node {     public:     int data;     node * next;     node ( int val ){         data = val ;         next = NULL ;     } }; // void deleteAtHead(node* &head, int val){ //     node* n=new node(val); //     n->next=head; //     head=n; // } // void insertElement(node* &head, int val){ // } void deleteAtHead ( node * & head ){         node * todelete = head ;     head = head ->next;     delete todelete;     } void deleteElement ( node * & head , int val ){     if ( head == NULL ){         return ;     }     node * temp = head ;     if ( head ->next == NULL ){         deleteAtHead ( head );         return ;     }     while (temp->next->data != val ){         temp = temp->next;     }     node * todelete = temp->next;     temp->next = temp->next->next;     delete todelete; } void insertElement ( node * & head , int val , int pr

Doubly linked list

  #include <iostream> using namespace std ; class node {     public:     int data;     node * next;     node * prev;     node ( int val ){         data = val ;         next = NULL ;         prev = NULL ;     } }; void deleteAtHead ( node * & head ){     node * todelete = head ;     head = head ->next;     head ->prev = NULL ;     delete todelete; } void deletion ( node * & head , int pos ){     if ( pos == 1 ){         deleteAtHead ( head );         return ;     }     node * temp = head ;     int count = 1 ;     while (temp != NULL && count != pos ){         temp = temp->next;         count ++ ;     }     temp->prev->next = temp->next;     if (temp->next != NULL ){         temp->next->prev = temp->prev;     }     delete temp; } void insertAtHead ( node * & head , int val ){     node * n = new node ( val );     n->next = head ;     if ( head != NULL ){         head ->prev = n;     }     head = n; } void

priority_queue

  #include <iostream> #include <queue> #include <vector> using namespace std ; int main (){     // priority_queue<int> q;     // priority_queue<int, vector<int>, greater<int>> q;     auto cmp = []( int a , int b ){         return a < b ;     };     priority_queue < int , vector < int > , decltype (cmp) > q(cmp);         vector < int > v = { 8 , 1 , 6 , 4 , 0 , 7 , 2 , 9 };     for ( int x: v) q. push (x);     while ( ! q. empty ()){         cout << q. top () << " " ;         q. pop ();     }     cout << endl ;     return 0 ; }

Circular queue

  #include <iostream> using namespace std ; #define n 20 class queue {     int * arr;     int front;     int back;     public:     queue (){         arr = new int [ n ];         front =- 1 ;         back =- 1 ;     }     bool isFull (){         if ((back + 1 ) % n == front){ return true ;         }         else {             return false ;         }                             }     void push ( int x ){         if ( isFull ()){             cout << "queue is overflow " << endl ;             return ;         }         back = (back + 1 ) % n ;         arr[back] = x ;         if (front ==- 1 ){             front ++ ;         }     }     int pop (){         int x = 0 ;         if (front ==- 1 && front > back){             cout << "no element in the queue " << endl ;             return - 1 ;         }         else if (back == front){             x = arr[back];             back =- 1 ;             front =- 1 ;