├── README.md ├── .gitignore ├── Day-10 ├── satyam.txt ├── student.bat ├── 06-File_handling_2.cpp ├── 03-Vector_4.cpp ├── 01-Vector_2.cpp ├── 04-Pair.cpp ├── 05-File_handling_1.cpp └── 02-Vector_3.cpp ├── Slides ├── Lec1.pptx.pdf ├── Lec2.pptx.pdf ├── Lec3.pptx.pdf ├── Lec4.pptx.pdf ├── Lec5.pptx.pdf ├── Lec6.pptx.pdf ├── Lec7.pptx.pdf ├── Lec8.pptx.pdf ├── Lec9.pptx.pdf ├── Lec10 .pptx.pdf ├── Lec11.pptx.pdf ├── Lec12.pptx.pdf └── Lec13.pptx.pdf ├── Day-0 ├── 01-Hello_world.cpp └── intro.txt ├── Day-3 ├── 06-Pointer_practice.cpp ├── 02-Void_pointer.cpp ├── 05-Pointer_to_pointer.cpp ├── 03-Pointer_arithmetic.cpp ├── 04-Pointer_to_array.cpp └── 01-Pointer.cpp ├── Day-2 ├── 06-Recursion1.cpp ├── 05-Recursion.cpp ├── 02-Default_arguments.cpp ├── 07-Power.cpp ├── 04-Call_by.cpp ├── 03-Function_overloading.cpp └── 01-Functions.cpp ├── Day-14 ├── 06-Stack_stl.cpp ├── 04-Multiple_templates.cpp ├── 08-Queue_stl.cpp ├── 01-Functional_templates.cpp ├── 02-Template_func_to_get_sum.cpp ├── 03-Class_template.cpp ├── 05-Stack_using_vector.cpp └── 07-Queue_using_array.cpp ├── Day-1 ├── 05-Loops.cpp ├── 06-Sample_question.cpp ├── 03-Type_cast.cpp ├── 04-Flow_control.cpp ├── 02-Operators.cpp └── 01-Data_types.cpp ├── Day-11 ├── 04-Exception_handling.cpp ├── 01-Exception_handling.cpp ├── 05-Exception_handling.cpp ├── 02-Exception_handling.cpp ├── 03-Exception_handling.cpp ├── 08-Set.cpp ├── 07-Unordered_map.cpp ├── 09-Unordered_set.cpp └── 06-Map.cpp ├── Day-5 ├── 04-Destructors.cpp ├── 03-Copy_constructors.cpp ├── 02-Constructors.cpp └── 01-Class_intro.cpp ├── Day-9 ├── 01-Inheritance.cpp ├── 02-Inheritance_types.cpp ├── 06-Vector_1.cpp ├── 04-Function_overriding.cpp ├── 03-Inheritance_access_modes.cpp └── 05-Vector_implementation.cpp ├── Day-12 ├── 03-Bit_manipulation.cpp ├── 04-Bit_manipulation.cpp ├── 01-Bit_manipulation.cpp ├── 02-Bit_manipulation.cpp └── 05-Binary_tree.cpp ├── Day-8 ├── 03-Practice_string_count_digits.cpp ├── 02-Practice_string_sort.cpp └── 01-linked_list.cpp ├── Day-15 ├── 04-Weighted_graph.cpp ├── 01-Valid_parentheses.cpp ├── 03-DFS_graph.cpp └── 02-BFS_Graph.cpp ├── Day-4 ├── 01-Arrays_intro.cpp ├── 03-Move_zeroes.cpp ├── 02-Array_rotate.cpp ├── 04-Matrix_add.cpp └── 05-String.cpp ├── Day-6-7 ├── 03-Searching.cpp ├── 01-Add_Matrices.cpp ├── 02-Complex_number.cpp └── 04-Sorting.cpp └── Day-13 ├── 02-Binary_search_tree.cpp └── 01-Binary_tree_1.cpp /README.md: -------------------------------------------------------------------------------- 1 | # CPP-B2-2022 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | *.exe -------------------------------------------------------------------------------- /Day-10/satyam.txt: -------------------------------------------------------------------------------- 1 | Hello 2 | How are you doing? 3 | I am a Software Developer 4 | -------------------------------------------------------------------------------- /Day-10/student.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Day-10/student.bat -------------------------------------------------------------------------------- /Slides/Lec1.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec1.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec2.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec2.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec3.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec3.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec4.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec4.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec5.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec5.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec6.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec6.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec7.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec7.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec8.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec8.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec9.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec9.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec10 .pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec10 .pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec11.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec11.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec12.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec12.pptx.pdf -------------------------------------------------------------------------------- /Slides/Lec13.pptx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Satyam-Bhalla/CPP-Practice/HEAD/Slides/Lec13.pptx.pdf -------------------------------------------------------------------------------- /Day-0/01-Hello_world.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | cout << "Hello World!" << endl; 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Day-3/06-Pointer_practice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int arr[] = {4,5,6,7}; 6 | int *p = (arr+1); 7 | cout << *p << endl; 8 | cout << *arr +10 << endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Day-2/06-Recursion1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int factorial(int n); 6 | 7 | int main(){ 8 | factorial(5); 9 | } 10 | 11 | int factorial(int n){ 12 | if(n>1){ 13 | return factorial(n-1)*n; 14 | }else{ 15 | return 1; 16 | } 17 | } -------------------------------------------------------------------------------- /Day-2/05-Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void rec(int a){ 6 | // Base condition 7 | if(a==0){ 8 | return; 9 | } 10 | rec(a-1); 11 | cout << a << endl; 12 | 13 | } 14 | 15 | int main(){ 16 | int a = 3; 17 | rec(a); 18 | } -------------------------------------------------------------------------------- /Day-2/02-Default_arguments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int sum(int a=0, int b=0, int c=0 ){ 6 | return a+b+c; 7 | } 8 | 9 | 10 | int main(){ 11 | cout << sum() << endl; 12 | cout << sum(1) << endl; 13 | cout << sum(1,2) << endl; 14 | cout << sum(1,2,3) << endl; 15 | } -------------------------------------------------------------------------------- /Day-3/02-Void_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int x = 10; 6 | double c = 100.36; 7 | 8 | void *ptr; 9 | ptr = &x; 10 | 11 | cout << &x << " " << ptr << endl; 12 | 13 | ptr = &c; 14 | cout << &c << " " << ptr << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Day-14/06-Stack_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | stack s; 8 | 9 | for(int i=0;i<5;i++){ 10 | s.push(i*i); 11 | } 12 | 13 | while(!s.empty()){ 14 | cout << s.top() << endl; 15 | s.pop(); 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Day-3/05-Pointer_to_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int x = 10; 6 | int *y = &x; 7 | int **z = &y; 8 | int ***a = &z; 9 | 10 | cout << x << " " << y << " " << z << " " << a << endl; 11 | cout << *y << " " << **z << " " << ***a << endl; 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Day-14/04-Multiple_templates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | T2 add(T1 a,T2 b){ 7 | return a+b; 8 | } 9 | 10 | int main(){ 11 | cout << add(10,20) << endl; 12 | cout << add(10,20.5) << endl; 13 | cout << add(10.5,20.5) << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Day-14/08-Queue_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | queue q; 8 | int n = 10; 9 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int i; 7 | for(i=0;i<5;i++){ 8 | if(i==3){ 9 | break; 10 | } 11 | cout << i << endl; 12 | } 13 | cout << "************" << endl; 14 | while(i<8){ 15 | cout << i << endl; 16 | i++; 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Day-3/03-Pointer_arithmetic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int x = 10; 6 | int *y = &x; 7 | 8 | cout << y << " -> " << *y << endl; 9 | // ptr + n = ptr + (sizeof(datatype)*n) 10 | y = y+1; 11 | cout << y << " -> " << *y << endl; 12 | y = y-2; 13 | cout << y << " -> " << *y << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Day-11/04-Exception_handling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Base{}; 6 | class Derived : public Base{}; 7 | 8 | int main(){ 9 | try{ 10 | Derived d; 11 | Base b; 12 | throw d; 13 | }catch(Derived d2){ 14 | cout << "Derived class caught" << endl; 15 | }catch(Base b){ 16 | cout << "Base class caught" << endl; 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Day-11/01-Exception_handling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int a = 1000000000; 7 | 8 | try { 9 | int *arr = new int[a*a]; 10 | cout << "It worked!!" << endl; 11 | }catch(bad_alloc e){ 12 | // Catch one exception 13 | cout << "Can't allocate this much memory" << endl; 14 | cout << "The error is " << e.what() << endl; 15 | } 16 | 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Day-5/04-Destructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int i; 5 | class A{ 6 | public: 7 | A(){ 8 | cout << "Constructor called " << endl; 9 | } 10 | ~A(){ 11 | cout << "Destructor called" << endl; 12 | i = 0; 13 | } 14 | 15 | }; 16 | 17 | 18 | int foo(){ 19 | i = 3; 20 | A obj; 21 | return i; 22 | } 23 | 24 | 25 | int main(){ 26 | cout << foo(); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Day-3/04-Pointer_to_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | // Array with 4 elements 6 | int val[4] = {1,2,3,4}; 7 | int *ptr; 8 | ptr = val; 9 | 10 | cout << ptr << endl; // Base address of array 11 | cout << *ptr+1 << endl; // First element + 1 12 | cout << ptr[0] << ptr[1] << ptr[2] << ptr[3] << endl; 13 | 14 | 15 | // For loop 16 | for(int i=0;i<4;i++){ 17 | cout << *ptr+i; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Day-11/05-Exception_handling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Rethrowing exceptions 6 | void func(){ 7 | throw 3; 8 | } 9 | 10 | 11 | void func1(){ 12 | try{ 13 | func(); 14 | }catch(int e){ 15 | cout << "Error occured in Func1" << endl; 16 | throw; 17 | } 18 | } 19 | 20 | int main(){ 21 | try{ 22 | func1(); 23 | }catch(...){ 24 | cout << "Error occured in main" << endl; 25 | } 26 | 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Day-9/01-Inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Base class 6 | class Animal{ 7 | public: 8 | int age; 9 | void breathe(){ 10 | cout << "Taking O2 and giving out Co2" << endl; 11 | } 12 | }; 13 | 14 | // Derived class or child class or sub class 15 | class Dog : public Animal { 16 | 17 | }; 18 | 19 | 20 | int main(){ 21 | Dog d; 22 | d.age = 5; 23 | cout << d.age << endl; 24 | d.breathe(); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Day-12/03-Bit_manipulation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | /* 7 | To calculate the number of set bits 8 | Input:- 3 -> 0011 9 | Output:- 2 10 | Input:- 4 -> 0100 11 | Output:- 1 12 | */ 13 | // Method 1 14 | int n; 15 | cin >> n; 16 | 17 | int result = 0; 18 | while(n!=0){ 19 | if(n&1){ 20 | result++; 21 | } 22 | n = n>>1; 23 | } 24 | cout << result << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Day-14/01-Functional_templates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // void display(int a){ 6 | // cout << "value of a is " << a << endl; 7 | // } 8 | 9 | 10 | // Functional Template 11 | template 12 | void display(T a){ 13 | cout << "Value of a is " << a << endl; 14 | } 15 | 16 | 17 | int main(){ 18 | // display(2); 19 | 20 | // Template based function call 21 | display(10); 22 | display(2.3); 23 | display("Hello everyone"); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Day-3/01-Pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int x = 10; 6 | cout << "Value of x is " << x << " and address of x is " << &x << endl; 7 | 8 | int *y = &x; 9 | // int *y 10 | // y = &x 11 | 12 | cout << "Value of variable y is: " << y << endl; 13 | cout << "Value at the address stored in y is: " << *y << endl; 14 | cout << "Address of y is: " << &y << endl; 15 | cout << sizeof(y) << endl; 16 | 17 | *y =100; 18 | cout << x << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Day-8/03-Practice_string_count_digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | string s; 7 | cin >> s; 8 | // count the frequency of a single digit 9 | // hello i am in 5th std and my roll no is 21556 -> 3 10 | int d; 11 | cin >> d; 12 | 13 | int count = 0; 14 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | try{ 8 | int a; 9 | cin >> a; 10 | // can't allow value of a to be 10 11 | if (a == 10){ 12 | // Do something 13 | throw "String exception thrown"; 14 | } 15 | cout << "Value of a is: " << a << endl; 16 | }catch (int a){ 17 | cout << "Can't assign a to 10" << endl; 18 | }catch(...){ 19 | cout << "Generic Exception" << endl; 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Day-8/02-Practice_string_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool compare(string a, string b){ 6 | return a.length() < b.length(); 7 | } 8 | 9 | int main(){ 10 | int n; 11 | cout << "Enter the number of strings" << endl; 12 | cin >> n; 13 | cin.get(); 14 | 15 | string s[100]; 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | // n => n natural numbers 3 6 | int n; 7 | long long int sum=0; 8 | cout << "Enter the value of n: " << endl; 9 | cin >> n; 10 | 11 | 12 | // Sol 1 -> O(n) 13 | if(n>0){ 14 | for(int i=1;i<=n;i++){ 15 | sum += i; 16 | } 17 | } 18 | cout << "Sum of n natural numbers is: " << sum << endl; 19 | sum = 0; 20 | // Sol 2 -> O(1) 21 | sum = (n*(n+1))/2; 22 | 23 | cout << "Sum of n natural numbers is: " << sum; 24 | 25 | } -------------------------------------------------------------------------------- /Day-14/02-Template_func_to_get_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | T sum_arr(T arr[],int size){ 7 | T result = 0; 8 | for(int i=0;i(arr_i,10) << endl; 22 | cout << "Sum of double array is " << sum_arr(arr_d,8) << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Day-15/04-Weighted_graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void add_edge(vector> adj[],int a,int b, int wt){ 6 | adj[a].push_back(make_pair(b,wt)); 7 | adj[b].push_back(make_pair(a,wt)); 8 | } 9 | 10 | 11 | int main(){ 12 | int V = 5; 13 | 14 | // Array of vector of pairs 15 | vector> adj[V]; 16 | 17 | add_edge(adj,0,1,10); 18 | add_edge(adj,0,4,20); 19 | add_edge(adj,1,4,30); 20 | add_edge(adj,1,3,40); 21 | add_edge(adj,1,2,50); 22 | add_edge(adj,3,4,60); 23 | add_edge(adj,2,3,70); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Day-4/01-Arrays_intro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | // int arr[] = {1,2,3,4,5,6,7,8,9,0}; 7 | // int size = 10; 8 | // for(int i=0;i> size2; 15 | 16 | int arr2[size2]; 17 | for(int i=0;i> arr2[i]; 19 | } 20 | 21 | cout << "You entered: " << endl; 22 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | void display(int arr[],int size){ 6 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | int result = 0; 8 | cin >> n; 9 | result = 0; 10 | // Removes the last set bit, So the number of times it removes the set bit 11 | // becomes the total number of set bits 12 | while (n > 0) 13 | { 14 | n = n & (n - 1); // 3 -> 0011&0010 -> 0010&0001 -> 0000 15 | // 9 -> 1001&1000 -> 1000&0111 -> 0000 16 | // 7 -> 0111&0110 -> 0110&0101-> 0100&0011 -> 0000 17 | result++; 18 | } 19 | cout << result << endl; 20 | // Method 3 21 | cout << __builtin_popcount(7); 22 | } -------------------------------------------------------------------------------- /Day-2/07-Power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long int power_iterative(int a, int b){ 5 | long long int result=1; 6 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | void display(int arr[],int size){ 6 | for(int i=0;i> rot; 21 | display(arr,size); 22 | 23 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | template 6 | class Calculator{ 7 | T x; 8 | T y; 9 | public: 10 | Calculator(T a,T b){ 11 | x = a; 12 | y = b; 13 | } 14 | 15 | T add(){ 16 | return x+y; 17 | } 18 | 19 | T sub(){ 20 | return x-y; 21 | } 22 | 23 | T mul(){ 24 | return x*y; 25 | } 26 | 27 | }; 28 | 29 | int main(){ 30 | Calculator c(10.3,20.6); 31 | 32 | cout << c.add() << endl; 33 | cout << c.mul() << endl; 34 | cout << c.sub() << endl; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Day-1/03-Type_cast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int num = 9; 5 | double num_double; 6 | 7 | // Implicit conversion 8 | num_double = num; 9 | cout << num << " " << num_double << endl; 10 | 11 | int num2; 12 | double num_double2 = 100.68; 13 | num2 = num_double2; 14 | cout << num2 << " " << num_double2 << endl; 15 | 16 | // C style type conversion 17 | double num_double3 = 3.14; 18 | int num3 = (int)num_double3; 19 | cout << num3 << " " << num_double3 << endl; 20 | 21 | 22 | // function type conversion 23 | int num4 = int(num_double3); 24 | cout << num4 << " " << num_double3; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Day-9/06-Vector_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | vector v; 8 | vector b(5,10); // 5 times 10 9 | vector c(b.begin(),b.end()); ///first index and 1 index next to the last index 10 | vector d{1,2,3,4,5}; 11 | 12 | // Iterate using a loop 13 | for(int i=0;i::iterator it 19 | for(auto it=d.begin();it!=d.end();it++){ 20 | cout << *it << " "; 21 | } 22 | cout << endl; 23 | 24 | // For each loop 25 | for(int x:d){ 26 | cout << x << " "; 27 | } 28 | cout << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Day-9/04-Function_overriding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Animal{ 6 | public: 7 | void feeding(){ 8 | cout << "Eat food" << endl; 9 | } 10 | void speak(){ 11 | cout << "Makes a sound " << endl; 12 | } 13 | 14 | }; 15 | 16 | class Dog : public Animal{ 17 | public: 18 | void speak(){ 19 | // Animal::speak(); 20 | cout << "Bow bow" << endl; 21 | } 22 | void do_trick(){ 23 | cout << "Sit" << endl; 24 | } 25 | 26 | }; 27 | 28 | class Oliver : public Dog{}; 29 | 30 | 31 | int main(){ 32 | Oliver d; 33 | d.speak(); 34 | d.do_trick(); 35 | d.feeding(); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Day-9/03-Inheritance_access_modes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class A{ 6 | private: 7 | int a; 8 | protected: 9 | int b; 10 | public: 11 | int c; 12 | }; 13 | 14 | class B : public A{ 15 | private: 16 | int d; 17 | protected: 18 | int e; 19 | public: 20 | B(){ 21 | cout << "b = " << b << endl; 22 | } 23 | }; 24 | 25 | class C : private B{ 26 | public: 27 | C(){ 28 | cout << " b = " << b << endl; 29 | cout << " c = " << c << endl; // Can we access ? 30 | cout << " e = " << e << endl; // Can we access ? 31 | } 32 | }; 33 | 34 | 35 | 36 | int main(){ 37 | C obj; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Day-11/03-Exception_handling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Student{}; 6 | 7 | int main(){ 8 | double a,b; 9 | cin >> a >> b; 10 | try{ 11 | if(b==0){ 12 | throw 400; 13 | }else if(a==20){ 14 | throw 10; 15 | } 16 | if(a == 100){ 17 | Student s; 18 | throw s; 19 | } 20 | cout << "a/b = " << a/b << endl; 21 | 22 | }catch(int e){ 23 | if(e==400){ 24 | cout << "Infinity" << endl; 25 | } 26 | cout << "Integer error occured" << endl; 27 | }catch(Student e){ 28 | cout << "Saved this student from errors" << endl; 29 | }catch(...){ 30 | cout << "Generic exception occured" << endl; 31 | } 32 | 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Day-12/01-Bit_manipulation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | 1111 -> 0-15 7 | 8421 8 | 9 | & -> 1 1 => 1, 0 otherwise 10 | | -> 0 0 => 0, 1 otherwise 11 | ! -> 0 => 1, 0 otherwise 12 | ^ -> 1 1 or 0 0 => 0 and 1 otherwise 13 | 14 | 5 ^ 5 -> 0101^0101 -> 0 15 | 5 ^ 5 ^ 6 -> 0000^0110 -> 0110 = 6 16 | 17 | */ 18 | 19 | int main(){ 20 | // To find a unique number from an array 21 | // Input:- 11 22 | // 5 6 7 5 7 8 8 0 0 3 3 23 | // Output:- 6 24 | int n; 25 | cin >> n; 26 | 27 | int result = 0; 28 | while(n--){ 29 | int temp; 30 | cin >> temp; 31 | result = result ^ temp; 32 | } 33 | cout << "********************" << endl; 34 | cout << result << endl; 35 | 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Day-0/intro.txt: -------------------------------------------------------------------------------- 1 | Intro about Cpp/C++:- 2 | 1.) General Purpose Middle Level Programming Language. 3 | 2.) It's a superset of C Language 4 | 3.) It has object oriented and also have generic programming features. 5 | 4.) Linux,Unix, Windows and Mac 6 | 7 | G++ Commands:- 8 | 1.) g++ fileName.cpp 9 | 2.) g++ -o ExecutableName.exe fileName.cpp 10 | 11 | Git Commands:- 12 | 1.) For cloning the repo -> git clone https://github.com/Satyam-Bhalla/CPP-B2-2022.git 13 | 2.) For adding all our changes -> git add . 14 | 3.) For commiting our changes -> git commit -m "Message for commit" 15 | 4.) For pushing code -> git push 16 | 5.) For pulling code -> git pull 17 | 18 | Other Links:- 19 | 1.) https://www.codeblocks.org/ 20 | 2.) https://code.visualstudio.com/docs/cpp/config-mingw 21 | 3.) https://git-scm.com/ 22 | -------------------------------------------------------------------------------- /Day-2/04-Call_by.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void incre_by_value(int a){ 5 | a++; 6 | cout << "Value of the copy variable a is: " << a << endl; 7 | } 8 | 9 | void incre_by_address(int *ptr){ 10 | (*ptr)++; 11 | } 12 | 13 | // Reference is an alias 14 | void incre_by_reference(int &r){ 15 | r++; 16 | } 17 | 18 | 19 | int main(){ 20 | int a = 10; 21 | cout << "Value of variable a is " << a << " and Address of variable a is: " << &a << endl; 22 | 23 | incre_by_value(a); 24 | cout << "Value of variable a is " << a << endl; 25 | 26 | // int *ptr = &a; 27 | incre_by_address(&a); 28 | cout << "Value of variable a is " << a << endl; 29 | 30 | incre_by_reference(a); 31 | cout << "Value of variable a is " << a << endl; 32 | 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Day-11/08-Set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Program to find unique strings in sorted order 6 | void print(set &s){ 7 | cout << "Size of set is: " << s.size() << endl; 8 | // Printing method 1 9 | for(string value: s){ 10 | cout << value << endl; 11 | } 12 | cout << endl; 13 | // Printing method 2 14 | for(auto it = s.begin(); it != s.end();it++){ 15 | cout << *it << endl; 16 | } 17 | } 18 | 19 | int main(){ 20 | // Creating a set 21 | set s; 22 | 23 | // Inserting data in a set 24 | s.insert("abc"); //log n 25 | s.insert("abcd"); 26 | s.insert("abc"); 27 | s.insert("ab"); 28 | s.insert("bcd"); 29 | 30 | // Searching in a set 31 | auto it = s.find("abc"); 32 | if(it != s.end()){ 33 | s.erase(it); 34 | } 35 | cout << "*******************" << endl; 36 | print(s); 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Day-15/01-Valid_parentheses.cpp: -------------------------------------------------------------------------------- 1 | // () {} [] -> valid 2 | // ({[]}) -> valid 3 | // (}) -> Invalid 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | bool isValid(string s){ 10 | stack stk; 11 | for(const auto &c : s){ 12 | switch(c){ 13 | case '{': 14 | stk.push('}'); 15 | break; 16 | case '[': 17 | stk.push(']'); 18 | break; 19 | case '(': 20 | stk.push(')'); 21 | break; 22 | default: 23 | if(stk.size()==0 || c != stk.top()) return false; 24 | else stk.pop(); 25 | 26 | } 27 | } 28 | return stk.size()==0; 29 | 30 | } 31 | 32 | 33 | int main(){ 34 | string s; 35 | cin >> s; 36 | if(isValid(s)){ 37 | cout << "Valid" << endl; 38 | }else{ 39 | cout << "Invalid" << endl; 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Day-10/06-File_handling_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // istream and ostream 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Student{ 8 | public: 9 | string name; 10 | int age; 11 | Student(){} 12 | Student(string name,int age){ 13 | this->name = name; 14 | this->age = age; 15 | } 16 | void display(){ 17 | cout << "Name: " << name << " Age: " << age << endl; 18 | } 19 | }; 20 | 21 | 22 | int main(){ 23 | Student s("Satyam",2500); 24 | fstream f("student.bat",ios::out|ios::binary); 25 | if(!f){ 26 | cout << "Some error occured" << endl; 27 | }else{ 28 | f.write((char *)&s, sizeof(Student)); 29 | f.close(); 30 | } 31 | 32 | if(f.good()){ 33 | cout << "File written successfully" << endl; 34 | s.display(); 35 | }else{ 36 | cout << "Error occured" << endl; 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Day-11/07-Unordered_map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Program to find the frequency of strings 6 | void print(unordered_map &m){ 7 | cout << "Size of unordered map is: " << m.size() << endl; 8 | // map :: iterator it; 9 | // for(it = m.begin(); it!=m.end();it++){ 10 | // cout << (*it).first << " " << it->second << endl; 11 | // } 12 | for(auto &it:m){ 13 | cout << it.first << " " << it.second << endl; 14 | } 15 | } 16 | 17 | int main(){ 18 | /* Key value pair 19 | unique key : Value 20 | */ 21 | 22 | // Red black trees are used under the hood 23 | // Create a unordered map 24 | unordered_map m; 25 | int n; 26 | cin >> n; 27 | for(int i=0;i> s; 30 | m[s]++; 31 | } 32 | 33 | cout << "*******************" << endl; 34 | print(m); 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Day-11/09-Unordered_set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Program to find unique strings in sorted order 6 | void print(unordered_set &s){ 7 | cout << "Size of set is: " << s.size() << endl; 8 | // Printing method 1 9 | for(string value: s){ 10 | cout << value << endl; 11 | } 12 | cout << endl; 13 | // Printing method 2 14 | for(auto it = s.begin(); it != s.end();it++){ 15 | cout << *it << endl; 16 | } 17 | } 18 | 19 | int main(){ 20 | // Creating a set 21 | unordered_set s; 22 | 23 | // Inserting data in a set 24 | s.insert("abc"); //log n 25 | s.insert("abcd"); 26 | s.insert("abc"); 27 | s.insert("ab"); 28 | s.insert("bcd"); 29 | 30 | // Searching in a set 31 | auto it = s.find("abc"); 32 | if(it != s.end()){ 33 | s.erase(it); 34 | } 35 | cout << "*******************" << endl; 36 | print(s); 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Day-1/04-Flow_control.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int a=7,b=2; 7 | /* 8 | if(condition is true){ 9 | run if block 10 | }else{ 11 | run else block 12 | } 13 | 14 | if(condition is true){ 15 | run if block 16 | }else if(if this condition is true){ 17 | run else if block 18 | }else{ 19 | run else block 20 | } 21 | 22 | */ 23 | // a = 7,b=2 24 | if((a%b==1) || (a> number >> c; 33 | cout << "Number is: " << number << " and ascii of character is " << (int)c << endl; 34 | 35 | if(number%2==0){ 36 | cout << "Even number" << endl; 37 | }else{ 38 | cout << "Odd Number" << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Day-2/03-Function_overloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int max_value(int a, int b){ 6 | cout << "Max with 2 integers is " << endl; 7 | if(a>b){ 8 | return a; 9 | }else{ 10 | return b; 11 | } 12 | } 13 | 14 | double max_value(double a, double b){ 15 | cout << "Max with 2 double is " << endl; 16 | if(a>b){ 17 | return a; 18 | }else{ 19 | return b; 20 | } 21 | } 22 | 23 | int max_value(int a, int b, int c){ 24 | cout << "Max with 3 integers is " << endl; 25 | if(a>b){ 26 | if(a>c){ 27 | return a; 28 | }else{ 29 | return c; 30 | } 31 | 32 | }else{ 33 | if(b>c){ 34 | return b; 35 | }else{ 36 | return c; 37 | } 38 | } 39 | } 40 | 41 | 42 | 43 | int main(){ 44 | cout << max_value(10,20) << endl; 45 | cout << max_value(10,20,30) << endl; 46 | cout << max_value(10.25,20.36) << endl; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Day-1/02-Operators.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Arithmetic operators => +,-,*,/,% 3 | Unary Operators: => ++,-- 4 | Binary Operators: => +,-,*,/,% 5 | 6 | Relational Operators (>,<,>=,<=,==) 7 | Logical Operators (||, &&) 8 | Bitwise Operators( &, |, ^) 9 | Assignment operator ( = ) 10 | 11 | Other operators:- 12 | 1.) sizeof 13 | 2.) Comma operator 14 | 3.) Conditional ( condition ? true : false) 15 | */ 16 | #include 17 | 18 | using namespace std; 19 | 20 | int main(){ 21 | int a,b; 22 | a = 7; 23 | b = 9; 24 | 25 | cout << "a+b=" << (a+b) << endl; 26 | cout << "a-b=" << (a-b) << endl; 27 | cout << "a*b=" << (a*b) << endl; 28 | cout << "a/b=" << (a/b) << endl; 29 | cout << "a%b=" << (a%b) << endl; 30 | 31 | cout << "******Unary Operators*********" << endl; 32 | cout << a++ << endl; 33 | cout << --b << endl; 34 | 35 | 36 | cout << "**********************" << endl; 37 | bool result = !(a==b); 38 | cout << result << endl; 39 | 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Day-5/03-Copy_constructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Number{ 6 | int a; 7 | public: 8 | // Default constructor 9 | Number(){ 10 | a = 0; 11 | cout << "Default constructor called" << endl; 12 | } 13 | // Parameterized constructor 14 | Number(int num){ 15 | cout << "Parameterized constructor called "<< endl; 16 | a = num; 17 | } 18 | // Copy constructor 19 | Number(Number &obj){ 20 | cout << "Copy constructor called" << endl; 21 | a = obj.a; 22 | } 23 | 24 | void display(){ 25 | cout << "Number is " << a << endl; 26 | } 27 | }; 28 | 29 | 30 | 31 | int main(){ 32 | Number x,y,z(42),z2; 33 | x.display(); 34 | y.display(); 35 | z.display(); 36 | 37 | // Copy object from one to another 38 | z2 = z ; 39 | z2.display(); 40 | 41 | Number z3(z); 42 | z3.display(); 43 | 44 | Number z4 = z; 45 | z4.display(); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Day-15/03-DFS_graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // For undirected graph 6 | void add_edge(vector adj[],int a,int b){ 7 | adj[a].push_back(b); 8 | adj[b].push_back(a); 9 | } 10 | 11 | 12 | void dfs_check(int u,vectoradj[], vector &visited ){ 13 | visited[u] = true; 14 | cout << u << " "; 15 | for(int i=0;i adj[],int V){ 23 | vector visited(V,false); 24 | for(int i=0;i adj[V]; 36 | 37 | add_edge(adj,0,1); 38 | add_edge(adj,0,4); 39 | add_edge(adj,1,4); 40 | add_edge(adj,1,3); 41 | add_edge(adj,1,2); 42 | add_edge(adj,2,3); 43 | DFS(adj,V); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Day-14/05-Stack_using_vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | template 7 | class Stack{ 8 | private: 9 | vector v; 10 | public: 11 | void push(T data){ 12 | v.push_back(data); 13 | } 14 | 15 | bool empty(){ 16 | return v.empty(); 17 | } 18 | 19 | void pop(){ 20 | if(!v.empty()){ 21 | v.pop_back(); 22 | } 23 | } 24 | 25 | T top(){ 26 | return v.back(); 27 | } 28 | 29 | 30 | }; 31 | 32 | 33 | 34 | int main(){ 35 | Stack s; 36 | for(int i=0;i<5;i++){ 37 | s.push(i*i); 38 | } 39 | 40 | while(!s.empty()){ 41 | cout << s.top() << endl; 42 | s.pop(); 43 | } 44 | cout << endl << "************************" << endl; 45 | 46 | Stack c; 47 | for(int i = 0;i<26;i++){ 48 | c.push('a'+i); 49 | } 50 | 51 | while(!c.empty()){ 52 | cout << c.top() << endl; 53 | c.pop(); 54 | } 55 | cout << endl; 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Day-11/06-Map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void print(map &m){ 7 | cout << "Size of map is: " << m.size() << endl; 8 | map :: iterator it; 9 | for(it = m.begin(); it!=m.end();it++){ 10 | cout << (*it).first << " " << it->second << endl; 11 | } 12 | } 13 | 14 | int main(){ 15 | /* Key value pair 16 | unique key : Value 17 | */ 18 | 19 | // Red black trees are used under the hood 20 | // Create a map 21 | map m; 22 | 23 | // Insert Data in the map 24 | m["Gagan"] = 90; 25 | m["Ashutosh"] = 80; 26 | m["Pallavi"] = 85; 27 | m.insert({"Aditya", 88}); 28 | m["Ashutosh"] = 70; // O(Log n) 29 | 30 | // map::iterator it; 31 | auto it = m.find("Aditya"); // m["Aditya"] 32 | if(it == m.end()){ 33 | m["Aditya"] = 88; 34 | }else{ 35 | cout << it->first << " " << it->second << endl; 36 | m.erase(it); // O(log(n)) 37 | } 38 | 39 | cout << "*******************" << endl; 40 | print(m); 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Day-10/03-Vector_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Car{ 9 | public: 10 | string name; 11 | int price; 12 | int model; 13 | 14 | Car(string name,int x,int y){ 15 | this->name = name; 16 | this->price = x; 17 | this->model = y; 18 | } 19 | 20 | void print(){ 21 | cout << "Name: " << name << " " << price << " " << model << endl; 22 | } 23 | }; 24 | 25 | bool compare(Car a, Car b){ 26 | return a.price> n; 32 | vector v; 33 | for(int i = 0; i> name >> x >> y; 37 | Car c(name,x,y); 38 | v.push_back(c); 39 | } 40 | 41 | for(auto x:v){ 42 | x.print(); 43 | } 44 | 45 | cout << "************Sorting****************" << endl; 46 | sort(v.begin(),v.end(),compare); 47 | for(auto x:v){ 48 | x.print(); 49 | } 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Day-10/01-Vector_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | vector v; 8 | vector b(5,10); // 5 times 10 9 | vector c(b.begin(),b.end()); ///first index and 1 index next to the last index 10 | vector d{1,2,3,4,5}; 11 | 12 | //Pushing elements inside vector by taking input from user 13 | int n = 5; 14 | int num; 15 | for(int i=0;i> num; 17 | v.push_back(num); 18 | } 19 | 20 | cout << "You have entered " << endl; 21 | 22 | // Iterate using a loop 23 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | int r, c, a[100][100],b[100][100],sum[100][100],i,j; 8 | cout << "Enter the number of rows between 1-100: "; 9 | cin >> r; 10 | cout << "Enter the number of cols between 1-100: "; 11 | cin >> c; 12 | cout << endl << "Enter the elements of first matrix:" << endl; 13 | // Take input in 2D array 14 | for(i = 0;i> a[i][j]; 18 | } 19 | } 20 | for(i = 0;i> b[i][j]; 24 | } 25 | } 26 | 27 | // Adding the matrix 28 | cout << endl << "Sum of 2 matix is" << endl; 29 | for(i=0;i 2 | #include // istream and ostream 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | fstream myFile; 9 | /* 10 | Modes 11 | 1.) Read -> ios::in 12 | 2.) Write -> ios::out 13 | 3.) Append -> ios::app 14 | */ 15 | // write 16 | // myFile.open("satyam.txt",ios::out); 17 | // if(myFile.is_open()){ 18 | // myFile << "Hello\n"; 19 | // myFile << "How are you doing?\n"; 20 | // myFile.close(); 21 | // }else{ 22 | // cout << "Error in opening the file" << endl; 23 | // } 24 | 25 | // Append 26 | // myFile.open("satyam.txt",ios::app); 27 | // if(myFile.is_open()){ 28 | // myFile << "I am a Software Developer\n"; 29 | // myFile.close(); 30 | // }else{ 31 | // cout << "Error in opening the file" << endl; 32 | // } 33 | 34 | 35 | // Read 36 | myFile.open("satyam.txt",ios::in); 37 | if(myFile.is_open()){ 38 | string line; 39 | while(getline(myFile,line)){ 40 | cout << line << endl; 41 | } 42 | myFile.close(); 43 | }else{ 44 | cout << "Error in opening the file" << endl; 45 | } 46 | } -------------------------------------------------------------------------------- /Day-6-7/03-Searching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // O(n) 6 | int linear_search(int arr[],int n, int key){ 7 | for(int i=0;ikey){ 23 | return binary_search(arr,start,mid-1,key); 24 | } 25 | if(arr[mid]> key; 42 | 43 | // int result = linear_search(arr,n,key); 44 | int result = binary_search(arr,0,n-1,key); 45 | 46 | if(result != -1){ 47 | cout << "Element found at index " << result << endl; 48 | }else{ 49 | cout << "Element not found"<< endl; 50 | } 51 | 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Day-15/02-BFS_Graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector> g; 6 | vector v;//visited check vector 7 | 8 | //For directed graph 9 | void add_edge(int a,int b){ 10 | g[a].push_back(b); 11 | 12 | // For Undirected graph 13 | // g[b].push_back(a); 14 | } 15 | 16 | 17 | void bfs(int u){ 18 | queue q;; 19 | // Push elements in queue for processing 20 | q.push(u); 21 | 22 | // Update the visited array to traverse every node once 23 | v[u] = true; 24 | 25 | while(!q.empty()){ 26 | int f = q.front(); 27 | q.pop(); 28 | cout << f << " "; 29 | 30 | //Push all the neighbours of u in the queue 31 | for(auto i=g[f].begin();i!=g[f].end();i++){ 32 | if(!v[*i]){ 33 | q.push(*i); 34 | v[*i] = true; 35 | } 36 | } 37 | } 38 | } 39 | 40 | 41 | int main(){ 42 | int n,e; 43 | cin >> n >> e; 44 | g.assign(n,vector()); 45 | v.assign(n,false); 46 | 47 | int a , b; 48 | for(int i=0;i> a >> b; 50 | add_edge(a,b); 51 | } 52 | 53 | // BFS 54 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | class Student{ 6 | public: 7 | string name; 8 | string school; 9 | int rollNo; 10 | int age; 11 | 12 | // Default 13 | Student(){ 14 | cout << "Calling the default constructor"<< endl; 15 | } 16 | 17 | // Parameterized constructors 18 | Student(string n, string s, int r, int a){ 19 | name = n; 20 | school = s; 21 | rollNo = r; 22 | age = a; 23 | } 24 | 25 | Student(string n, int r, int a){ 26 | name = n; 27 | school = "D.A.V"; 28 | rollNo = r; 29 | age = a; 30 | } 31 | 32 | 33 | 34 | void introduce(){ 35 | cout << "Name: " << name << endl; 36 | cout << "School: " << school << endl; 37 | cout << "Roll No: " << rollNo << endl; 38 | cout << "Age: " << age << endl; 39 | } 40 | }; 41 | 42 | 43 | int main(){ 44 | Student s1; 45 | s1.name = "Satyam"; 46 | s1.school = "D.A.V."; 47 | s1.rollNo = 15; 48 | s1.age = 10; 49 | s1.introduce(); 50 | 51 | Student s2("Satyam","Cipherschools",23,121); 52 | s2.introduce(); 53 | 54 | Student s3("Simran",28,11); 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Day-12/02-Bit_manipulation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | 5 | /* 6 | 1111 -> 0-15 7 | 8421 8 | 9 | & -> 1 1 => 1, 0 otherwise 10 | | -> 0 0 => 0, 1 otherwise 11 | ! -> 0 => 1, 0 otherwise 12 | ^ -> 1 1 or 0 0 => 0 and 1 otherwise 13 | 14 | 5 ^ 5 -> 0101^0101 -> 0 15 | 5 ^ 5 ^ 6 -> 0000^0110 -> 0110 = 6 16 | 17 | 18 | 2^3 = 8 (^ represents power) 19 | 3 -> 0011 20 | 2*2^0 + 2*2^1 21 | 2^4 = 16 22 | 4 = 0100 23 | 2^0*0 + 24 | 2^1*0 + 25 | 2^2*1 + 26 | 2^3*0 27 | 28 | 29 | */ 30 | 31 | 32 | ll fastExpo(ll a,ll b){ // a = 2, b = 3 33 | ll p =a, ans = 1; // p = 2, ans = 1 34 | while(b>0){ // b = 3 35 | if(b&1){ // 0011&0001 -> 0001 | 0001&0001 -> 0001 36 | ans *= p; // ans = 2 | ans = 2*4 = 8 37 | } 38 | p *= p; // p = 2*2 = 4 | p = 4*4 = 16 39 | b = b>>1; // b = 0011 -> 0001 | 0001 -> 0000 40 | } 41 | return ans; 42 | } 43 | 44 | 45 | int main(){ 46 | /* To find a power b in log n time 47 | Input:- 2 3 48 | Output:- 8 49 | Solution: 50 | Step 1:- Check if any bit is set 51 | Step 2:- check if last bit is set 52 | Step 3:- Right shift by 1 53 | 54 | 2 power 3 55 | 0011&0001 => 0001 => ans = 2, p=p*p => 2*2 = 4, p=4 56 | 0001&0001 => 0001 57 | */ 58 | ll a,b; 59 | cin >> a >> b; 60 | cout << fastExpo(a,b); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /Day-10/02-Vector_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | vector v; 9 | vector b(5, 10); // 5 times 10 10 | vector c(b.begin(), b.end()); ///first index and 1 index next to the last index 11 | vector d{1, 2, 3, 4, 5}; 12 | 13 | // pop_back removes the last element 14 | d.pop_back(); 15 | 16 | // insert elemenets in the middle of the vector: O(n) 17 | d.insert(d.begin() + 2, 3, 200); 18 | 19 | // Delete few elements from the middle of the vector 20 | d.erase(d.begin() + 1, d.begin() + 4); 21 | 22 | // Iterate using a loop 23 | for (int i = 0; i < d.size(); i++) 24 | { 25 | cout << d[i] << " "; 26 | } 27 | cout << endl; 28 | 29 | cout << "Capacity of d is " << d.capacity() << endl; 30 | 31 | d.resize(5); 32 | 33 | cout << "Capacity of d is " << d.capacity() << endl; 34 | 35 | d.clear(); 36 | cout << "Size of d is " << d.size() << endl; 37 | 38 | if (d.empty()) 39 | { 40 | cout << "No elemenets available" << endl; 41 | } 42 | else 43 | { 44 | for (int i = 0; i < d.size(); i++) 45 | { 46 | cout << d[i] << " "; 47 | } 48 | cout << endl; 49 | } 50 | 51 | d.push_back(10); 52 | d.push_back(20); 53 | d.push_back(30); 54 | d.push_back(40); 55 | 56 | // First and last element from a vector 57 | cout << d.front() << endl; 58 | cout << d.back() << endl; 59 | 60 | // Sort vector 61 | sort(d.begin(),d.end()); 62 | 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Day-14/07-Queue_using_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Queue{ 6 | int *arr; 7 | int f,r,cs,ms; 8 | public: 9 | Queue(int ds=5){ 10 | arr = new int[ds]; 11 | cs = 0; 12 | ms = ds; 13 | f = 0; 14 | r = ms-1; 15 | } 16 | 17 | bool full(){ 18 | return cs==ms; 19 | } 20 | 21 | bool empty(){ 22 | return cs==0; 23 | } 24 | 25 | int front(){ 26 | return arr[f]; 27 | } 28 | 29 | void push(int data){ 30 | if(!full()){ 31 | r = (r+1)%ms; 32 | arr[r] = data; 33 | cs++; 34 | }else{ 35 | cout << "Queue overflow" << endl; 36 | } 37 | } 38 | 39 | void pop(){ 40 | if(!empty()){ 41 | f = (f+1)%ms; 42 | cs--; 43 | }else{ 44 | cout << "Queue underflow!" << endl; 45 | } 46 | } 47 | 48 | ~Queue(){ 49 | if(arr!=NULL){ 50 | delete [] arr; 51 | arr = NULL; 52 | } 53 | } 54 | }; 55 | 56 | int main(){ 57 | 58 | int n = 10; 59 | Queue q(n); 60 | 61 | // Push elements in Queue 62 | for(int i=0;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | // User defined functions or Inbuilt functions 7 | 8 | 9 | /* 10 | Function syntax 11 | return_type function_name(params){ 12 | function body 13 | } 14 | */ 15 | 16 | // Function declaration/ Function Prototype 17 | int addition(int c,int d); 18 | 19 | // Declaration and definition 20 | int subtraction(int a,int b){ 21 | return a-b; 22 | } 23 | 24 | int main(){ 25 | int a,b; 26 | bool is_continue = true; 27 | while(is_continue){ 28 | cout << "Enter two numbers: "; 29 | cin >> a >> b; 30 | cout << "Enter the option: " << endl; 31 | cout << "Press 1 for addition" << endl; 32 | cout << "Press 2 for subtraction" << endl; 33 | cout << "Press 3 for power" << endl; 34 | cout << "Press 4 to exit" << endl; 35 | cout << "Your Input: "; 36 | int res; 37 | cin >> res; 38 | switch(res){ 39 | case 1: 40 | cout << "addition = " << addition(a,b) << endl; 41 | break; 42 | case 2: 43 | cout << "subtraction = " << subtraction(a,b) << endl; 44 | break; 45 | case 3: 46 | cout << "a power b is = " << pow(a,b) << endl; 47 | break; 48 | case 4: 49 | is_continue = false; 50 | break; 51 | default: 52 | cout << "Please use numbers from 1 to 4" << endl; 53 | break; 54 | } 55 | 56 | } 57 | return 0; 58 | } 59 | 60 | int addition(int c,int d){ 61 | return c+d; 62 | } -------------------------------------------------------------------------------- /Day-4/05-String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // for string class 3 | using namespace std; 4 | int main() 5 | { 6 | string str = "learn and grow and learn fast"; 7 | cout << "The initial string is : "; 8 | cout << str << endl; 9 | str.resize(13); 10 | cout << "The string after resize operation is : "; 11 | cout << str << endl; 12 | cout << "The capacity of string is : "; 13 | cout << str.capacity() << endl; 14 | cout<<"The length of the string is :"< 2 | 3 | using namespace std; 4 | 5 | // Access Modifiers 6 | // 1.) Public:- Accessible by all 7 | // 2.) Private:- Accessible by member functions of same class 8 | // 3.) Protected:- Accessible by member functions and derived class member functions 9 | 10 | 11 | class Person{ 12 | public: 13 | string name; 14 | int age; 15 | void func(){ 16 | introduce(); 17 | } 18 | void printMarks(int marks); 19 | 20 | private: 21 | int money; 22 | void introduce(){ 23 | cout<< "My name is " << name << endl; 24 | cout << "My age is " << age << endl; 25 | } 26 | friend class Government; 27 | friend void friendFunction(Person p); 28 | }; 29 | 30 | // friend function 31 | void friendFunction(Person p){ 32 | p.introduce(); 33 | } 34 | 35 | // friend class 36 | class Government{ 37 | public: 38 | void hackerman(Person p3){ 39 | cout << "The name of this person is " << p3.name << endl; 40 | cout << "The age of this person is " << p3.age << endl; 41 | p3.introduce(); 42 | } 43 | void assignAadhar(Person p3){ 44 | 45 | } 46 | }; 47 | 48 | void Person::printMarks(int marks){ 49 | cout << "You got " << marks << " marks" << endl; 50 | func(); 51 | } 52 | 53 | int main(){ 54 | Person p; 55 | p.name = "Satyam"; 56 | p.age= 1000; 57 | 58 | Person p2; 59 | p2.name = "Mohit"; 60 | p2.age = 1500; 61 | 62 | // p.func(); 63 | // p2.func(); 64 | // p.printMarks(100); 65 | 66 | Person p3; 67 | p3.name = "Swati"; 68 | p3.age = 800; 69 | 70 | Government g; 71 | g.hackerman(p3); 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Day-6-7/01-Add_Matrices.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Matrix{ 6 | public: 7 | int arr[3][3]; 8 | Matrix(int m){ 9 | for(int i=0;i<3;i++){ 10 | for(int j=0;j<3;j++){ 11 | arr[i][j] = (i+j)*m; 12 | } 13 | } 14 | } 15 | 16 | void display(){ 17 | for(int i=0;i<3;i++){ 18 | for(int j=0;j<3;j++){ 19 | cout << arr[i][j] << " "; 20 | } 21 | cout << endl; 22 | } 23 | } 24 | 25 | // Matrix a + Matrix b 26 | Matrix operator +(Matrix b){ 27 | Matrix res(0); 28 | for(int i=0;i<3;i++){ 29 | for(int j=0;j<3;j++){ 30 | res.arr[i][j] = arr[i][j] + b.arr[i][j]; 31 | } 32 | } 33 | return res; 34 | } 35 | 36 | void operator ++(){ 37 | for(int i=0;i<3;i++){ 38 | for(int j=0;j<3;j++){ 39 | ++arr[i][j]; 40 | } 41 | } 42 | } 43 | 44 | 45 | }; 46 | 47 | 48 | int main(){ 49 | Matrix a(1); 50 | Matrix b(2); 51 | Matrix c(3); 52 | a.display(); 53 | cout << "*****************************" << endl; 54 | b.display(); 55 | cout << "*****************************" << endl; 56 | c.display(); 57 | cout << "*****************************" << endl; 58 | 59 | // Overloading 60 | Matrix d = a+b+c; 61 | d.display(); 62 | cout << "*****************************" << endl; 63 | Matrix e(6); 64 | e.display(); 65 | cout << "*****************************" << endl; 66 | 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Day-12/05-Binary_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node *left = NULL; 9 | node *right = NULL; 10 | node(int d){ 11 | data=d; 12 | } 13 | }; 14 | // 10 15 | // NULL NULL 16 | // 10 -1 -1 17 | // 1 18 | // 2 3 19 | // 4 5 -1 -1 20 | //-1 -1 -1 -1 21 | 22 | node* buildTree(){ 23 | int d; 24 | cin >> d; 25 | 26 | // Base case 27 | if(d==-1){ 28 | return NULL; 29 | } 30 | node *root = new node(d); 31 | root->left = buildTree(); 32 | root->right = buildTree(); 33 | 34 | return root; 35 | } 36 | 37 | // Preorder Traversal 38 | void printPreorder(node *root){ 39 | if(root==NULL) return; 40 | // Root Left Right 41 | cout << root->data << " "; 42 | printPreorder(root->left); 43 | printPreorder(root->right); 44 | } 45 | 46 | // Inorder Traversal 47 | void printInorder(node *root){ 48 | if(root==NULL){ 49 | return; 50 | } 51 | // Left Root Right 52 | printInorder(root->left); 53 | cout << root->data << " "; 54 | printInorder(root->right); 55 | } 56 | 57 | // Postorder Traversal 58 | void printPostorder(node *root){ 59 | if(root==NULL){ 60 | return; 61 | } 62 | // Left Right Root 63 | printPostorder(root->left); 64 | printPostorder(root->right); 65 | cout << root->data << " "; 66 | } 67 | 68 | 69 | 70 | int main(){ 71 | // node *root = new node(1); 72 | // root->left = new node(2); 73 | // root->right = new node(3); 74 | // root->left->left = new node(4); 75 | // root->left->right = new node(5); 76 | 77 | node *root = buildTree(); 78 | printPreorder(root); 79 | cout << endl; 80 | printInorder(root); 81 | cout << endl; 82 | printPostorder(root); 83 | return 0; 84 | } -------------------------------------------------------------------------------- /Day-1/01-Data_types.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1.) Primitive -> Integer, Floating Point, Character, Boolean, Double, Void, Wide Character 3 | 2.) Derived Data Types -> Function, Array, Pointer, References(Alias) 4 | 3.) Abstract/User Defined -> Class, Structure, Union, Enumerations and Typedef 5 | 6 | Memory and the Number which we can store 7 | Character -> 1 Byte -> -128 to 127 -> 0-255 8 | Boolean -> 1 Byte -> 0 or 1 9 | Integer -> 4 Bytes -> 32 bits -> -2,147,483,648 to 2,147,483,647 10 | Float -> 4 Bytes -> 32 bits 11 | Double -> 8 Bytes 12 | Void -> Valueless entity 13 | Wide Character-> 2 or 4 Bytes 14 | 15 | Data type modifiers 16 | signed 17 | unsigned 18 | short 19 | long 20 | long long 21 | 22 | Examples Memory in Bytes 23 | short int 2 24 | int 4 25 | long int 4 26 | long long int 8 27 | signed int 4 28 | unsigned int 4 29 | unsigned short int 2 30 | */ 31 | 32 | #include 33 | using namespace std; 34 | int main(){ 35 | cout << "Size of char " << sizeof(char) << " byte." << endl; 36 | cout << "Size of int " << sizeof(int) << " byte." << endl; 37 | cout << "Size of float " << sizeof(float) << " byte." << endl; 38 | cout << "Size of double " << sizeof(double) << " byte." << endl; 39 | cout << "Size of short int " << sizeof(short int) << " byte." << endl; 40 | cout << "Size of long int " << sizeof(long int) << " byte." << endl; 41 | cout << "Size of long long int " << sizeof(long long int) << " byte." << endl; 42 | cout << "Size of wchar_t " << sizeof(wchar_t) << " byte." << endl; 43 | 44 | // Declarations and definition 45 | char a = 'a'; 46 | int b = 10; 47 | float c = 100.2; 48 | wchar_t t = L'S'; 49 | cout << sizeof(a) << " " << b << " " << c << endl; 50 | 51 | 52 | // Multiple declarations and definitions 53 | int d = 0, e, f; 54 | d = 15; 55 | cout << e << endl; 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Day-9/05-Vector_implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Dynamic Arrays are known as vectors 6 | class Vector{ 7 | // Data members 8 | int *arr; 9 | int cs; 10 | int maxSize; 11 | public: 12 | Vector(int defaultSize=4){ 13 | maxSize = defaultSize; 14 | cs = 0; 15 | // Dynamically allocate memory at run time 16 | arr = new int[maxSize]; 17 | } 18 | 19 | void push_back(int data){ 20 | if(cs==maxSize){ 21 | // Create a copy of initial array 22 | int *oldArr = arr; 23 | 24 | // Double the size of current array 25 | arr = new int[2*maxSize]; 26 | maxSize = 2*maxSize; 27 | 28 | // Copy elements from old to new array 29 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | class Complex{ 6 | int real; 7 | int img; 8 | public: 9 | Complex(){ 10 | real = 0; 11 | img = 0; 12 | } 13 | 14 | Complex(int r,int i){ 15 | real = r; 16 | img = i; 17 | } 18 | 19 | // Setters/ Set Values 20 | void setReal(const int r){ 21 | real = r; 22 | } 23 | 24 | void setImg(const int i){ 25 | img = i; 26 | } 27 | 28 | // Getters / Get values 29 | int getReal() const { 30 | return real; 31 | } 32 | 33 | int getImg() const { 34 | return img; 35 | } 36 | 37 | void print(){ 38 | if(img>0){ 39 | cout << real << " + " << img << "i" << endl; 40 | }else{ 41 | cout << real << " - " << img << "i" << endl; 42 | } 43 | } 44 | 45 | void add(const Complex &x){ 46 | real += x.real; 47 | img += x.img; 48 | } 49 | 50 | void operator+(const Complex &x){ 51 | real += x.real; 52 | img += x.img; 53 | } 54 | 55 | void operator!(){ 56 | img *= -1; 57 | } 58 | 59 | int operator[](string s){ 60 | if(s=="real"){ 61 | return real; 62 | }else{ 63 | return img; 64 | } 65 | } 66 | 67 | }; 68 | 69 | istream& operator>>(istream& is,Complex &c){ 70 | int r1,i1; 71 | is >> r1 >> i1; 72 | c.setReal(r1); 73 | c.setImg(i1); 74 | return is; 75 | } 76 | 77 | ostream& operator<<(ostream& os,Complex &c){ 78 | c.print(); 79 | return os; 80 | } 81 | 82 | 83 | 84 | int main(){ 85 | Complex c1(5,3); 86 | 87 | Complex c2; 88 | c2.setReal(1); 89 | c2.setImg(4); 90 | 91 | c1.print(); 92 | c2.print(); 93 | 94 | cout << "*************Add***************" << endl; 95 | // c1.add(c2); 96 | // c1.print(); 97 | c1+c2; 98 | c1.print(); 99 | cout << "*************Negation***************" << endl; 100 | !c1; 101 | c1.print(); 102 | cout << "*************Square Brackets and Getters usage***************" << endl; 103 | cout << c1["real"] << " or " << c1.getReal() << endl; 104 | 105 | 106 | Complex d1,d2; 107 | cin >> d1 >> d2; 108 | cout << d1 << d2; 109 | return 0; 110 | } -------------------------------------------------------------------------------- /Day-6-7/04-Sorting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool compare(int a,int b){ 6 | return a0;j--){ 13 | for(int i=0;i=e){ 98 | return; 99 | } 100 | int p = partition(arr,s,e); 101 | quick_sort(arr,s,p-1); 102 | quick_sort(arr,p+1,e); 103 | } 104 | 105 | 106 | 107 | void display(int arr[],int n){ 108 | for(int i=0;i> n; 120 | 121 | int arr[n]; 122 | for(int i=0;i> arr[i]; 124 | } 125 | 126 | // bubble_sort(arr,n,compare); 127 | // merge_sort(arr,0,n-1); 128 | quick_sort(arr,0,n-1); 129 | display(arr,n); 130 | 131 | return 0; 132 | } -------------------------------------------------------------------------------- /Day-8/01-linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node* next; 9 | 10 | node(int d){ 11 | data = d; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | 17 | void print(node *head){ 18 | while(head != NULL){ 19 | cout << head->data << " -> "; 20 | head = head->next; 21 | } 22 | cout << " NULL "<< endl; 23 | } 24 | 25 | int length(node *head){ 26 | int count = 0; 27 | while(head!=NULL){ 28 | head = head->next; 29 | count++; 30 | } 31 | return count; 32 | } 33 | 34 | void insertAtHead(node *&head,int val){ 35 | if(head==NULL){ 36 | head = new node(val); 37 | return; 38 | } 39 | node* temp = new node(val); 40 | temp->next = head; 41 | // (*temp).next = head; 42 | head=temp; 43 | } 44 | 45 | void insertAtTail(node *&head, int val){ 46 | // List is empty 47 | if(head==NULL){ 48 | head = new node(val); 49 | return; 50 | } 51 | // List is not empty 52 | node *curr = head; 53 | while(curr->next != NULL){ 54 | curr = curr -> next; 55 | } 56 | node *temp = new node(val); 57 | curr->next = temp; 58 | } 59 | 60 | void insertAtMiddle(node *&head,int pos, int val){ 61 | if(head == NULL || pos<=0){ 62 | insertAtHead(head,val); 63 | return; 64 | }else if(pos>length(head)){ 65 | insertAtTail(head,val); 66 | return; 67 | } 68 | 69 | // Pos -1 jumps 70 | node* curr = head; 71 | for(int i=0;i next; 73 | } 74 | node* temp = new node(val); 75 | temp->next = curr -> next; 76 | curr->next = temp; 77 | } 78 | 79 | 80 | void deleteAtHead(node *&head){ 81 | if(head==NULL){ 82 | return; 83 | } 84 | node *temp = head; 85 | head = head->next; 86 | delete temp; 87 | } 88 | 89 | void deleteAtTail(node *&head){ 90 | node *curr = head; 91 | while(curr->next->next != NULL){ 92 | curr = curr->next; 93 | } 94 | node *temp = curr->next; 95 | curr -> next = NULL; 96 | delete temp; 97 | } 98 | 99 | void deleteAtMiddle(node *&head,int pos){ 100 | if(head==NULL){ 101 | return; 102 | }else if(pos>length(head)){ 103 | deleteAtTail(head); 104 | return; 105 | }else if(pos<=0){ 106 | deleteAtHead(head); 107 | return; 108 | } 109 | node *curr = head; 110 | for(int i=0;i next; 112 | } 113 | node *temp = curr->next; 114 | curr->next = temp->next; 115 | delete temp; 116 | } 117 | 118 | bool search(node *head,int key){ 119 | node *curr =head; 120 | while(curr != NULL){ 121 | if(curr->data == key) return 1; 122 | curr = curr->next; 123 | } 124 | return 0; 125 | } 126 | 127 | bool searchRecursive(node *head,int key){ 128 | if(head==NULL) return 0; 129 | if(head->data==key) return 1; 130 | return searchRecursive(head->next,key); 131 | } 132 | 133 | void reverseList(node *&head){ 134 | node *prev = NULL, *curr = head, *fwd=head; 135 | while(curr != NULL){ 136 | fwd = fwd->next; 137 | curr->next = prev; 138 | prev = curr; 139 | curr = fwd; 140 | } 141 | head = prev; 142 | } 143 | 144 | int main(){ 145 | node *head = NULL; 146 | insertAtHead(head,3); 147 | insertAtHead(head,2); 148 | insertAtHead(head,1); 149 | insertAtTail(head,5); 150 | insertAtMiddle(head,3,4); 151 | print(head); 152 | cout << "Length is " << length(head) << endl; 153 | if(search(head,6)){ 154 | cout << "Element is available"<< endl; 155 | }else{ 156 | cout << "Element is not available" << endl; 157 | } 158 | 159 | reverseList(head); 160 | print(head); 161 | return 0; 162 | } -------------------------------------------------------------------------------- /Day-13/02-Binary_search_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | // left <= root < right 9 | 10 | class node{ 11 | public: 12 | int data; 13 | node *left = NULL; 14 | node *right = NULL; 15 | node(int d){ 16 | data=d; 17 | left=NULL; 18 | right=NULL; 19 | } 20 | }; 21 | 22 | 23 | node* insertInBST(node *root,int data){ 24 | // Base case 25 | if(root==NULL){ 26 | return new node(data); 27 | } 28 | 29 | // Rec case - Insert in the subtree and update the pointers 30 | if(data<=root->data){ 31 | root->left = insertInBST(root->left,data); 32 | }else{ 33 | root->right = insertInBST(root->right,data); 34 | } 35 | return root; 36 | } 37 | 38 | // 5 2 3 4 8 9 -1 39 | node* build(){ 40 | int d; 41 | cin >> d; 42 | node *root = NULL; 43 | while(d!=-1){ 44 | root = insertInBST(root,d); 45 | cin >> d; 46 | } 47 | return root; 48 | } 49 | 50 | 51 | bool search(node *root,int data){ 52 | if(root==NULL){ 53 | return false; 54 | } 55 | if(root->data==data){ 56 | return true; 57 | } 58 | 59 | // Recursively search in left and right 60 | if(datadata){ 61 | return search(root->left,data); 62 | }else{ 63 | return search(root->right,data); 64 | } 65 | } 66 | 67 | void bfs(node *root){ 68 | queue q; 69 | 70 | q.push(root); 71 | q.push(NULL); 72 | 73 | while(!q.empty()){ 74 | node *f = q.front(); 75 | if(f==NULL){ 76 | cout << endl; 77 | q.pop(); 78 | if(!q.empty()){ 79 | q.push(NULL); 80 | } 81 | }else{ 82 | cout << f->data << " "; 83 | q.pop(); 84 | if(f->left){ 85 | q.push(f->left); 86 | } 87 | if(f->right){ 88 | q.push(f->right); 89 | } 90 | } 91 | } 92 | return; 93 | } 94 | 95 | 96 | // Preorder Traversal 97 | void printPreorder(node *root){ 98 | if(root==NULL) return; 99 | // Root Left Right 100 | cout << root->data << " "; 101 | printPreorder(root->left); 102 | printPreorder(root->right); 103 | } 104 | 105 | // Inorder Traversal 106 | void printInorder(node *root){ 107 | if(root==NULL){ 108 | return; 109 | } 110 | // Left Root Right 111 | printInorder(root->left); 112 | cout << root->data << " "; 113 | printInorder(root->right); 114 | } 115 | 116 | // Postorder Traversal 117 | void printPostorder(node *root){ 118 | if(root==NULL){ 119 | return; 120 | } 121 | // Left Right Root 122 | printPostorder(root->left); 123 | printPostorder(root->right); 124 | cout << root->data << " "; 125 | } 126 | 127 | 128 | 129 | bool isBST(node *root,int minV = INT_MIN, int maxV=INT_MAX ){ 130 | if(root==NULL){ 131 | return true; 132 | } 133 | 134 | if(root->data >= minV && root->data <= maxV && isBST(root->left,minV,root->data) && isBST(root->right,root->data,maxV)){ 135 | return true; 136 | } 137 | 138 | return false; 139 | } 140 | 141 | 142 | 143 | node* deleteInBST(node* root, int data){ 144 | if(root==NULL){ 145 | return NULL; 146 | }else if(datadata){ 147 | root->left = deleteInBST(root->left,data); 148 | return root; 149 | }else if(data==root->data){ 150 | /* 151 | 3 cases in BST when you got the data 152 | 1.) Node with no child/leaf node 153 | 2.) Node with a single child 154 | 3.) Node with 2 children 155 | */ 156 | 157 | // 1.) Node with no child/leaf node 158 | if(root->left==NULL && root->right==NULL){ 159 | delete root; 160 | return NULL; 161 | } 162 | 163 | // 2.) Node with a single child 164 | if(root->left!=NULL && root->right==NULL){ 165 | node *temp = root->left; 166 | delete root; 167 | return temp; 168 | } 169 | if(root->left==NULL && root->right !=NULL){ 170 | node *temp = root->right; 171 | delete root; 172 | return temp; 173 | } 174 | 175 | // 3.) Node with 2 children 176 | node* replace = root->right; 177 | while(replace->left !=NULL){ 178 | replace = replace->left; 179 | } 180 | 181 | root->data = replace->data; 182 | root->right = deleteInBST(root->right,replace->data); 183 | return root; 184 | 185 | }else{ 186 | root->right = deleteInBST(root->right,data); 187 | return root; 188 | } 189 | } 190 | 191 | 192 | 193 | 194 | 195 | 196 | int main(){ 197 | node *root = build(); 198 | printInorder(root); 199 | cout << endl; 200 | if(isBST(root)){ 201 | cout << "It is a valid BST" << endl; 202 | }else{ 203 | cout << "Not a BST" << endl; 204 | } 205 | 206 | cout << "BFS of a binary search tree is: " << endl; 207 | bfs(root); 208 | 209 | return 0; 210 | } -------------------------------------------------------------------------------- /Day-13/01-Binary_tree_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class node{ 7 | public: 8 | int data; 9 | node *left = NULL; 10 | node *right = NULL; 11 | node(int d){ 12 | data=d; 13 | } 14 | }; 15 | // 10 16 | // NULL NULL 17 | // 10 -1 -1 18 | // 1 19 | // 2 3 20 | // 4 5 -1 -1 21 | //-1 -1 -1 -1 22 | 23 | node* buildTree(){ 24 | int d; 25 | cin >> d; 26 | 27 | // Base case 28 | if(d==-1){ 29 | return NULL; 30 | } 31 | node *root = new node(d); 32 | root->left = buildTree(); 33 | root->right = buildTree(); 34 | 35 | return root; 36 | } 37 | 38 | // Preorder Traversal 39 | void printPreorder(node *root){ 40 | if(root==NULL) return; 41 | // Root Left Right 42 | cout << root->data << " "; 43 | printPreorder(root->left); 44 | printPreorder(root->right); 45 | } 46 | 47 | // Inorder Traversal 48 | void printInorder(node *root){ 49 | if(root==NULL){ 50 | return; 51 | } 52 | // Left Root Right 53 | printInorder(root->left); 54 | cout << root->data << " "; 55 | printInorder(root->right); 56 | } 57 | 58 | // Postorder Traversal 59 | void printPostorder(node *root){ 60 | if(root==NULL){ 61 | return; 62 | } 63 | // Left Right Root 64 | printPostorder(root->left); 65 | printPostorder(root->right); 66 | cout << root->data << " "; 67 | } 68 | 69 | 70 | int h=0; 71 | void calculateHeight(node *root,int depth){ 72 | //Base case 73 | if(root==NULL){ 74 | h = max(h,depth); 75 | return; 76 | } 77 | 78 | calculateHeight(root->left,depth+1); 79 | calculateHeight(root->right,depth+1); 80 | 81 | } 82 | 83 | 84 | int height(node *root){ 85 | // Base case 86 | if(root==NULL){ 87 | return 0; 88 | } 89 | int h1 = height(root->left); 90 | int h2 = height(root->right); 91 | return max(h1,h2)+1; 92 | 93 | } 94 | 95 | 96 | void printNthLevel(node *root,int n){ 97 | //Base case 1 98 | if(root==NULL){ 99 | return; 100 | } 101 | 102 | // Base case 2 103 | if(n==1){ 104 | cout << root->data << " "; 105 | return; 106 | } 107 | 108 | printNthLevel(root->left,n-1); 109 | printNthLevel(root->right,n-1); 110 | } 111 | 112 | 113 | 114 | void printAllLevels(node *root){ 115 | int h = height(root); 116 | for(int i=1;i<=h;i++){ 117 | printNthLevel(root,i); 118 | cout << endl; 119 | } 120 | } 121 | 122 | // Count all the nodes in a tree 123 | int count(node *root){ 124 | if(root==NULL){ 125 | return 0; 126 | } 127 | return 1+count(root->left)+count(root->right); 128 | } 129 | 130 | void mirror(node *root){ 131 | if(root==NULL){ 132 | return; 133 | } 134 | swap(root->left,root->right); 135 | mirror(root->left); 136 | mirror(root->right); 137 | } 138 | 139 | 140 | void bfs(node *root){ 141 | queue q; 142 | 143 | q.push(root); 144 | q.push(NULL); 145 | 146 | while(!q.empty()){ 147 | node *f = q.front(); 148 | if(f==NULL){ 149 | cout << endl; 150 | q.pop(); 151 | if(!q.empty()){ 152 | q.push(NULL); 153 | } 154 | }else{ 155 | cout << f->data << " "; 156 | q.pop(); 157 | if(f->left){ 158 | q.push(f->left); 159 | } 160 | if(f->right){ 161 | q.push(f->right); 162 | } 163 | } 164 | } 165 | return; 166 | } 167 | 168 | 169 | node* buildTreeFromArray(int *a,int s, int e){ 170 | if(s>e){ 171 | return NULL; 172 | } 173 | 174 | int mid = (s+e)/2; 175 | node *root = new node(a[mid]); 176 | root->left = buildTreeFromArray(a,s,mid-1); 177 | root->right = buildTreeFromArray(a,mid+1,e); 178 | return root; 179 | } 180 | 181 | 182 | int main(){ 183 | // node *root = new node(1); 184 | // root->left = new node(2); 185 | // root->right = new node(3); 186 | // root->left->left = new node(4); 187 | // root->left->right = new node(5); 188 | 189 | node *root = buildTree(); 190 | cout << "Preorder traversal: "; 191 | printPreorder(root); 192 | cout << endl; 193 | cout << "Inorder traversal: "; 194 | printInorder(root); 195 | cout << endl; 196 | cout << "Postorder traversal: "; 197 | printPostorder(root); 198 | cout << endl; 199 | 200 | calculateHeight(root,0); 201 | cout << "Height of the tree is: " << h << endl; 202 | 203 | cout << "Nth level of a tree is: " << endl; 204 | printNthLevel(root,3); 205 | 206 | cout << endl << "Complete tree is: " << endl; 207 | printAllLevels(root); 208 | 209 | cout << "Total number of nodes are " << count(root) << endl; 210 | 211 | cout << endl << endl; 212 | mirror(root); 213 | cout << "All the elements \n"; 214 | printAllLevels(root); 215 | 216 | cout << "BFS on a binary tree is: " << endl; 217 | bfs(root); 218 | 219 | 220 | int a[] = {1,2,3,4,5,6,7,8}; 221 | int n = 8; 222 | node *root2 = buildTreeFromArray(a,0,n-1); 223 | cout << "New binary tree looks like " << endl; 224 | bfs(root2); 225 | 226 | 227 | 228 | return 0; 229 | } --------------------------------------------------------------------------------