├── Module 1 ├── .gitignore ├── O_N.cpp ├── O_N_logN.cpp ├── O_N_square.cpp ├── O_logN.cpp ├── O_sqrtN.cpp └── test.cpp ├── Module 10 ├── .gitignore ├── capacity_functions.cpp ├── cycle_detect.cpp ├── element_access.cpp ├── list_constructors.cpp ├── modifiers_functions.cpp ├── operations_functions.cpp ├── reverse_doubly_linked_list.cpp └── reverse_singly_lined_list.cpp ├── Module 11 ├── delete_node.cpp ├── linked_list_cycle.cpp ├── middle_linked_list_one.cpp ├── middle_linked_list_two.cpp ├── palindrom_linked_list.cpp ├── remove_duplicated_from_sorted_list.cpp └── reverse_list.cpp ├── Module 13 ├── .gitignore ├── stack_using_array.cpp ├── stack_using_linked_list.cpp ├── stack_using_list.cpp └── stl_stack.cpp ├── Module 14 ├── .gitignore ├── implement_queue_using_stack.cpp ├── implement_stack_using_queue.cpp ├── queue_using_doubly_linked_list.cpp ├── queue_using_list.cpp ├── queue_using_singly_linked_list.cpp └── stl_queue.cpp ├── Module 15 ├── backspace_string_compare.cpp ├── insert_element_at_bottom_of_stack.cpp ├── maximum_equal_stack_sum.cpp ├── reverse_queue.cpp ├── reverse_stack_using_recursion.cpp └── valid_parentheses.cpp ├── Module 17 ├── .gitignore ├── binary_tree_implement.cpp ├── postorder.cpp ├── postorder_traversal.cpp └── preorder_traversal.cpp ├── Module 18 ├── .gitignore ├── count_leaf_nodes.cpp ├── count_number_of_nodes.cpp ├── input_binary_tree.cpp ├── level_order_print.cpp └── max_height.cpp ├── Module 19 ├── diameter_of_binary_tree.cpp ├── is_node_present.cpp ├── left_view_of_a_binary_tree.cpp ├── node_level.cpp ├── reverse_level_order_traversal.cpp └── special_binary_tree.cpp ├── Module 2 ├── .gitignore ├── vector_access_and_iterators.cpp ├── vector_capacity.cpp ├── vector_erase.cpp ├── vector_initialization.cpp ├── vector_input.cpp ├── vector_insert.cpp ├── vector_modifiers.cpp ├── vector_of_string.cpp ├── vector_of_string_space.cpp └── vector_replace_and_find.cpp ├── Module 21 ├── .gitignore ├── convert_array_to_bst.cpp ├── insert_in_bst.cpp └── search_in_bst.cpp ├── Module 22 ├── .gitignore ├── delete_from_max_heap.cpp ├── delete_from_min_heap.cpp ├── insert_in_max_heap.cpp └── insert_in_min_heap.cpp ├── Module 23 ├── .gitignore ├── custom_compare_class.cpp ├── priority_queue.cpp ├── set.cpp ├── stl_map.cpp └── word_count.cpp ├── Module 3 ├── .gitignore ├── Y_Range_sum_query.cpp ├── Z_Binary_Search.cpp ├── binary_search.cpp └── prefix_sum_array.cpp ├── Module 5 ├── .gitignore ├── create_a_node.cpp ├── dynamic_node_create.cpp ├── make_constructor.cpp └── print_linked_list.cpp ├── Module 6 ├── .gitignore ├── delete_from_position.cpp ├── delete_head.cpp ├── handle_error.cpp ├── input_linked_list.cpp ├── insert_at_head.cpp ├── insert_at_position.cpp ├── insert_at_tail.cpp └── reference_of_a_pointer.cpp ├── Module 7 ├── .gitignore ├── delete.cpp ├── input_linked_list.cpp ├── insert.cpp ├── print_recersively.cpp ├── singly_linked_list.cpp ├── sort_linked_list.cpp └── tempCodeRunnerFile.cpp ├── Module 9 ├── .gitignore ├── delete_node.cpp ├── doubly_linked_list.cpp ├── input.cpp └── insert_at_position.cpp └── README.md /Module 1/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 1/O_N.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | int a[n]; 8 | for(int i=0;i>a[i]; 11 | } 12 | int s=0; 13 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++) // O(Nlog(N)) 8 | { 9 | int x=i; 10 | while(x>0) 11 | { 12 | int digit=x%10; 13 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | // for(int i=1;i<=n;i++) // O(N*N) 8 | // { 9 | // for(int j=1;j<=n;j++) 10 | // { 11 | // cout<<"hello"< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | // while(n>0) // O(logN) 8 | // { 9 | // int digit=n%10; 10 | // cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | // for(int i=1;i<=sqrt(n);i++) // O(sqrt(N)) 8 | // { 9 | // if(n%i==0) 10 | // { 11 | // cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; cin>>n; 6 | int a[n]; 7 | for(int i=0;i>a[i]; 9 | } 10 | for(int i=0;ia[j]) swap(a[i],a[j]); 13 | } 14 | } 15 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | list myList = {10, 20, 30}; 6 | // cout << myList.max_size(); 7 | // myList.clear(); 8 | myList.resize(2); 9 | myList.resize(5, 100); 10 | cout << myList.size() << endl; 11 | for (int val : myList) 12 | { 13 | cout << val << endl; 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Module 10/cycle_detect.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | int main() 15 | { 16 | Node *head = new Node(10); 17 | Node *a = new Node(20); 18 | Node *b = new Node(30); 19 | 20 | head->next = a; 21 | a->next = b; 22 | 23 | Node *slow = head; 24 | Node *fast = head; 25 | bool flag = false; 26 | while (fast != NULL && fast->next != NULL) 27 | { 28 | slow = slow->next; 29 | fast = fast->next->next; 30 | if (fast == slow) 31 | { 32 | flag = true; 33 | break; 34 | } 35 | } 36 | if (flag == true) 37 | cout << "Cycle Detected" << endl; 38 | else 39 | cout << "No Cycle" << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Module 10/element_access.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | list myList = {10, 20, 30, 40, 50}; 6 | // cout << myList.front(); 7 | // cout << myList.back(); 8 | cout << *next(myList.begin(), 3) << endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Module 10/list_constructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | // list myList; 6 | // list list2 = {1, 2, 3, 4, 5}; 7 | int a[5] = {10, 20, 30, 40, 50}; 8 | vector v = {100, 200, 300}; 9 | // list myList(10, 5); 10 | // list myList(list2); 11 | // list myList(a, a + 5); 12 | list myList(v.begin(), v.end()); 13 | 14 | // for (auto it = myList.begin(); it != myList.end(); it++) 15 | // { 16 | // cout << *it << endl; 17 | // } 18 | for (int val : myList) 19 | { 20 | cout << val << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Module 10/modifiers_functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | list myList = {10, 20, 30, 40, 30, 30, 70}; 6 | // list newList = {100, 200, 300}; 7 | // vector v = {60, 70, 80}; 8 | // myList.pop_back(); 9 | // myList.pop_back(); 10 | // myList.pop_front(); 11 | // myList.insert(next(myList.begin(), 3), 100); 12 | // myList.erase(next(myList.begin(), 2)); 13 | // myList.insert(next(myList.begin(), 3), v.begin(), v.end()); 14 | // myList.erase(next(myList.begin(), 2), next(myList.begin(), 5)); 15 | // replace(myList.begin(), myList.end(), 30, 100); 16 | 17 | // for (int val : myList) 18 | // { 19 | // cout << val << endl; 20 | // } 21 | 22 | auto it = find(myList.begin(), myList.end(), 60); 23 | if (it == myList.end()) 24 | { 25 | cout << "Not found"; 26 | } 27 | else 28 | { 29 | cout << "Found"; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Module 10/operations_functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | list myList = {20, 40, 30, 10, 50, 10, 10}; 6 | // myList.remove(10); 7 | // sort(myList.begin(), myList.end(),greater()); 8 | // myList.sort(); 9 | // myList.sort(greater()); 10 | // myList.unique(); 11 | myList.reverse(); 12 | for (int val : myList) 13 | { 14 | cout << val << endl; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Module 10/reverse_doubly_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node *prev; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->next = NULL; 13 | this->prev = NULL; 14 | } 15 | }; 16 | void print_normal(Node *head) 17 | { 18 | Node *tmp = head; 19 | while (tmp != NULL) 20 | { 21 | cout << tmp->val << " "; 22 | tmp = tmp->next; 23 | } 24 | cout << endl; 25 | } 26 | void print_reverse(Node *tail) 27 | { 28 | Node *tmp = tail; 29 | while (tmp != NULL) 30 | { 31 | cout << tmp->val << " "; 32 | tmp = tmp->prev; 33 | } 34 | cout << endl; 35 | } 36 | void reverse(Node *head, Node *tail) 37 | { 38 | Node *i = head; 39 | Node *j = tail; 40 | while (i != j && i->next != j) 41 | { 42 | swap(i->val, j->val); 43 | i = i->next; 44 | j = j->prev; 45 | } 46 | swap(i->val, j->val); 47 | } 48 | int main() 49 | { 50 | Node *head = new Node(10); 51 | // Node *a = new Node(20); 52 | Node *tail = head; 53 | 54 | // connection 55 | // head->next = a; 56 | // a->prev = head; 57 | // a->next = b; 58 | // b->prev = a; 59 | // b->next = c; 60 | // c->prev = b; 61 | 62 | reverse(head, tail); 63 | print_normal(head); 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /Module 10/reverse_singly_lined_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void print_recursion(Node *n) 15 | { 16 | // base case 17 | if (n == NULL) 18 | return; 19 | cout << n->val << " "; 20 | print_recursion(n->next); 21 | } 22 | void print_reverse(Node *n) 23 | { 24 | // base case 25 | if (n == NULL) 26 | return; 27 | 28 | print_reverse(n->next); 29 | cout << n->val << " "; 30 | } 31 | void print(Node *head) 32 | { 33 | Node *tmp = head; 34 | while (tmp != NULL) 35 | { 36 | cout << tmp->val << " "; 37 | tmp = tmp->next; 38 | } 39 | cout << endl; 40 | } 41 | void reverse(Node *&head, Node *cur) 42 | { 43 | if (cur->next == NULL) 44 | { 45 | head = cur; 46 | return; 47 | } 48 | reverse(head, cur->next); 49 | cur->next->next = cur; 50 | cur->next = NULL; 51 | } 52 | int main() 53 | { 54 | Node *head = new Node(10); 55 | Node *a = new Node(20); 56 | Node *b = new Node(30); 57 | Node *c = new Node(40); 58 | 59 | head->next = a; 60 | a->next = b; 61 | b->next = c; 62 | reverse(head, head); 63 | print(head); 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Module 11/delete_node.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void deleteNode(ListNode *node) 5 | { 6 | node->val = node->next->val; 7 | ListNode *deleteNode = node->next; 8 | node->next = node->next->next; 9 | delete deleteNode; 10 | } 11 | }; -------------------------------------------------------------------------------- /Module 11/linked_list_cycle.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool hasCycle(ListNode *head) 5 | { 6 | ListNode *slow = head; 7 | ListNode *fast = head; 8 | while (fast != NULL && fast->next != NULL) 9 | { 10 | slow = slow->next; 11 | fast = fast->next->next; 12 | if (slow == fast) 13 | { 14 | return true; 15 | } 16 | } 17 | return false; 18 | } 19 | }; -------------------------------------------------------------------------------- /Module 11/middle_linked_list_one.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int size(ListNode *head) 5 | { 6 | ListNode *tmp = head; 7 | int cnt = 0; 8 | while (tmp != NULL) 9 | { 10 | cnt++; 11 | tmp = tmp->next; 12 | } 13 | return cnt; 14 | } 15 | ListNode *middleNode(ListNode *head) 16 | { 17 | int sz = size(head); 18 | ListNode *tmp = head; 19 | for (int i = 1; i <= sz / 2; i++) 20 | { 21 | tmp = tmp->next; 22 | } 23 | return tmp; 24 | } 25 | }; -------------------------------------------------------------------------------- /Module 11/middle_linked_list_two.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | ListNode *middleNode(ListNode *head) 5 | { 6 | ListNode *slow = head; 7 | ListNode *fast = head; 8 | while (fast != NULL && fast->next != NULL) 9 | { 10 | slow = slow->next; 11 | fast = fast->next->next; 12 | } 13 | cout << "My " << slow->val << endl; 14 | return slow; 15 | } 16 | }; -------------------------------------------------------------------------------- /Module 11/palindrom_linked_list.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void insert_tail(ListNode *&head, ListNode *&tail, int val) 5 | { 6 | ListNode *newNode = new ListNode(val); 7 | if (head == NULL) 8 | { 9 | head = newNode; 10 | tail = newNode; 11 | return; 12 | } 13 | tail->next = newNode; 14 | tail = tail->next; 15 | } 16 | void reverse(ListNode *&head, ListNode *cur) 17 | { 18 | if (cur->next == NULL) 19 | { 20 | head = cur; 21 | return; 22 | } 23 | reverse(head, cur->next); 24 | cur->next->next = cur; 25 | cur->next = NULL; 26 | } 27 | void print_list(ListNode *head) 28 | { 29 | ListNode *tmp = head; 30 | while (tmp != NULL) 31 | { 32 | cout << tmp->val << " "; 33 | tmp = tmp->next; 34 | } 35 | } 36 | bool isPalindrome(ListNode *head) 37 | { 38 | ListNode *newHead = NULL; 39 | ListNode *newTail = NULL; 40 | ListNode *tmp = head; 41 | while (tmp != NULL) 42 | { 43 | insert_tail(newHead, newTail, tmp->val); 44 | tmp = tmp->next; 45 | } 46 | reverse(newHead, newHead); 47 | print_list(newHead); 48 | tmp = head; 49 | ListNode *tmp2 = newHead; 50 | while (tmp != NULL) 51 | { 52 | if (tmp->val != tmp2->val) 53 | { 54 | return false; 55 | } 56 | tmp = tmp->next; 57 | tmp2 = tmp2->next; 58 | } 59 | return true; 60 | } 61 | }; -------------------------------------------------------------------------------- /Module 11/remove_duplicated_from_sorted_list.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | ListNode *deleteDuplicates(ListNode *head) 5 | { 6 | if (head == NULL) 7 | return head; 8 | ListNode *tmp = head; 9 | while (tmp->next != NULL) 10 | { 11 | if (tmp->val == tmp->next->val) 12 | { 13 | ListNode *deleteNode = tmp->next; 14 | tmp->next = tmp->next->next; 15 | delete deleteNode; 16 | } 17 | if (tmp->next == NULL) 18 | break; 19 | else if (tmp->val != tmp->next->val) 20 | { 21 | tmp = tmp->next; 22 | } 23 | } 24 | return head; 25 | } 26 | }; -------------------------------------------------------------------------------- /Module 11/reverse_list.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void reverse(ListNode *&head, ListNode *cur) 5 | { 6 | if (cur->next == NULL) 7 | { 8 | head = cur; 9 | return; 10 | } 11 | reverse(head, cur->next); 12 | cur->next->next = cur; 13 | cur->next = NULL; 14 | } 15 | ListNode *reverseList(ListNode *head) 16 | { 17 | if (head == NULL) 18 | return head; 19 | reverse(head, head); 20 | return head; 21 | } 22 | }; -------------------------------------------------------------------------------- /Module 13/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 13/stack_using_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class myStack 4 | { 5 | public: 6 | vector v; 7 | void push(int val) 8 | { 9 | v.push_back(val); 10 | } 11 | void pop() 12 | { 13 | v.pop_back(); 14 | } 15 | int top() 16 | { 17 | return v.back(); 18 | } 19 | int size() 20 | { 21 | return v.size(); 22 | } 23 | bool empty() 24 | { 25 | if (v.size() == 0) 26 | return true; 27 | else 28 | return false; 29 | } 30 | }; 31 | int main() 32 | { 33 | myStack st; 34 | int n; 35 | cin >> n; 36 | for (int i = 0; i < n; i++) 37 | { 38 | int x; 39 | cin >> x; 40 | st.push(x); 41 | } 42 | while (st.empty() == false) 43 | { 44 | cout << st.top() << endl; 45 | st.pop(); 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Module 13/stack_using_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node *prev; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->next = NULL; 13 | this->prev = NULL; 14 | } 15 | }; 16 | class myStack 17 | { 18 | public: 19 | Node *head = NULL; 20 | Node *tail = NULL; 21 | int sz = 0; 22 | void push(int val) 23 | { 24 | sz++; 25 | Node *newNode = new Node(val); 26 | if (head == NULL) 27 | { 28 | head = newNode; 29 | tail = newNode; 30 | return; 31 | } 32 | newNode->prev = tail; 33 | tail->next = newNode; 34 | tail = tail->next; 35 | } 36 | void pop() 37 | { 38 | sz--; 39 | Node *deleteNode = tail; 40 | tail = tail->prev; 41 | if (tail == NULL) 42 | { 43 | head = NULL; 44 | } 45 | else 46 | { 47 | tail->next = NULL; 48 | } 49 | 50 | delete deleteNode; 51 | } 52 | int top() 53 | { 54 | return tail->val; 55 | } 56 | int size() 57 | { 58 | return sz; 59 | } 60 | bool empty() 61 | { 62 | if (sz == 0) 63 | return true; 64 | else 65 | return false; 66 | } 67 | }; 68 | int main() 69 | { 70 | myStack st; 71 | // st.pop(); 72 | cout << st.top() << endl; 73 | 74 | // int n; 75 | // cin >> n; 76 | // for (int i = 0; i < n; i++) 77 | // { 78 | // int x; 79 | // cin >> x; 80 | // st.push(x); 81 | // } 82 | // while (!st.empty()) 83 | // { 84 | // cout << st.top() << endl; 85 | // st.pop(); 86 | // } 87 | return 0; 88 | } -------------------------------------------------------------------------------- /Module 13/stack_using_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class myStack 4 | { 5 | public: 6 | list l; 7 | void push(int val) 8 | { 9 | l.push_back(val); 10 | } 11 | void pop() 12 | { 13 | l.pop_back(); 14 | } 15 | int top() 16 | { 17 | return l.back(); 18 | } 19 | int size() 20 | { 21 | return l.size(); 22 | } 23 | bool empty() 24 | { 25 | if (l.size() == 0) 26 | return true; 27 | else 28 | return false; 29 | } 30 | }; 31 | int main() 32 | { 33 | myStack st; 34 | int n; 35 | cin >> n; 36 | for (int i = 0; i < n; i++) 37 | { 38 | int x; 39 | cin >> x; 40 | st.push(x); 41 | } 42 | while (!st.empty()) 43 | { 44 | cout << st.top() << endl; 45 | st.pop(); 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Module 13/stl_stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | stack st; 6 | int n; 7 | cin >> n; 8 | for (int i = 0; i < n; i++) 9 | { 10 | int x; 11 | cin >> x; 12 | st.push(x); 13 | } 14 | while (!st.empty()) 15 | { 16 | cout << st.top() << endl; 17 | st.pop(); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Module 14/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 14/implement_queue_using_stack.cpp: -------------------------------------------------------------------------------- 1 | class MyQueue 2 | { 3 | public: 4 | stack st; 5 | MyQueue() 6 | { 7 | } 8 | 9 | void push(int x) 10 | { 11 | st.push(x); 12 | } 13 | 14 | int pop() 15 | { 16 | stack newSt; 17 | int last; 18 | while (!st.empty()) 19 | { 20 | int k = st.top(); 21 | st.pop(); 22 | if (st.empty()) 23 | { 24 | // last element 25 | last = k; 26 | break; 27 | } 28 | newSt.push(k); 29 | } 30 | while (!newSt.empty()) 31 | { 32 | st.push(newSt.top()); 33 | newSt.pop(); 34 | } 35 | return last; 36 | } 37 | 38 | int peek() 39 | { 40 | stack newSt; 41 | int last; 42 | while (!st.empty()) 43 | { 44 | int k = st.top(); 45 | st.pop(); 46 | if (st.empty()) 47 | { 48 | // last element 49 | last = k; 50 | } 51 | newSt.push(k); 52 | } 53 | while (!newSt.empty()) 54 | { 55 | st.push(newSt.top()); 56 | newSt.pop(); 57 | } 58 | return last; 59 | } 60 | 61 | bool empty() 62 | { 63 | return st.empty(); 64 | } 65 | }; 66 | 67 | /** 68 | * Your MyQueue object will be instantiated and called as such: 69 | * MyQueue* obj = new MyQueue(); 70 | * obj->push(x); 71 | * int param_2 = obj->pop(); 72 | * int param_3 = obj->peek(); 73 | * bool param_4 = obj->empty(); 74 | */ -------------------------------------------------------------------------------- /Module 14/implement_stack_using_queue.cpp: -------------------------------------------------------------------------------- 1 | class MyStack 2 | { 3 | public: 4 | queue q; 5 | MyStack() 6 | { 7 | } 8 | 9 | void push(int x) 10 | { 11 | q.push(x); 12 | } 13 | 14 | int pop() 15 | { 16 | queue newQ; 17 | int last; 18 | while (!q.empty()) 19 | { 20 | int k = q.front(); 21 | q.pop(); 22 | if (q.empty()) 23 | { 24 | // last element 25 | last = k; 26 | break; 27 | } 28 | newQ.push(k); 29 | } 30 | q = newQ; 31 | return last; 32 | } 33 | 34 | int top() 35 | { 36 | queue newQ; 37 | int last; 38 | while (!q.empty()) 39 | { 40 | int k = q.front(); 41 | q.pop(); 42 | if (q.empty()) 43 | { 44 | // last element 45 | last = k; 46 | } 47 | newQ.push(k); 48 | } 49 | q = newQ; 50 | return last; 51 | } 52 | 53 | bool empty() 54 | { 55 | return q.empty(); 56 | } 57 | }; 58 | 59 | /** 60 | * Your MyStack object will be instantiated and called as such: 61 | * MyStack* obj = new MyStack(); 62 | * obj->push(x); 63 | * int param_2 = obj->pop(); 64 | * int param_3 = obj->top(); 65 | * bool param_4 = obj->empty(); 66 | */ -------------------------------------------------------------------------------- /Module 14/queue_using_doubly_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node *prev; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->next = NULL; 13 | this->prev = NULL; 14 | } 15 | }; 16 | class myQueue 17 | { 18 | public: 19 | Node *head = NULL; 20 | Node *tail = NULL; 21 | int sz = 0; 22 | void push(int val) 23 | { 24 | sz++; 25 | Node *newNode = new Node(val); 26 | if (head == NULL) 27 | { 28 | head = newNode; 29 | tail = newNode; 30 | return; 31 | } 32 | tail->next = newNode; 33 | newNode->prev = tail; 34 | tail = tail->next; 35 | } 36 | void pop() 37 | { 38 | sz--; 39 | Node *deleteNode = head; 40 | head = head->next; 41 | if (head == NULL) 42 | { 43 | tail = NULL; 44 | delete deleteNode; 45 | return; 46 | } 47 | head->prev = NULL; 48 | delete deleteNode; 49 | } 50 | int front() 51 | { 52 | return head->val; 53 | } 54 | int size() 55 | { 56 | return sz; 57 | } 58 | bool empty() 59 | { 60 | if (sz == 0) 61 | return true; 62 | else 63 | return false; 64 | } 65 | }; 66 | int main() 67 | { 68 | myQueue q; 69 | int n; 70 | cin >> n; 71 | for (int i = 0; i < n; i++) 72 | { 73 | int x; 74 | cin >> x; 75 | q.push(x); 76 | } 77 | while (!q.empty()) 78 | { 79 | cout << q.front() << endl; 80 | q.pop(); 81 | } 82 | return 0; 83 | } -------------------------------------------------------------------------------- /Module 14/queue_using_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class myQueue 4 | { 5 | public: 6 | list l; 7 | void push(int val) 8 | { 9 | l.push_back(val); 10 | } 11 | void pop() 12 | { 13 | l.pop_front(); 14 | } 15 | int front() 16 | { 17 | return l.front(); 18 | } 19 | int size() 20 | { 21 | return l.size(); 22 | } 23 | bool empty() 24 | { 25 | return l.empty(); 26 | } 27 | }; 28 | int main() 29 | { 30 | myQueue q; 31 | int n; 32 | cin >> n; 33 | for (int i = 0; i < n; i++) 34 | { 35 | int x; 36 | cin >> x; 37 | q.push(x); 38 | } 39 | while (!q.empty()) 40 | { 41 | cout << q.front() << endl; 42 | q.pop(); 43 | } 44 | if (!q.empty()) 45 | { 46 | cout << q.front(); 47 | } 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Module 14/queue_using_singly_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | class myQueue 15 | { 16 | public: 17 | Node *head = NULL; 18 | Node *tail = NULL; 19 | int sz = 0; 20 | void push(int val) 21 | { 22 | sz++; 23 | Node *newNode = new Node(val); 24 | if (head == NULL) 25 | { 26 | head = newNode; 27 | tail = newNode; 28 | return; 29 | } 30 | tail->next = newNode; 31 | tail = tail->next; 32 | } 33 | void pop() 34 | { 35 | sz--; 36 | Node *deleteNode = head; 37 | head = head->next; 38 | delete deleteNode; 39 | if (head == NULL) 40 | { 41 | tail = NULL; 42 | } 43 | } 44 | int front() 45 | { 46 | return head->val; 47 | } 48 | int size() 49 | { 50 | return sz; 51 | } 52 | bool empty() 53 | { 54 | if (sz == 0) 55 | return true; 56 | else 57 | return false; 58 | } 59 | }; 60 | int main() 61 | { 62 | myQueue q; 63 | int n; 64 | cin >> n; 65 | for (int i = 0; i < n; i++) 66 | { 67 | int x; 68 | cin >> x; 69 | q.push(x); 70 | } 71 | while (!q.empty()) 72 | { 73 | cout << q.front() << endl; 74 | q.pop(); 75 | } 76 | return 0; 77 | } -------------------------------------------------------------------------------- /Module 14/stl_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | queue q; 6 | int n; 7 | cin >> n; 8 | for (int i = 0; i < n; i++) 9 | { 10 | int x; 11 | cin >> x; 12 | q.push(x); 13 | } 14 | cout << q.size() << endl; 15 | while (!q.empty()) 16 | { 17 | cout << q.front() << endl; 18 | q.pop(); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Module 15/backspace_string_compare.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool backspaceCompare(string s, string t) 5 | { 6 | stack s1, s2; 7 | for (char c : s) 8 | { 9 | if (c == '#') 10 | { 11 | if (!s1.empty()) 12 | s1.pop(); 13 | } 14 | else 15 | { 16 | s1.push(c); 17 | } 18 | } 19 | for (char c : t) 20 | { 21 | if (c == '#') 22 | { 23 | if (!s2.empty()) 24 | s2.pop(); 25 | } 26 | else 27 | { 28 | s2.push(c); 29 | } 30 | } 31 | return s1 == s2; 32 | } 33 | }; -------------------------------------------------------------------------------- /Module 15/insert_element_at_bottom_of_stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | stack pushAtBottom(stack &st, int x) 3 | { 4 | // Write your code here. 5 | stack newSt; 6 | while (!st.empty()) 7 | { 8 | newSt.push(st.top()); 9 | st.pop(); 10 | } 11 | newSt.push(x); 12 | while (!newSt.empty()) 13 | { 14 | st.push(newSt.top()); 15 | newSt.pop(); 16 | } 17 | return st; 18 | } 19 | -------------------------------------------------------------------------------- /Module 15/maximum_equal_stack_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int getSum(stack s) 3 | { 4 | int sum = 0; 5 | while (!s.empty()) 6 | { 7 | sum += s.top(); 8 | s.pop(); 9 | } 10 | return sum; 11 | } 12 | int maxSum(stack &s1, stack &s2, stack &s3) 13 | { 14 | // Write your code here 15 | int sum1 = getSum(s1); 16 | int sum2 = getSum(s2); 17 | int sum3 = getSum(s3); 18 | // cout<= sum2 && sum1 >= sum3) 24 | { 25 | sum1 -= s1.top(); 26 | s1.pop(); 27 | } 28 | else if (sum2 >= sum1 && sum2 >= sum3) 29 | { 30 | sum2 -= s2.top(); 31 | s2.pop(); 32 | } 33 | else 34 | { 35 | sum3 -= s3.top(); 36 | s3.pop(); 37 | } 38 | } 39 | return sum1; 40 | } -------------------------------------------------------------------------------- /Module 15/reverse_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | queue reverseQueue(queue q) 3 | { 4 | // Write your code here. 5 | stack s; 6 | while (!q.empty()) 7 | { 8 | s.push(q.front()); 9 | q.pop(); 10 | } 11 | while (!s.empty()) 12 | { 13 | q.push(s.top()); 14 | s.pop(); 15 | } 16 | return q; 17 | } 18 | -------------------------------------------------------------------------------- /Module 15/reverse_stack_using_recursion.cpp: -------------------------------------------------------------------------------- 1 | void reverseStack(stack &s) 2 | { 3 | // Write your code here 4 | if (s.empty()) 5 | return; 6 | int x = s.top(); 7 | s.pop(); 8 | reverseStack(s); 9 | // reverse hoye gse 10 | stack cp; 11 | while (!s.empty()) 12 | { 13 | cp.push(s.top()); 14 | s.pop(); 15 | } 16 | cp.push(x); 17 | while (!cp.empty()) 18 | { 19 | s.push(cp.top()); 20 | cp.pop(); 21 | } 22 | } -------------------------------------------------------------------------------- /Module 15/valid_parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isValid(string s) 5 | { 6 | stack st; 7 | for (char c : s) 8 | { 9 | if (c == '(' || c == '{' || c == '[') 10 | { 11 | st.push(c); 12 | } 13 | else 14 | { 15 | if (st.empty()) 16 | { 17 | return false; 18 | } 19 | else 20 | { 21 | if (c == ')' && st.top() == '(') 22 | { 23 | st.pop(); 24 | } 25 | else if (c == '}' && st.top() == '{') 26 | { 27 | st.pop(); 28 | } 29 | else if (c == ']' && st.top() == '[') 30 | { 31 | st.pop(); 32 | } 33 | else 34 | { 35 | return false; 36 | } 37 | } 38 | } 39 | } 40 | return st.empty(); 41 | } 42 | }; -------------------------------------------------------------------------------- /Module 17/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 17/binary_tree_implement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | int main() 17 | { 18 | Node *root = new Node(10); 19 | Node *a = new Node(20); 20 | Node *b = new Node(30); 21 | Node *c = new Node(40); 22 | Node *d = new Node(50); 23 | Node *e = new Node(60); 24 | Node *f = new Node(70); 25 | Node *g = new Node(80); 26 | Node *h = new Node(90); 27 | Node *i = new Node(100); 28 | 29 | // connection 30 | root->left = a; 31 | root->right = b; 32 | a->left = c; 33 | a->right = h; 34 | c->right = e; 35 | b->right = d; 36 | d->left = f; 37 | d->right = g; 38 | h->right = i; 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Module 17/postorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | void inorder(Node *root) 17 | { 18 | if (root == NULL) 19 | { 20 | return; 21 | } 22 | inorder(root->left); 23 | cout << root->val << " "; 24 | inorder(root->right); 25 | } 26 | int main() 27 | { 28 | Node *root = new Node(10); 29 | Node *a = new Node(20); 30 | Node *b = new Node(30); 31 | Node *c = new Node(40); 32 | Node *d = new Node(50); 33 | Node *e = new Node(60); 34 | Node *f = new Node(70); 35 | Node *g = new Node(80); 36 | Node *h = new Node(90); 37 | Node *i = new Node(100); 38 | 39 | // connection 40 | root->left = a; 41 | root->right = b; 42 | a->left = c; 43 | a->right = h; 44 | c->right = e; 45 | b->right = d; 46 | d->left = f; 47 | d->right = g; 48 | h->right = i; 49 | 50 | // call 51 | inorder(root); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Module 17/postorder_traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | void postorder(Node *root) 17 | { 18 | if (root == NULL) 19 | { 20 | return; 21 | } 22 | postorder(root->left); 23 | postorder(root->right); 24 | cout << root->val << " "; 25 | } 26 | int main() 27 | { 28 | Node *root = new Node(10); 29 | Node *a = new Node(20); 30 | Node *b = new Node(30); 31 | Node *c = new Node(40); 32 | Node *d = new Node(50); 33 | Node *e = new Node(60); 34 | Node *f = new Node(70); 35 | Node *g = new Node(80); 36 | Node *h = new Node(90); 37 | Node *i = new Node(100); 38 | 39 | // connection 40 | root->left = a; 41 | root->right = b; 42 | a->left = c; 43 | a->right = h; 44 | c->right = e; 45 | b->right = d; 46 | d->left = f; 47 | d->right = g; 48 | h->right = i; 49 | 50 | // call 51 | postorder(root); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Module 17/preorder_traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | void preorder(Node *root) 17 | { 18 | if (root == NULL) 19 | { 20 | return; 21 | } 22 | cout << root->val << " "; 23 | preorder(root->left); 24 | preorder(root->right); 25 | } 26 | int main() 27 | { 28 | Node *root = new Node(10); 29 | Node *a = new Node(20); 30 | Node *b = new Node(30); 31 | Node *c = new Node(40); 32 | Node *d = new Node(50); 33 | Node *e = new Node(60); 34 | Node *f = new Node(70); 35 | Node *g = new Node(80); 36 | Node *h = new Node(90); 37 | Node *i = new Node(100); 38 | 39 | // connection 40 | root->left = a; 41 | root->right = b; 42 | a->left = c; 43 | a->right = h; 44 | c->right = e; 45 | b->right = d; 46 | d->left = f; 47 | d->right = g; 48 | h->right = i; 49 | 50 | // call 51 | preorder(root); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Module 18/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 18/count_leaf_nodes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | Node *input_tree() 17 | { 18 | int val; 19 | cin >> val; 20 | Node *root; 21 | if (val == -1) 22 | root = NULL; 23 | else 24 | root = new Node(val); 25 | queue q; 26 | if (root) 27 | q.push(root); 28 | while (!q.empty()) 29 | { 30 | // 1. ber kore ano 31 | Node *p = q.front(); 32 | q.pop(); 33 | 34 | // 2. jabotiyo ja kaj ache 35 | int l, r; 36 | cin >> l >> r; 37 | Node *myLeft; 38 | Node *myRight; 39 | if (l == -1) 40 | myLeft = NULL; 41 | else 42 | myLeft = new Node(l); 43 | if (r == -1) 44 | myRight = NULL; 45 | else 46 | myRight = new Node(r); 47 | 48 | p->left = myLeft; 49 | p->right = myRight; 50 | 51 | // 3. children gulo ke push koro 52 | if (p->left) 53 | q.push(p->left); 54 | if (p->right) 55 | q.push(p->right); 56 | } 57 | return root; 58 | } 59 | int count_leaf(Node *root) 60 | { 61 | if (root == NULL) 62 | return 0; 63 | if (root->left == NULL && root->right == NULL) 64 | { 65 | return 1; 66 | } 67 | else 68 | { 69 | int l = count_leaf(root->left); 70 | int r = count_leaf(root->right); 71 | return l + r; 72 | } 73 | } 74 | int main() 75 | { 76 | Node *root = input_tree(); 77 | cout << count_leaf(root) << endl; 78 | return 0; 79 | } -------------------------------------------------------------------------------- /Module 18/count_number_of_nodes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | Node *input_tree() 17 | { 18 | int val; 19 | cin >> val; 20 | Node *root; 21 | if (val == -1) 22 | root = NULL; 23 | else 24 | root = new Node(val); 25 | queue q; 26 | if (root) 27 | q.push(root); 28 | while (!q.empty()) 29 | { 30 | // 1. ber kore ano 31 | Node *p = q.front(); 32 | q.pop(); 33 | 34 | // 2. jabotiyo ja kaj ache 35 | int l, r; 36 | cin >> l >> r; 37 | Node *myLeft; 38 | Node *myRight; 39 | if (l == -1) 40 | myLeft = NULL; 41 | else 42 | myLeft = new Node(l); 43 | if (r == -1) 44 | myRight = NULL; 45 | else 46 | myRight = new Node(r); 47 | 48 | p->left = myLeft; 49 | p->right = myRight; 50 | 51 | // 3. children gulo ke push koro 52 | if (p->left) 53 | q.push(p->left); 54 | if (p->right) 55 | q.push(p->right); 56 | } 57 | return root; 58 | } 59 | int count(Node *root) 60 | { 61 | if (root == NULL) 62 | return 0; 63 | int l = count(root->left); 64 | int r = count(root->right); 65 | return l + r + 1; 66 | } 67 | int main() 68 | { 69 | Node *root = input_tree(); 70 | cout << count(root) << endl; 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Module 18/input_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | Node *input_tree() 17 | { 18 | int val; 19 | cin >> val; 20 | Node *root; 21 | if (val == -1) 22 | root = NULL; 23 | else 24 | root = new Node(val); 25 | queue q; 26 | if (root) 27 | q.push(root); 28 | while (!q.empty()) 29 | { 30 | // 1. ber kore ano 31 | Node *p = q.front(); 32 | q.pop(); 33 | 34 | // 2. jabotiyo ja kaj ache 35 | int l, r; 36 | cin >> l >> r; 37 | Node *myLeft; 38 | Node *myRight; 39 | if (l == -1) 40 | myLeft = NULL; 41 | else 42 | myLeft = new Node(l); 43 | if (r == -1) 44 | myRight = NULL; 45 | else 46 | myRight = new Node(r); 47 | 48 | p->left = myLeft; 49 | p->right = myRight; 50 | 51 | // 3. children gulo ke push koro 52 | if (p->left) 53 | q.push(p->left); 54 | if (p->right) 55 | q.push(p->right); 56 | } 57 | return root; 58 | } 59 | void level_order(Node *root) 60 | { 61 | if (root == NULL) 62 | { 63 | cout << "Tree nai" << endl; 64 | return; 65 | } 66 | queue q; 67 | q.push(root); 68 | while (!q.empty()) 69 | { 70 | // 1. ber kore ana 71 | Node *f = q.front(); 72 | q.pop(); 73 | 74 | // 2. jabotiyo ja kaj ache 75 | cout << f->val << " "; 76 | 77 | // 3. tar children gulo ke rakha 78 | if (f->left) 79 | q.push(f->left); 80 | if (f->right) 81 | q.push(f->right); 82 | } 83 | } 84 | int main() 85 | { 86 | Node *root = input_tree(); 87 | level_order(root); 88 | return 0; 89 | } -------------------------------------------------------------------------------- /Module 18/level_order_print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | void level_order(Node *root) 17 | { 18 | if (root == NULL) 19 | { 20 | cout << "Tree nai" << endl; 21 | return; 22 | } 23 | queue q; 24 | q.push(root); 25 | while (!q.empty()) 26 | { 27 | // 1. ber kore ana 28 | Node *f = q.front(); 29 | q.pop(); 30 | 31 | // 2. jabotiyo ja kaj ache 32 | cout << f->val << " "; 33 | 34 | // 3. tar children gulo ke rakha 35 | if (f->left) 36 | q.push(f->left); 37 | if (f->right) 38 | q.push(f->right); 39 | } 40 | } 41 | int main() 42 | { 43 | Node *root = new Node(10); 44 | Node *a = new Node(20); 45 | Node *b = new Node(30); 46 | Node *c = new Node(40); 47 | Node *d = new Node(50); 48 | Node *e = new Node(60); 49 | Node *f = new Node(70); 50 | Node *g = new Node(80); 51 | Node *h = new Node(90); 52 | Node *i = new Node(100); 53 | 54 | // connection 55 | root->left = a; 56 | root->right = b; 57 | a->left = c; 58 | a->right = h; 59 | c->right = e; 60 | b->right = d; 61 | d->left = f; 62 | d->right = g; 63 | h->right = i; 64 | 65 | level_order(root); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Module 18/max_height.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | Node *input_tree() 17 | { 18 | int val; 19 | cin >> val; 20 | Node *root; 21 | if (val == -1) 22 | root = NULL; 23 | else 24 | root = new Node(val); 25 | queue q; 26 | if (root) 27 | q.push(root); 28 | while (!q.empty()) 29 | { 30 | // 1. ber kore ano 31 | Node *p = q.front(); 32 | q.pop(); 33 | 34 | // 2. jabotiyo ja kaj ache 35 | int l, r; 36 | cin >> l >> r; 37 | Node *myLeft; 38 | Node *myRight; 39 | if (l == -1) 40 | myLeft = NULL; 41 | else 42 | myLeft = new Node(l); 43 | if (r == -1) 44 | myRight = NULL; 45 | else 46 | myRight = new Node(r); 47 | 48 | p->left = myLeft; 49 | p->right = myRight; 50 | 51 | // 3. children gulo ke push koro 52 | if (p->left) 53 | q.push(p->left); 54 | if (p->right) 55 | q.push(p->right); 56 | } 57 | return root; 58 | } 59 | int maxHeight(Node *root) 60 | { 61 | if (root == NULL) 62 | return 0; 63 | int l = maxHeight(root->left); 64 | int r = maxHeight(root->right); 65 | return max(l, r) + 1; 66 | } 67 | int main() 68 | { 69 | Node *root = input_tree(); 70 | cout << maxHeight(root) << endl; 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Module 19/diameter_of_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the TreeNode class structure 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | TreeNode *left; 10 | TreeNode *right; 11 | 12 | TreeNode(T data) { 13 | this->data = data; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ************************************************************/ 20 | int mx = 0; 21 | int maxHeight(TreeNode *root) 22 | { 23 | if (root == NULL) 24 | return 0; 25 | int l = maxHeight(root->left); 26 | int r = maxHeight(root->right); 27 | int d = l + r; 28 | mx = max(mx, d); 29 | return max(l, r) + 1; 30 | } 31 | int diameterOfBinaryTree(TreeNode *root) 32 | { 33 | // Write Your Code Here. 34 | mx = 0; 35 | 36 | int l = maxHeight(root->left); 37 | int r = maxHeight(root->right); 38 | return max(mx, l + r); 39 | } 40 | -------------------------------------------------------------------------------- /Module 19/is_node_present.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the Binary Tree node structure 4 | 5 | template 6 | class BinaryTreeNode 7 | { 8 | public : 9 | T data; 10 | BinaryTreeNode *left; 11 | BinaryTreeNode *right; 12 | 13 | BinaryTreeNode(T data) 14 | { 15 | this -> data = data; 16 | left = NULL; 17 | right = NULL; 18 | } 19 | }; 20 | 21 | ************************************************************/ 22 | 23 | bool isNodePresent(BinaryTreeNode *root, int x) 24 | { 25 | // Write your code here 26 | // base case 27 | if (root == NULL) 28 | return false; 29 | if (root->data == x) 30 | return true; 31 | bool l = isNodePresent(root->left, x); 32 | bool r = isNodePresent(root->right, x); 33 | return (l || r); 34 | } -------------------------------------------------------------------------------- /Module 19/left_view_of_a_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /************************************************************ 3 | 4 | Following is the TreeNode class structure 5 | 6 | template 7 | class TreeNode { 8 | public: 9 | T data; 10 | TreeNode *left; 11 | TreeNode *right; 12 | 13 | TreeNode(T data) { 14 | this->data = data; 15 | left = NULL; 16 | right = NULL; 17 | } 18 | }; 19 | 20 | ************************************************************/ 21 | 22 | vector getLeftView(TreeNode *root) 23 | { 24 | // Write your code here 25 | bool frq[3005] = {false}; 26 | queue *, int>> q; 27 | if (root) 28 | q.push({root, 1}); 29 | vector v; 30 | while (!q.empty()) 31 | { 32 | pair *, int> pr = q.front(); 33 | q.pop(); 34 | TreeNode *node = pr.first; 35 | int level = pr.second; 36 | 37 | if (frq[level] == false) 38 | { 39 | v.push_back(node->data); 40 | frq[level] = true; 41 | } 42 | 43 | if (node->left) 44 | q.push({node->left, level + 1}); 45 | if (node->right) 46 | q.push({node->right, level + 1}); 47 | } 48 | return v; 49 | } 50 | -------------------------------------------------------------------------------- /Module 19/node_level.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /************************************************************ 3 | 4 | Following is the TreeNode class structure 5 | 6 | template 7 | class TreeNode { 8 | public: 9 | T val; 10 | bool isOriginal; 11 | TreeNode *left; 12 | TreeNode *right; 13 | 14 | TreeNode(T val) { 15 | this->val = val; 16 | left = NULL; 17 | right = NULL; 18 | } 19 | }; 20 | 21 | ************************************************************/ 22 | 23 | int nodeLevel(TreeNode *root, int searchedValue) 24 | { 25 | // Write your code here. 26 | queue *, int>> q; 27 | // q.push(make_pair(root,1)); 28 | q.push({root, 1}); 29 | while (!q.empty()) 30 | { 31 | pair *, int> pr = q.front(); 32 | TreeNode *node = pr.first; 33 | int level = pr.second; 34 | q.pop(); 35 | 36 | if (node->val == searchedValue) 37 | { 38 | return level; 39 | } 40 | if (node->left) 41 | { 42 | q.push({node->left, level + 1}); 43 | } 44 | if (node->right) 45 | { 46 | q.push({node->right, level + 1}); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Module 19/reverse_level_order_traversal.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the TreeNode class structure: 4 | 5 | template 6 | 7 | class TreeNode { 8 | public: 9 | T val; 10 | TreeNode *left; 11 | TreeNode *right; 12 | TreeNode(T val) { 13 | this->val = val; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ************************************************************/ 20 | #include 21 | vector reverseLevelOrder(TreeNode *root) 22 | { 23 | // Write your code here. 24 | vector v; 25 | queue *> q; 26 | if (root) 27 | q.push(root); 28 | while (!q.empty()) 29 | { 30 | TreeNode *node = q.front(); 31 | q.pop(); 32 | v.push_back(node->val); 33 | if (node->left) 34 | q.push(node->left); 35 | if (node->right) 36 | q.push(node->right); 37 | } 38 | reverse(v.begin(), v.end()); 39 | return v; 40 | } 41 | -------------------------------------------------------------------------------- /Module 19/special_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /************************************************************* 3 | 4 | Following is the Binary Tree node structure 5 | 6 | class BinaryTreeNode 7 | { 8 | public : 9 | T data; 10 | BinaryTreeNode *left; 11 | BinaryTreeNode *right; 12 | 13 | BinaryTreeNode(T data) { 14 | this -> data = data; 15 | left = NULL; 16 | right = NULL; 17 | } 18 | }; 19 | 20 | *************************************************************/ 21 | 22 | bool isSpecialBinaryTree(BinaryTreeNode *root) 23 | { 24 | // Write your code here. 25 | if (root->left == NULL && root->right == NULL) 26 | return true; 27 | if (root->left == NULL || root->right == NULL) 28 | return false; 29 | bool l = isSpecialBinaryTree(root->left); 30 | bool r = isSpecialBinaryTree(root->right); 31 | if (l == false || r == false) 32 | return false; 33 | return true; 34 | } 35 | -------------------------------------------------------------------------------- /Module 2/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 2/vector_access_and_iterators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | vector v={1,2,3,4,5}; 6 | // vector::iterator it; 7 | // cout< 2 | using namespace std; 3 | int main() 4 | { 5 | vector v; 6 | // cout< 2 | using namespace std; 3 | int main() 4 | { 5 | vectorv={1,2,3,4,5}; 6 | // v.erase(v.begin()+3); 7 | // v.erase(v.begin()+1,v.begin()+4); 8 | v.erase(v.begin()+1,v.end()-1); 9 | for(int x:v) 10 | { 11 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | // vector v; type 1 6 | // vector v(5); type 2 7 | // vector v(5,10); type 3 8 | // vector v2(5,100); type 4 9 | // vector v(v2); type 4 10 | 11 | // int a[6]={1,2,3,4,5,6}; 12 | // vector v(a,a+6); 13 | vector v={2,10,3}; 14 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | // vector v; 6 | // int n; 7 | // cin>>n; 8 | // for(int i=0;i>x; 12 | // v.push_back(x); 13 | // } 14 | // for(int val:v) 15 | // { 16 | // cout<>n; 22 | vector v(n); 23 | for(int i=0;i>v[i]; 26 | } 27 | for(int val: v) 28 | { 29 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | vectorv={1,2,3,4,5}; 6 | vectorv2={100,101,102,103}; 7 | v.insert(v.begin()+2,v2.begin(),v2.end()); 8 | for(int x: v) 9 | { 10 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | vector x={10,20,30,40}; 6 | x.pop_back(); 7 | x.pop_back(); 8 | for(int i=0;i v={1,2,3}; 13 | // v=x; // O(N) 14 | // for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | vector v; 8 | for(int i=0;i>s; 12 | v.push_back(s); 13 | } 14 | // for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | cin.ignore(); 8 | vector v(n); 9 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | // replace 6 | // vectorv={1,2,2,4,3,5,1,2,4,5,3,2}; 7 | // replace(v.begin(),v.end(),2,100); 8 | // for(int x:v) 9 | // { 10 | // cout<v={1,2,2,4,3,5,1,2,4,5,3,2}; 15 | auto it = find(v.begin(),v.end(),100); 16 | if(it == v.end()) cout<<"Not found"; 17 | else cout<<"Found"; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Module 21/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 21/convert_array_to_bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | Node *convert(int a[], int n, int l, int r) 17 | { 18 | if (l > r) 19 | return NULL; 20 | int mid = (l + r) / 2; 21 | Node *root = new Node(a[mid]); 22 | Node *leftRoot = convert(a, n, l, mid - 1); 23 | Node *rootRight = convert(a, n, mid + 1, r); 24 | root->left = leftRoot; 25 | root->right = rootRight; 26 | return root; 27 | } 28 | void level_order(Node *root) 29 | { 30 | if (root == NULL) 31 | { 32 | cout << "Tree nai" << endl; 33 | return; 34 | } 35 | queue q; 36 | q.push(root); 37 | while (!q.empty()) 38 | { 39 | // 1. ber kore ana 40 | Node *f = q.front(); 41 | q.pop(); 42 | 43 | // 2. jabotiyo ja kaj ache 44 | cout << f->val << " "; 45 | 46 | // 3. tar children gulo ke rakha 47 | if (f->left) 48 | q.push(f->left); 49 | if (f->right) 50 | q.push(f->right); 51 | } 52 | } 53 | int main() 54 | { 55 | int n; 56 | cin >> n; 57 | int a[n]; 58 | for (int i = 0; i < n; i++) 59 | cin >> a[i]; 60 | Node *root = convert(a, n, 0, n - 1); 61 | level_order(root); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Module 21/insert_in_bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | Node *input_tree() 17 | { 18 | int val; 19 | cin >> val; 20 | Node *root; 21 | if (val == -1) 22 | root = NULL; 23 | else 24 | root = new Node(val); 25 | queue q; 26 | if (root) 27 | q.push(root); 28 | while (!q.empty()) 29 | { 30 | // 1. ber kore ano 31 | Node *p = q.front(); 32 | q.pop(); 33 | 34 | // 2. jabotiyo ja kaj ache 35 | int l, r; 36 | cin >> l >> r; 37 | Node *myLeft; 38 | Node *myRight; 39 | if (l == -1) 40 | myLeft = NULL; 41 | else 42 | myLeft = new Node(l); 43 | if (r == -1) 44 | myRight = NULL; 45 | else 46 | myRight = new Node(r); 47 | 48 | p->left = myLeft; 49 | p->right = myRight; 50 | 51 | // 3. children gulo ke push koro 52 | if (p->left) 53 | q.push(p->left); 54 | if (p->right) 55 | q.push(p->right); 56 | } 57 | return root; 58 | } 59 | void level_order(Node *root) 60 | { 61 | if (root == NULL) 62 | { 63 | cout << "Tree nai" << endl; 64 | return; 65 | } 66 | queue q; 67 | q.push(root); 68 | while (!q.empty()) 69 | { 70 | // 1. ber kore ana 71 | Node *f = q.front(); 72 | q.pop(); 73 | 74 | // 2. jabotiyo ja kaj ache 75 | cout << f->val << " "; 76 | 77 | // 3. tar children gulo ke rakha 78 | if (f->left) 79 | q.push(f->left); 80 | if (f->right) 81 | q.push(f->right); 82 | } 83 | } 84 | void insert(Node *&root, int x) 85 | { 86 | if (root == NULL) 87 | { 88 | root = new Node(x); 89 | return; 90 | } 91 | if (x < root->val) 92 | { 93 | if (root->left == NULL) 94 | { 95 | root->left = new Node(x); 96 | } 97 | else 98 | { 99 | insert(root->left, x); 100 | } 101 | } 102 | else 103 | { 104 | if (root->right == NULL) 105 | { 106 | root->right = new Node(x); 107 | } 108 | else 109 | { 110 | insert(root->right, x); 111 | } 112 | } 113 | } 114 | int main() 115 | { 116 | Node *root = input_tree(); 117 | 118 | insert(root, 13); 119 | insert(root, 32); 120 | insert(root, 27); 121 | insert(root, 22); 122 | level_order(root); 123 | return 0; 124 | } -------------------------------------------------------------------------------- /Module 21/search_in_bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | Node *input_tree() 17 | { 18 | int val; 19 | cin >> val; 20 | Node *root; 21 | if (val == -1) 22 | root = NULL; 23 | else 24 | root = new Node(val); 25 | queue q; 26 | if (root) 27 | q.push(root); 28 | while (!q.empty()) 29 | { 30 | // 1. ber kore ano 31 | Node *p = q.front(); 32 | q.pop(); 33 | 34 | // 2. jabotiyo ja kaj ache 35 | int l, r; 36 | cin >> l >> r; 37 | Node *myLeft; 38 | Node *myRight; 39 | if (l == -1) 40 | myLeft = NULL; 41 | else 42 | myLeft = new Node(l); 43 | if (r == -1) 44 | myRight = NULL; 45 | else 46 | myRight = new Node(r); 47 | 48 | p->left = myLeft; 49 | p->right = myRight; 50 | 51 | // 3. children gulo ke push koro 52 | if (p->left) 53 | q.push(p->left); 54 | if (p->right) 55 | q.push(p->right); 56 | } 57 | return root; 58 | } 59 | void level_order(Node *root) 60 | { 61 | if (root == NULL) 62 | { 63 | cout << "Tree nai" << endl; 64 | return; 65 | } 66 | queue q; 67 | q.push(root); 68 | while (!q.empty()) 69 | { 70 | // 1. ber kore ana 71 | Node *f = q.front(); 72 | q.pop(); 73 | 74 | // 2. jabotiyo ja kaj ache 75 | cout << f->val << " "; 76 | 77 | // 3. tar children gulo ke rakha 78 | if (f->left) 79 | q.push(f->left); 80 | if (f->right) 81 | q.push(f->right); 82 | } 83 | } 84 | bool search(Node *root, int x) 85 | { 86 | if (root == NULL) 87 | return false; 88 | if (root->val == x) 89 | return true; 90 | if (x < root->val) 91 | return search(root->left, x); 92 | else 93 | return search(root->right, x); 94 | } 95 | int main() 96 | { 97 | Node *root = input_tree(); 98 | if (search(root, 100)) 99 | cout << "Yes, Found" << endl; 100 | else 101 | cout << "No, Not Found" << endl; 102 | return 0; 103 | } -------------------------------------------------------------------------------- /Module 22/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 22/delete_from_max_heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void insert_heap(vector &v, int x) 4 | { 5 | v.push_back(x); 6 | int cur_idx = v.size() - 1; 7 | while (cur_idx != 0) 8 | { 9 | int parent_idx = (cur_idx - 1) / 2; 10 | if (v[parent_idx] < v[cur_idx]) 11 | swap(v[parent_idx], v[cur_idx]); 12 | else 13 | break; 14 | cur_idx = parent_idx; 15 | } 16 | } 17 | void delete_heap(vector &v) 18 | { 19 | v[0] = v[v.size() - 1]; 20 | v.pop_back(); 21 | int cur = 0; 22 | while (true) 23 | { 24 | int left_idx = cur * 2 + 1; 25 | int right_idx = cur * 2 + 2; 26 | int last_idx = v.size() - 1; 27 | if (left_idx <= last_idx && right_idx <= last_idx) 28 | { 29 | // duitai ache 30 | if (v[left_idx] >= v[right_idx] && v[left_idx] > v[cur]) 31 | { 32 | swap(v[left_idx], v[cur]); 33 | cur = left_idx; 34 | } 35 | else if (v[right_idx] >= v[left_idx] && v[right_idx] > v[cur]) 36 | { 37 | swap(v[right_idx], v[cur]); 38 | cur = right_idx; 39 | } 40 | else 41 | { 42 | break; 43 | } 44 | } 45 | else if (left_idx <= last_idx) 46 | { 47 | // left ase 48 | if (v[left_idx] > v[cur]) 49 | { 50 | swap(v[left_idx], v[cur]); 51 | cur = left_idx; 52 | } 53 | else 54 | { 55 | break; 56 | } 57 | } 58 | else if (right_idx <= last_idx) 59 | { 60 | // right ase 61 | if (v[right_idx] > v[cur]) 62 | { 63 | swap(v[right_idx], v[cur]); 64 | cur = right_idx; 65 | } 66 | else 67 | { 68 | break; 69 | } 70 | } 71 | else 72 | { 73 | break; 74 | } 75 | } 76 | } 77 | void print_heap(vector v) 78 | { 79 | for (int val : v) 80 | cout << val << " "; 81 | cout << endl; 82 | } 83 | int main() 84 | { 85 | int n; 86 | cin >> n; 87 | vector v; 88 | for (int i = 0; i < n; i++) 89 | { 90 | int x; 91 | cin >> x; 92 | insert_heap(v, x); 93 | } 94 | delete_heap(v); 95 | 96 | print_heap(v); 97 | delete_heap(v); 98 | print_heap(v); 99 | 100 | return 0; 101 | } -------------------------------------------------------------------------------- /Module 22/delete_from_min_heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void insert_heap(vector &v, int x) 4 | { 5 | v.push_back(x); 6 | int cur_idx = v.size() - 1; 7 | while (cur_idx != 0) 8 | { 9 | int parent_idx = (cur_idx - 1) / 2; 10 | if (v[parent_idx] > v[cur_idx]) 11 | swap(v[parent_idx], v[cur_idx]); 12 | else 13 | break; 14 | cur_idx = parent_idx; 15 | } 16 | } 17 | void delete_heap(vector &v) 18 | { 19 | v[0] = v[v.size() - 1]; 20 | v.pop_back(); 21 | int cur = 0; 22 | while (true) 23 | { 24 | int left_idx = cur * 2 + 1; 25 | int right_idx = cur * 2 + 2; 26 | int last_idx = v.size() - 1; 27 | if (left_idx <= last_idx && right_idx <= last_idx) 28 | { 29 | // duitai ache 30 | if (v[left_idx] <= v[right_idx] && v[left_idx] < v[cur]) 31 | { 32 | swap(v[left_idx], v[cur]); 33 | cur = left_idx; 34 | } 35 | else if (v[right_idx] <= v[left_idx] && v[right_idx] < v[cur]) 36 | { 37 | swap(v[right_idx], v[cur]); 38 | cur = right_idx; 39 | } 40 | else 41 | { 42 | break; 43 | } 44 | } 45 | else if (left_idx <= last_idx) 46 | { 47 | // left ase 48 | if (v[left_idx] < v[cur]) 49 | { 50 | swap(v[left_idx], v[cur]); 51 | cur = left_idx; 52 | } 53 | else 54 | { 55 | break; 56 | } 57 | } 58 | else if (right_idx <= last_idx) 59 | { 60 | // right ase 61 | if (v[right_idx] < v[cur]) 62 | { 63 | swap(v[right_idx], v[cur]); 64 | cur = right_idx; 65 | } 66 | else 67 | { 68 | break; 69 | } 70 | } 71 | else 72 | { 73 | break; 74 | } 75 | } 76 | } 77 | void print_heap(vector v) 78 | { 79 | for (int val : v) 80 | cout << val << " "; 81 | cout << endl; 82 | } 83 | int main() 84 | { 85 | int n; 86 | cin >> n; 87 | vector v; 88 | for (int i = 0; i < n; i++) 89 | { 90 | int x; 91 | cin >> x; 92 | insert_heap(v, x); 93 | } 94 | print_heap(v); 95 | delete_heap(v); 96 | print_heap(v); 97 | 98 | return 0; 99 | } -------------------------------------------------------------------------------- /Module 22/insert_in_max_heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | vector v; 6 | int n; 7 | cin >> n; 8 | for (int i = 0; i < n; i++) 9 | { 10 | int x; 11 | cin >> x; 12 | v.push_back(x); 13 | int cur_idx = v.size() - 1; 14 | while (cur_idx != 0) 15 | { 16 | int parent_idx = (cur_idx - 1) / 2; 17 | if (v[parent_idx] < v[cur_idx]) 18 | swap(v[parent_idx], v[cur_idx]); 19 | else 20 | break; 21 | cur_idx = parent_idx; 22 | } 23 | } 24 | 25 | for (int val : v) 26 | cout << val << " "; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Module 22/insert_in_min_heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | vector v; 6 | int n; 7 | cin >> n; 8 | for (int i = 0; i < n; i++) 9 | { 10 | int x; 11 | cin >> x; 12 | v.push_back(x); 13 | int cur_idx = v.size() - 1; 14 | while (cur_idx != 0) 15 | { 16 | int parent_idx = (cur_idx - 1) / 2; 17 | if (v[parent_idx] > v[cur_idx]) 18 | swap(v[parent_idx], v[cur_idx]); 19 | else 20 | break; 21 | cur_idx = parent_idx; 22 | } 23 | } 24 | 25 | for (int val : v) 26 | cout << val << " "; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Module 23/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 23/custom_compare_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Student 4 | { 5 | public: 6 | string name; 7 | int roll; 8 | int marks; 9 | Student(string name, int roll, int marks) 10 | { 11 | this->marks = marks; 12 | this->roll = roll; 13 | this->name = name; 14 | } 15 | }; 16 | class cmp 17 | { 18 | public: 19 | bool operator()(Student a, Student b) 20 | { 21 | if (a.marks > b.marks) 22 | return true; 23 | else if (a.marks < b.marks) 24 | return false; 25 | else 26 | { 27 | if (a.roll > b.roll) 28 | return true; 29 | else 30 | return false; 31 | } 32 | } 33 | }; 34 | int main() 35 | { 36 | int n; 37 | cin >> n; 38 | priority_queue, cmp> pq; 39 | for (int i = 0; i < n; i++) 40 | { 41 | string name; 42 | int roll, marks; 43 | cin >> name >> roll >> marks; 44 | Student obj(name, roll, marks); 45 | pq.push(obj); 46 | } 47 | while (!pq.empty()) 48 | { 49 | cout << pq.top().name << " " << pq.top().roll << " " << pq.top().marks << endl; 50 | pq.pop(); 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Module 23/priority_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | priority_queue, greater> pq; 6 | while (true) 7 | { 8 | int c; 9 | cin >> c; 10 | if (c == 0) 11 | { 12 | int x; 13 | cin >> x; 14 | pq.push(x); // O(logN) 15 | } 16 | else if (c == 1) 17 | { 18 | pq.pop(); // O(logN) 19 | } 20 | else if (c == 2) 21 | { 22 | cout << pq.top() << endl; // O(1) 23 | } 24 | else 25 | { 26 | break; 27 | } 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Module 23/set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | set s; 6 | int n; 7 | cin >> n; 8 | while (n--) 9 | { 10 | int x; 11 | cin >> x; 12 | s.insert(x); // O(logN) 13 | } 14 | cout << s.count(100) << endl; // O(logN) 15 | if (s.count(1000)) 16 | cout << "YES" << endl; 17 | else 18 | cout << "NO" << endl; 19 | // for (auto it = s.begin(); it != s.end(); it++) 20 | // { 21 | // cout << *it << endl; 22 | // } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Module 23/stl_map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | map mp; 6 | mp["Sakib al hasn"] = 79; // O(logN) 7 | mp.insert({"akib", 100}); 8 | mp.insert({"akib", 150}); 9 | mp["tamim"] = 79; 10 | 11 | // for (auto it = mp.begin(); it != mp.end(); it++) 12 | // { 13 | // cout << it->first << " " << it->second << endl; // O(logN) 14 | // } 15 | 16 | cout << mp.count("akib") << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Module 23/word_count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | string sentence; 6 | getline(cin, sentence); 7 | string word; 8 | stringstream ss(sentence); 9 | map mp; 10 | while (ss >> word) 11 | { 12 | mp[word]++; 13 | } 14 | // for (auto it = mp.begin(); it != mp.end(); it++) 15 | // { 16 | // cout << it->first << " " << it->second << endl; 17 | // } 18 | cout << mp["She"] << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Module 3/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 3/Y_Range_sum_query.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n,q; 6 | cin>>n>>q; 7 | int a[n]; 8 | for(int i=0;i>a[i]; 11 | } 12 | 13 | while(q--) 14 | { 15 | int l,r; 16 | cin>>l>>r; 17 | l--; 18 | r--; 19 | int sum=0; 20 | for(int i=l;i<=r;i++) 21 | { 22 | sum+=a[i]; 23 | } 24 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n,q; 6 | cin>>n>>q; 7 | int a[n]; 8 | for(int i=0;i>a[i]; 11 | } 12 | sort(a,a+n); 13 | while(q--) 14 | { 15 | int x; 16 | cin>>x; 17 | bool flag=false; 18 | int l=0,r=n-1; 19 | 20 | // binary search 21 | while(l<=r) 22 | { 23 | int mid=(l+r)/2; 24 | if(a[mid] == x) 25 | { 26 | flag=true; 27 | break; 28 | } 29 | if(x>a[mid]) 30 | { 31 | // dane 32 | l=mid+1; 33 | } 34 | else 35 | { 36 | // bame 37 | r=mid-1; 38 | } 39 | } 40 | 41 | if(flag==true) cout<<"found"< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | int a[n]; 8 | for(int i=0;i>a[i]; 11 | } 12 | int x; 13 | cin>>x; 14 | int l=0; 15 | int r=n-1; 16 | bool flag=false; 17 | while(l<=r) 18 | { 19 | int mid_index=(l+r)/2; 20 | if(a[mid_index]==x) 21 | { 22 | flag=true; 23 | break; 24 | } 25 | if(x>a[mid_index]) 26 | { 27 | // dane jao 28 | l=mid_index+1; 29 | } 30 | else 31 | { 32 | // bame jao 33 | r=mid_index-1; 34 | } 35 | } 36 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | long long n,q; 6 | cin>>n>>q; 7 | long long a[n]; 8 | for(int i=0;i>a[i]; 11 | } 12 | long long pre[n]; 13 | pre[0]=a[0]; 14 | for(int i=1;i>l>>r; 23 | l--; 24 | r--; 25 | long long sum; 26 | if(l==0) sum=pre[r]; 27 | else sum=pre[r]-pre[l-1]; 28 | cout< 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node* next; 8 | }; 9 | int main() 10 | { 11 | Node a,b; 12 | 13 | a.val=10; 14 | b.val=20; 15 | 16 | a.next=&b; 17 | b.next=NULL; 18 | 19 | cout<val< 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node* next; 8 | Node(int val) 9 | { 10 | this->val=val; 11 | this->next=NULL; 12 | } 13 | }; 14 | int main() 15 | { 16 | // Node head(10); 17 | Node* head = new Node(10); 18 | Node* a = new Node(20); 19 | 20 | head->next = a; 21 | 22 | cout<val<val<next->val< 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node* next; 8 | Node(int val) 9 | { 10 | this->val=val; 11 | this->next=NULL; 12 | } 13 | }; 14 | int main() 15 | { 16 | Node a(10); 17 | Node b(20); 18 | 19 | a.next=&b; 20 | 21 | 22 | 23 | cout<val< 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node* next; 8 | Node(int val) 9 | { 10 | this->val=val; 11 | this->next=NULL; 12 | } 13 | }; 14 | int main() 15 | { 16 | Node* head = new Node(10); 17 | Node* a = new Node(20); 18 | Node* b = new Node(30); 19 | Node* c = new Node(40); 20 | Node* d = new Node(50); 21 | 22 | head->next = a; 23 | a->next = b; 24 | b->next = c; 25 | c->next = d; 26 | 27 | Node* tmp = head; 28 | while(tmp != NULL) 29 | { 30 | cout<val<next; 32 | } 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Module 6/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 6/delete_from_position.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_at_tail(Node *&head, int v) 15 | { 16 | Node *newNode = new Node(v); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | cout << endl 21 | << "Inserted at head" << endl 22 | << endl; 23 | return; 24 | } 25 | 26 | Node *tmp = head; 27 | while (tmp->next != NULL) 28 | { 29 | tmp = tmp->next; 30 | } 31 | // tmp ekhon last node e 32 | tmp->next = newNode; 33 | cout << endl 34 | << "Inserted at tail" << endl 35 | << endl; 36 | } 37 | void print_linked_list(Node *head) 38 | { 39 | cout << endl; 40 | cout << "Your Linked List: "; 41 | Node *tmp = head; 42 | while (tmp != NULL) 43 | { 44 | cout << tmp->val << " "; 45 | tmp = tmp->next; 46 | } 47 | cout << endl 48 | << endl; 49 | } 50 | void insert_at_position(Node *head, int pos, int v) 51 | { 52 | Node *newNode = new Node(v); 53 | Node *tmp = head; 54 | for (int i = 1; i <= pos - 1; i++) 55 | { 56 | tmp = tmp->next; 57 | } 58 | newNode->next = tmp->next; 59 | tmp->next = newNode; 60 | cout << endl 61 | << endl 62 | << "Inserted at position " << pos << endl 63 | << endl; 64 | } 65 | void insert_at_head(Node *&head, int v) 66 | { 67 | Node *newNode = new Node(v); 68 | newNode->next = head; 69 | head = newNode; 70 | cout << endl 71 | << "Inserted at head" << endl 72 | << endl; 73 | } 74 | void delete_from_position(Node *head, int pos) 75 | { 76 | Node *tmp = head; 77 | for (int i = 1; i <= pos - 1; i++) 78 | { 79 | tmp = tmp->next; 80 | } 81 | Node *deleteNode = tmp->next; 82 | tmp->next = tmp->next->next; 83 | delete deleteNode; 84 | } 85 | int main() 86 | { 87 | Node *head = NULL; 88 | while (true) 89 | { 90 | cout << "Option 1: Insert at Tail" << endl; 91 | cout << "Option 2: Print Linked List" << endl; 92 | cout << "Option 3: Insert at any Position" << endl; 93 | cout << "Option 4: Insert at Head" << endl; 94 | cout << "Option 5: Delete from Position" << endl; 95 | cout << "Option 6: Terminate" << endl; 96 | int op; 97 | cin >> op; 98 | if (op == 1) 99 | { 100 | cout << "Please enter value: "; 101 | int v; 102 | cin >> v; 103 | insert_at_tail(head, v); 104 | } 105 | else if (op == 2) 106 | { 107 | print_linked_list(head); 108 | } 109 | else if (op == 3) 110 | { 111 | int pos, v; 112 | cout << "Enter position: "; 113 | cin >> pos; 114 | cout << "Enter value: "; 115 | cin >> v; 116 | if (pos == 0) 117 | { 118 | insert_at_head(head, v); 119 | } 120 | else 121 | { 122 | insert_at_position(head, pos, v); 123 | } 124 | } 125 | else if (op == 4) 126 | { 127 | int v; 128 | cout << "Enter value: "; 129 | cin >> v; 130 | insert_at_head(head, v); 131 | } 132 | else if (op == 5) 133 | { 134 | int pos; 135 | cout << "Enter position: "; 136 | cin >> pos; 137 | delete_from_position(head, pos); 138 | } 139 | } 140 | return 0; 141 | } -------------------------------------------------------------------------------- /Module 6/delete_head.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_at_tail(Node *&head, int v) 15 | { 16 | Node *newNode = new Node(v); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | cout << endl 21 | << "Inserted at head" << endl 22 | << endl; 23 | return; 24 | } 25 | 26 | Node *tmp = head; 27 | while (tmp->next != NULL) 28 | { 29 | tmp = tmp->next; 30 | } 31 | // tmp ekhon last node e 32 | tmp->next = newNode; 33 | cout << endl 34 | << "Inserted at tail" << endl 35 | << endl; 36 | } 37 | void print_linked_list(Node *head) 38 | { 39 | cout << endl; 40 | cout << "Your Linked List: "; 41 | Node *tmp = head; 42 | while (tmp != NULL) 43 | { 44 | cout << tmp->val << " "; 45 | tmp = tmp->next; 46 | } 47 | cout << endl 48 | << endl; 49 | } 50 | void insert_at_position(Node *head, int pos, int v) 51 | { 52 | Node *newNode = new Node(v); 53 | Node *tmp = head; 54 | for (int i = 1; i <= pos - 1; i++) 55 | { 56 | tmp = tmp->next; 57 | } 58 | newNode->next = tmp->next; 59 | tmp->next = newNode; 60 | cout << endl 61 | << endl 62 | << "Inserted at position " << pos << endl 63 | << endl; 64 | } 65 | void insert_at_head(Node *&head, int v) 66 | { 67 | Node *newNode = new Node(v); 68 | newNode->next = head; 69 | head = newNode; 70 | cout << endl 71 | << "Inserted at head" << endl 72 | << endl; 73 | } 74 | void delete_from_position(Node *head, int pos) 75 | { 76 | Node *tmp = head; 77 | for (int i = 1; i <= pos - 1; i++) 78 | { 79 | tmp = tmp->next; 80 | } 81 | Node *deleteNode = tmp->next; 82 | tmp->next = tmp->next->next; 83 | delete deleteNode; 84 | cout << endl 85 | << "Deleted position " << pos << endl 86 | << endl; 87 | } 88 | void delete_head(Node *&head) 89 | { 90 | Node *deleteNode = head; 91 | head = head->next; 92 | delete deleteNode; 93 | cout << endl 94 | << "Deleted head" << endl 95 | << endl; 96 | } 97 | int main() 98 | { 99 | Node *head = NULL; 100 | while (true) 101 | { 102 | cout << "Option 1: Insert at Tail" << endl; 103 | cout << "Option 2: Print Linked List" << endl; 104 | cout << "Option 3: Insert at any Position" << endl; 105 | cout << "Option 4: Insert at Head" << endl; 106 | cout << "Option 5: Delete from Position" << endl; 107 | cout << "Option 6: Delete head" << endl; 108 | cout << "Option 7: Terminate" << endl; 109 | int op; 110 | cin >> op; 111 | if (op == 1) 112 | { 113 | cout << "Please enter value: "; 114 | int v; 115 | cin >> v; 116 | insert_at_tail(head, v); 117 | } 118 | else if (op == 2) 119 | { 120 | print_linked_list(head); 121 | } 122 | else if (op == 3) 123 | { 124 | int pos, v; 125 | cout << "Enter position: "; 126 | cin >> pos; 127 | cout << "Enter value: "; 128 | cin >> v; 129 | if (pos == 0) 130 | { 131 | insert_at_head(head, v); 132 | } 133 | else 134 | { 135 | insert_at_position(head, pos, v); 136 | } 137 | } 138 | else if (op == 4) 139 | { 140 | int v; 141 | cout << "Enter value: "; 142 | cin >> v; 143 | insert_at_head(head, v); 144 | } 145 | else if (op == 5) 146 | { 147 | int pos; 148 | cout << "Enter position: "; 149 | cin >> pos; 150 | if (pos == 0) 151 | { 152 | delete_head(head); 153 | } 154 | else 155 | { 156 | delete_from_position(head, pos); 157 | } 158 | } 159 | else if (op == 6) 160 | { 161 | delete_head(head); 162 | } 163 | else if (op == 7) 164 | { 165 | break; 166 | } 167 | } 168 | return 0; 169 | } -------------------------------------------------------------------------------- /Module 6/handle_error.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_at_tail(Node *&head, int v) 15 | { 16 | Node *newNode = new Node(v); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | cout << endl 21 | << "Inserted at head" << endl 22 | << endl; 23 | return; 24 | } 25 | 26 | Node *tmp = head; 27 | while (tmp->next != NULL) 28 | { 29 | tmp = tmp->next; 30 | } 31 | // tmp ekhon last node e 32 | tmp->next = newNode; 33 | cout << endl 34 | << "Inserted at tail" << endl 35 | << endl; 36 | } 37 | void print_linked_list(Node *head) 38 | { 39 | cout << endl; 40 | cout << "Your Linked List: "; 41 | Node *tmp = head; 42 | while (tmp != NULL) 43 | { 44 | cout << tmp->val << " "; 45 | tmp = tmp->next; 46 | } 47 | cout << endl 48 | << endl; 49 | } 50 | void insert_at_position(Node *head, int pos, int v) 51 | { 52 | Node *newNode = new Node(v); 53 | Node *tmp = head; 54 | for (int i = 1; i <= pos - 1; i++) 55 | { 56 | tmp = tmp->next; 57 | if (tmp == NULL) 58 | { 59 | cout << endl 60 | << "Invalid Index" << endl 61 | << endl; 62 | return; 63 | } 64 | } 65 | newNode->next = tmp->next; 66 | tmp->next = newNode; 67 | cout << endl 68 | << endl 69 | << "Inserted at position " << pos << endl 70 | << endl; 71 | } 72 | void insert_at_head(Node *&head, int v) 73 | { 74 | Node *newNode = new Node(v); 75 | newNode->next = head; 76 | head = newNode; 77 | cout << endl 78 | << "Inserted at head" << endl 79 | << endl; 80 | } 81 | void delete_from_position(Node *head, int pos) 82 | { 83 | Node *tmp = head; 84 | for (int i = 1; i <= pos - 1; i++) 85 | { 86 | tmp = tmp->next; 87 | if (tmp == NULL) 88 | { 89 | cout << endl 90 | << "Invalid Index" << endl 91 | << endl; 92 | return; 93 | } 94 | } 95 | if (tmp->next == NULL) 96 | { 97 | cout << endl 98 | << "Invalid Index" << endl 99 | << endl; 100 | return; 101 | } 102 | Node *deleteNode = tmp->next; 103 | tmp->next = tmp->next->next; 104 | delete deleteNode; 105 | cout << endl 106 | << "Deleted position " << pos << endl 107 | << endl; 108 | } 109 | void delete_head(Node *&head) 110 | { 111 | if (head == NULL) 112 | { 113 | cout << endl 114 | << "Head is not available" << endl 115 | << endl; 116 | return; 117 | } 118 | Node *deleteNode = head; 119 | head = head->next; 120 | delete deleteNode; 121 | cout << endl 122 | << "Deleted head" << endl 123 | << endl; 124 | } 125 | int main() 126 | { 127 | Node *head = NULL; 128 | while (true) 129 | { 130 | cout << "Option 1: Insert at Tail" << endl; // done 131 | cout << "Option 2: Print Linked List" << endl; // done 132 | cout << "Option 3: Insert at any Position" << endl; // done 133 | cout << "Option 4: Insert at Head" << endl; // done 134 | cout << "Option 5: Delete from Position" << endl; // done 135 | cout << "Option 7: Terminate" << endl; 136 | int op; 137 | cin >> op; 138 | if (op == 1) 139 | { 140 | cout << "Please enter value: "; 141 | int v; 142 | cin >> v; 143 | insert_at_tail(head, v); 144 | } 145 | else if (op == 2) 146 | { 147 | print_linked_list(head); 148 | } 149 | else if (op == 3) 150 | { 151 | int pos, v; 152 | cout << "Enter position: "; 153 | cin >> pos; 154 | cout << "Enter value: "; 155 | cin >> v; 156 | if (pos == 0) 157 | { 158 | insert_at_head(head, v); 159 | } 160 | else 161 | { 162 | insert_at_position(head, pos, v); 163 | } 164 | } 165 | else if (op == 4) 166 | { 167 | int v; 168 | cout << "Enter value: "; 169 | cin >> v; 170 | insert_at_head(head, v); 171 | } 172 | else if (op == 5) 173 | { 174 | int pos; 175 | cout << "Enter position: "; 176 | cin >> pos; 177 | if (pos == 0) 178 | { 179 | delete_head(head); 180 | } 181 | else 182 | { 183 | delete_from_position(head, pos); 184 | } 185 | } 186 | else if (op == 6) 187 | { 188 | delete_head(head); 189 | } 190 | else if (op == 7) 191 | { 192 | break; 193 | } 194 | } 195 | return 0; 196 | } -------------------------------------------------------------------------------- /Module 6/input_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_at_tail(Node *&head, int v) 15 | { 16 | Node *newNode = new Node(v); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | return; 21 | } 22 | 23 | Node *tmp = head; 24 | while (tmp->next != NULL) 25 | { 26 | tmp = tmp->next; 27 | } 28 | // tmp ekhon last node e 29 | tmp->next = newNode; 30 | } 31 | void print_linked_list(Node *head) 32 | { 33 | cout << endl; 34 | cout << "Your Linked List: "; 35 | Node *tmp = head; 36 | while (tmp != NULL) 37 | { 38 | cout << tmp->val << " "; 39 | tmp = tmp->next; 40 | } 41 | cout << endl 42 | << endl; 43 | } 44 | int main() 45 | { 46 | int val; 47 | Node *head = NULL; 48 | while (true) 49 | { 50 | cin >> val; 51 | if (val == -1) 52 | break; 53 | insert_at_tail(head, val); 54 | } 55 | print_linked_list(head); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Module 6/insert_at_head.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_at_tail(Node *&head, int v) 15 | { 16 | Node *newNode = new Node(v); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | cout << endl 21 | << "Inserted at head" << endl 22 | << endl; 23 | return; 24 | } 25 | 26 | Node *tmp = head; 27 | while (tmp->next != NULL) 28 | { 29 | tmp = tmp->next; 30 | } 31 | // tmp ekhon last node e 32 | tmp->next = newNode; 33 | cout << endl 34 | << "Inserted at tail" << endl 35 | << endl; 36 | } 37 | void print_linked_list(Node *head) 38 | { 39 | cout << endl; 40 | cout << "Your Linked List: "; 41 | Node *tmp = head; 42 | while (tmp != NULL) 43 | { 44 | cout << tmp->val << " "; 45 | tmp = tmp->next; 46 | } 47 | cout << endl 48 | << endl; 49 | } 50 | void insert_at_position(Node *head, int pos, int v) 51 | { 52 | Node *newNode = new Node(v); 53 | Node *tmp = head; 54 | for (int i = 1; i <= pos - 1; i++) 55 | { 56 | tmp = tmp->next; 57 | } 58 | newNode->next = tmp->next; 59 | tmp->next = newNode; 60 | cout << endl 61 | << endl 62 | << "Inserted at position " << pos << endl 63 | << endl; 64 | } 65 | void insert_at_head(Node *&head, int v) 66 | { 67 | Node *newNode = new Node(v); 68 | newNode->next = head; 69 | head = newNode; 70 | cout << endl 71 | << "Inserted at head" << endl 72 | << endl; 73 | } 74 | int main() 75 | { 76 | Node *head = NULL; 77 | while (true) 78 | { 79 | cout << "Option 1: Insert at Tail" << endl; 80 | cout << "Option 2: Print Linked List" << endl; 81 | cout << "Option 3: Insert at any Position" << endl; 82 | cout << "Option 4: Insert at Head" << endl; 83 | cout << "Option 5: Terminate" << endl; 84 | int op; 85 | cin >> op; 86 | if (op == 1) 87 | { 88 | cout << "Please enter value: "; 89 | int v; 90 | cin >> v; 91 | insert_at_tail(head, v); 92 | } 93 | else if (op == 2) 94 | { 95 | print_linked_list(head); 96 | } 97 | else if (op == 3) 98 | { 99 | int pos, v; 100 | cout << "Enter position: "; 101 | cin >> pos; 102 | cout << "Enter value: "; 103 | cin >> v; 104 | if (pos == 0) 105 | { 106 | insert_at_head(head, v); 107 | } 108 | else 109 | { 110 | insert_at_position(head, pos, v); 111 | } 112 | } 113 | else if (op == 4) 114 | { 115 | int v; 116 | cout << "Enter value: "; 117 | cin >> v; 118 | insert_at_head(head, v); 119 | } 120 | } 121 | return 0; 122 | } -------------------------------------------------------------------------------- /Module 6/insert_at_position.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_at_tail(Node *&head, int v) 15 | { 16 | Node *newNode = new Node(v); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | cout << endl 21 | << "Inserted at head" << endl 22 | << endl; 23 | return; 24 | } 25 | 26 | Node *tmp = head; 27 | while (tmp->next != NULL) 28 | { 29 | tmp = tmp->next; 30 | } 31 | // tmp ekhon last node e 32 | tmp->next = newNode; 33 | cout << endl 34 | << "Inserted at tail" << endl 35 | << endl; 36 | } 37 | void print_linked_list(Node *head) 38 | { 39 | cout << endl; 40 | cout << "Your Linked List: "; 41 | Node *tmp = head; 42 | while (tmp != NULL) 43 | { 44 | cout << tmp->val << " "; 45 | tmp = tmp->next; 46 | } 47 | cout << endl 48 | << endl; 49 | } 50 | void insert_at_position(Node *head, int pos, int v) 51 | { 52 | Node *newNode = new Node(v); 53 | Node *tmp = head; 54 | for (int i = 1; i <= pos - 1; i++) 55 | { 56 | tmp = tmp->next; 57 | } 58 | newNode->next = tmp->next; 59 | tmp->next = newNode; 60 | cout << endl 61 | << endl 62 | << "Inserted at position " << pos << endl 63 | << endl; 64 | } 65 | int main() 66 | { 67 | Node *head = NULL; 68 | while (true) 69 | { 70 | cout << "Option 1: Insert at Tail" << endl; 71 | cout << "Option 2: Print Linked List" << endl; 72 | cout << "Option 3: Insert at any Position" << endl; 73 | cout << "Option 4: Terminate" << endl; 74 | int op; 75 | cin >> op; 76 | if (op == 1) 77 | { 78 | cout << "Please enter value: "; 79 | int v; 80 | cin >> v; 81 | insert_at_tail(head, v); 82 | } 83 | else if (op == 2) 84 | { 85 | print_linked_list(head); 86 | } 87 | else if (op == 3) 88 | { 89 | int pos, v; 90 | cout << "Enter position: "; 91 | cin >> pos; 92 | cout << "Enter value: "; 93 | cin >> v; 94 | insert_at_position(head, pos, v); 95 | } 96 | else if (op == 4) 97 | { 98 | break; 99 | } 100 | } 101 | return 0; 102 | } -------------------------------------------------------------------------------- /Module 6/insert_at_tail.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_at_tail(Node *&head, int v) 15 | { 16 | Node *newNode = new Node(v); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | return; 21 | } 22 | 23 | Node *tmp = head; 24 | while (tmp->next != NULL) 25 | { 26 | tmp = tmp->next; 27 | } 28 | // tmp ekhon last node e 29 | tmp->next = newNode; 30 | } 31 | void print_linked_list(Node *head) 32 | { 33 | cout << "Your Linked List: "; 34 | Node *tmp = head; 35 | while (tmp != NULL) 36 | { 37 | cout << tmp->val << " "; 38 | tmp = tmp->next; 39 | } 40 | cout << endl; 41 | } 42 | int main() 43 | { 44 | Node *head = NULL; 45 | while (true) 46 | { 47 | cout << "Option 1: Insert at Tail" << endl; 48 | cout << "Option 2: Print Linked List" << endl; 49 | cout << "Option 3: Terminate" << endl; 50 | int op; 51 | cin >> op; 52 | if (op == 1) 53 | { 54 | cout << "Please enter value: "; 55 | int v; 56 | cin >> v; 57 | insert_at_tail(head, v); 58 | } 59 | else if (op == 2) 60 | { 61 | print_linked_list(head); 62 | } 63 | else if (op == 3) 64 | { 65 | break; 66 | } 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Module 6/reference_of_a_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void fun(int *&p) 4 | { 5 | cout << &p << endl; 6 | } 7 | int main() 8 | { 9 | int val = 10; 10 | int *ptr = &val; 11 | fun(ptr); 12 | cout << &ptr << endl; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Module 7/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 7/delete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void print_linekd_list(Node *head) 15 | { 16 | Node *tmp = head; 17 | while (tmp != NULL) 18 | { 19 | cout << tmp->val << " "; 20 | tmp = tmp->next; 21 | } 22 | cout << endl; 23 | } 24 | int size(Node *head) 25 | { 26 | Node *tmp = head; 27 | int count = 0; 28 | while (tmp != NULL) 29 | { 30 | count++; 31 | tmp = tmp->next; 32 | } 33 | return count; 34 | } 35 | void delete_node(Node *head, int pos) 36 | { 37 | Node *tmp = head; 38 | for (int i = 1; i <= pos - 1; i++) 39 | { 40 | tmp = tmp->next; 41 | } 42 | Node *deleteNode = tmp->next; // 30 43 | tmp->next = tmp->next->next; 44 | delete deleteNode; 45 | } 46 | void delete_head(Node *&head) 47 | { 48 | Node *deleteNode = head; 49 | head = head->next; 50 | delete deleteNode; 51 | } 52 | int main() 53 | { 54 | Node *head = new Node(10); 55 | Node *a = new Node(20); 56 | Node *b = new Node(30); 57 | Node *c = new Node(40); 58 | Node *d = new Node(50); 59 | head->next = a; 60 | a->next = b; 61 | b->next = c; 62 | c->next = d; 63 | 64 | print_linekd_list(head); 65 | int pos; 66 | cin >> pos; 67 | if (pos >= size(head)) 68 | { 69 | cout << "Invalid" << endl; 70 | } 71 | else if (pos == 0) 72 | { 73 | delete_head(head); 74 | } 75 | else 76 | { 77 | delete_node(head, pos); 78 | } 79 | print_linekd_list(head); 80 | 81 | return 0; 82 | } -------------------------------------------------------------------------------- /Module 7/input_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_tail(Node *&head, Node *&tail, int val) 15 | { 16 | Node *newNode = new Node(val); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | tail = newNode; 21 | return; 22 | } 23 | tail->next = newNode; 24 | tail = newNode; 25 | } 26 | void print_linekd_list(Node *head) 27 | { 28 | Node *tmp = head; 29 | while (tmp != NULL) 30 | { 31 | cout << tmp->val << " "; 32 | tmp = tmp->next; 33 | } 34 | cout << endl; 35 | } 36 | int main() 37 | { 38 | Node *head = NULL; 39 | Node *tail = NULL; 40 | int val; 41 | while (true) 42 | { 43 | cin >> val; 44 | if (val == -1) 45 | break; 46 | insert_tail(head, tail, val); 47 | } 48 | print_linekd_list(head); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Module 7/insert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void print_linekd_list(Node *head) 15 | { 16 | Node *tmp = head; 17 | while (tmp != NULL) 18 | { 19 | cout << tmp->val << " "; 20 | tmp = tmp->next; 21 | } 22 | cout << endl; 23 | } 24 | int size(Node *head) 25 | { 26 | Node *tmp = head; 27 | int count = 0; 28 | while (tmp != NULL) 29 | { 30 | count++; 31 | tmp = tmp->next; 32 | } 33 | return count; 34 | } 35 | void insert(Node *head, int pos, int val) 36 | { 37 | Node *newNode = new Node(val); 38 | Node *tmp = head; 39 | for (int i = 1; i <= pos - 1; i++) 40 | { 41 | tmp = tmp->next; 42 | } 43 | // tmp = pos-1 44 | newNode->next = tmp->next; 45 | tmp->next = newNode; 46 | } 47 | void insert_head(Node *&head, int val) 48 | { 49 | Node *newNode = new Node(val); 50 | newNode->next = head; 51 | head = newNode; 52 | } 53 | void insert_tail(Node *&head, Node *&tail, int val) 54 | { 55 | Node *newNode = new Node(val); 56 | if (head == NULL) 57 | { 58 | head = newNode; 59 | tail = newNode; 60 | return; 61 | } 62 | tail->next = newNode; 63 | tail = newNode; 64 | } 65 | int main() 66 | { 67 | Node *head = new Node(10); 68 | Node *a = new Node(20); 69 | Node *b = new Node(30); 70 | Node *c = new Node(40); 71 | Node *d = new Node(50); 72 | Node *tail = d; 73 | head->next = a; 74 | a->next = b; 75 | b->next = c; 76 | c->next = d; 77 | print_linekd_list(head); 78 | cout << "Tail-> " << tail->val << endl; 79 | 80 | int pos, val; 81 | cin >> pos >> val; 82 | if (pos > size(head)) 83 | { 84 | cout << "Invalid Index" << endl; 85 | } 86 | else if (pos == 0) 87 | { 88 | insert_head(head, val); 89 | } 90 | else if (pos == size(head)) 91 | { 92 | insert_tail(head, tail, val); 93 | } 94 | else 95 | { 96 | insert(head, pos, val); 97 | } 98 | 99 | print_linekd_list(head); 100 | cout << "Tail-> " << tail->val << endl; 101 | return 0; 102 | } -------------------------------------------------------------------------------- /Module 7/print_recersively.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void print_recursion(Node *n) 15 | { 16 | // base case 17 | if (n == NULL) 18 | return; 19 | cout << n->val << " "; 20 | print_recursion(n->next); 21 | } 22 | void print_reverse(Node *n) 23 | { 24 | // base case 25 | if (n == NULL) 26 | return; 27 | 28 | print_reverse(n->next); 29 | cout << n->val << " "; 30 | } 31 | int main() 32 | { 33 | Node *head = new Node(10); 34 | Node *a = new Node(20); 35 | Node *b = new Node(30); 36 | Node *c = new Node(40); 37 | Node *d = new Node(50); 38 | head->next = a; 39 | a->next = b; 40 | b->next = c; 41 | c->next = d; 42 | 43 | print_recursion(head); 44 | cout << endl; 45 | print_reverse(head); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Module 7/singly_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void print_linekd_list(Node *head) 15 | { 16 | Node *tmp = head; 17 | while (tmp != NULL) 18 | { 19 | cout << tmp->val << " "; 20 | tmp = tmp->next; 21 | } 22 | cout << endl; 23 | } 24 | int main() 25 | { 26 | Node *head = new Node(10); 27 | Node *a = new Node(20); 28 | Node *b = new Node(30); 29 | Node *c = new Node(40); 30 | Node *d = new Node(50); 31 | head->next = a; 32 | a->next = b; 33 | b->next = c; 34 | c->next = d; 35 | 36 | print_linekd_list(head); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Module 7/sort_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void insert_tail(Node *&head, Node *&tail, int val) 15 | { 16 | Node *newNode = new Node(val); 17 | if (head == NULL) 18 | { 19 | head = newNode; 20 | tail = newNode; 21 | return; 22 | } 23 | tail->next = newNode; 24 | tail = newNode; 25 | } 26 | void print_linekd_list(Node *head) 27 | { 28 | Node *tmp = head; 29 | while (tmp != NULL) 30 | { 31 | cout << tmp->val << " "; 32 | tmp = tmp->next; 33 | } 34 | cout << endl; 35 | } 36 | int main() 37 | { 38 | Node *head = NULL; 39 | Node *tail = NULL; 40 | int val; 41 | while (true) 42 | { 43 | cin >> val; 44 | if (val == -1) 45 | break; 46 | insert_tail(head, tail, val); 47 | } 48 | for (Node *i = head; i->next != NULL; i = i->next) 49 | { 50 | for (Node *j = i->next; j != NULL; j = j->next) 51 | { 52 | if (i->val < j->val) 53 | { 54 | swap(i->val, j->val); 55 | } 56 | } 57 | } 58 | print_linekd_list(head); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Module 7/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node(int val) 9 | { 10 | this->val = val; 11 | this->next = NULL; 12 | } 13 | }; 14 | void print_linekd_list(Node *head) 15 | { 16 | Node *tmp = head; 17 | while (tmp != NULL) 18 | { 19 | cout << tmp->val << " "; 20 | tmp = tmp->next; 21 | } 22 | cout << endl; 23 | } 24 | int size(Node *head) 25 | { 26 | Node *tmp = head; 27 | int count = 0; 28 | while (tmp != NULL) 29 | { 30 | count++; 31 | tmp = tmp->next; 32 | } 33 | return count; 34 | } 35 | void insert(Node *head, int pos, int val) 36 | { 37 | Node *newNode = new Node(val); 38 | Node *tmp = head; 39 | for (int i = 1; i <= pos - 1; i++) 40 | { 41 | tmp = tmp->next; 42 | } 43 | // tmp = pos-1 44 | newNode->next = tmp->next; 45 | tmp->next = newNode; 46 | } 47 | void insert_head(Node *&head, int val) 48 | { 49 | Node *newNode = new Node(val); 50 | newNode->next = head; 51 | head = newNode; 52 | } 53 | int main() 54 | { 55 | Node *head = new Node(10); 56 | Node *a = new Node(20); 57 | Node *b = new Node(30); 58 | Node *c = new Node(40); 59 | Node *d = new Node(50); 60 | head->next = a; 61 | a->next = b; 62 | b->next = c; 63 | c->next = d; 64 | print_linekd_list(head); 65 | 66 | int pos, val; 67 | cin >> pos >> val; 68 | if (pos > size(head)) 69 | { 70 | cout << "Invalid Index" << endl; 71 | } 72 | else if (pos == 0) 73 | { 74 | insert_head(head, val); 75 | } 76 | else 77 | { 78 | insert(head, pos, val); 79 | } 80 | 81 | print_linekd_list(head); 82 | return 0; 83 | } -------------------------------------------------------------------------------- /Module 9/.gitignore: -------------------------------------------------------------------------------- 1 | .cph/ 2 | .vscode/ 3 | *.txt 4 | *.exe 5 | *.bin -------------------------------------------------------------------------------- /Module 9/delete_node.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node *prev; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->next = NULL; 13 | this->prev = NULL; 14 | } 15 | }; 16 | void print_normal(Node *head) 17 | { 18 | Node *tmp = head; 19 | while (tmp != NULL) 20 | { 21 | cout << tmp->val << " "; 22 | tmp = tmp->next; 23 | } 24 | cout << endl; 25 | } 26 | void print_reverse(Node *tail) 27 | { 28 | Node *tmp = tail; 29 | while (tmp != NULL) 30 | { 31 | cout << tmp->val << " "; 32 | tmp = tmp->prev; 33 | } 34 | cout << endl; 35 | } 36 | int size(Node *head) 37 | { 38 | Node *tmp = head; 39 | int cnt = 0; 40 | while (tmp != NULL) 41 | { 42 | cnt++; 43 | tmp = tmp->next; 44 | } 45 | return cnt; 46 | } 47 | void delete_at_position(Node *head, int pos) 48 | { 49 | Node *tmp = head; 50 | for (int i = 1; i <= pos - 1; i++) 51 | { 52 | tmp = tmp->next; 53 | } 54 | Node *deleteNode = tmp->next; 55 | tmp->next = tmp->next->next; 56 | tmp->next->prev = tmp; 57 | delete deleteNode; 58 | } 59 | void delete_tail(Node *&head, Node *&tail) 60 | { 61 | Node *deleteNode = tail; 62 | tail = tail->prev; 63 | delete deleteNode; 64 | if (tail == NULL) 65 | { 66 | head = NULL; 67 | return; 68 | } 69 | tail->next = NULL; 70 | } 71 | void delete_head(Node *&head, Node *&tail) 72 | { 73 | Node *deleteNode = head; 74 | head = head->next; 75 | delete deleteNode; 76 | if (head == NULL) 77 | { 78 | tail = NULL; 79 | return; 80 | } 81 | head->prev = NULL; 82 | } 83 | int main() 84 | { 85 | Node *head = new Node(10); 86 | Node *tail = head; 87 | 88 | // connection 89 | 90 | int pos; 91 | cin >> pos; 92 | 93 | if (pos >= size(head)) 94 | { 95 | cout << "Invalid" << endl; 96 | } 97 | else if (pos == 0) 98 | { 99 | delete_head(head, tail); 100 | } 101 | else if (pos == size(head) - 1) 102 | { 103 | delete_tail(head, tail); 104 | } 105 | else 106 | { 107 | delete_at_position(head, pos); 108 | } 109 | 110 | print_normal(head); 111 | print_reverse(tail); 112 | return 0; 113 | } -------------------------------------------------------------------------------- /Module 9/doubly_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node *prev; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->next = NULL; 13 | this->prev = NULL; 14 | } 15 | }; 16 | void print_normal(Node *head) 17 | { 18 | Node *tmp = head; 19 | while (tmp != NULL) 20 | { 21 | cout << tmp->val << " "; 22 | tmp = tmp->next; 23 | } 24 | cout << endl; 25 | } 26 | void print_reverse(Node *tail) 27 | { 28 | Node *tmp = tail; 29 | while (tmp != NULL) 30 | { 31 | cout << tmp->val << " "; 32 | tmp = tmp->prev; 33 | } 34 | cout << endl; 35 | } 36 | int main() 37 | { 38 | Node *head = new Node(10); 39 | Node *a = new Node(20); 40 | Node *b = new Node(30); 41 | Node *tail = b; 42 | 43 | // connection 44 | head->next = a; 45 | a->prev = head; 46 | a->next = b; 47 | b->prev = a; 48 | 49 | print_normal(head); 50 | print_reverse(tail); 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Module 9/input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node *prev; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->next = NULL; 13 | this->prev = NULL; 14 | } 15 | }; 16 | void print_normal(Node *head) 17 | { 18 | Node *tmp = head; 19 | while (tmp != NULL) 20 | { 21 | cout << tmp->val << " "; 22 | tmp = tmp->next; 23 | } 24 | cout << endl; 25 | } 26 | void print_reverse(Node *tail) 27 | { 28 | Node *tmp = tail; 29 | while (tmp != NULL) 30 | { 31 | cout << tmp->val << " "; 32 | tmp = tmp->prev; 33 | } 34 | cout << endl; 35 | } 36 | void insert_tail(Node *&head, Node *&tail, int val) 37 | { 38 | Node *newNode = new Node(val); 39 | if (tail == NULL) 40 | { 41 | head = newNode; 42 | tail = newNode; 43 | return; 44 | } 45 | tail->next = newNode; 46 | newNode->prev = tail; 47 | tail = tail->next; 48 | } 49 | int main() 50 | { 51 | Node *head = NULL; 52 | Node *tail = NULL; 53 | int val; 54 | while (true) 55 | { 56 | cin >> val; 57 | if (val == -1) 58 | break; 59 | insert_tail(head, tail, val); 60 | } 61 | print_normal(head); 62 | print_reverse(tail); 63 | return 0; 64 | } -------------------------------------------------------------------------------- /Module 9/insert_at_position.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int val; 7 | Node *next; 8 | Node *prev; 9 | Node(int val) 10 | { 11 | this->val = val; 12 | this->next = NULL; 13 | this->prev = NULL; 14 | } 15 | }; 16 | void print_normal(Node *head) 17 | { 18 | Node *tmp = head; 19 | while (tmp != NULL) 20 | { 21 | cout << tmp->val << " "; 22 | tmp = tmp->next; 23 | } 24 | cout << endl; 25 | } 26 | void print_reverse(Node *tail) 27 | { 28 | Node *tmp = tail; 29 | while (tmp != NULL) 30 | { 31 | cout << tmp->val << " "; 32 | tmp = tmp->prev; 33 | } 34 | cout << endl; 35 | } 36 | void insert_at_position(Node *head, int pos, int val) 37 | { 38 | Node *newNode = new Node(val); 39 | Node *tmp = head; 40 | for (int i = 1; i <= pos - 1; i++) 41 | { 42 | tmp = tmp->next; 43 | } 44 | newNode->next = tmp->next; // 100->30 45 | tmp->next = newNode; // 20->100 46 | newNode->next->prev = newNode; // 100<-30 47 | newNode->prev = tmp; // 20<-100 48 | } 49 | int size(Node *head) 50 | { 51 | Node *tmp = head; 52 | int cnt = 0; 53 | while (tmp != NULL) 54 | { 55 | cnt++; 56 | tmp = tmp->next; 57 | } 58 | return cnt; 59 | } 60 | void insert_head(Node *&head, Node *&tail, int val) 61 | { 62 | Node *newNode = new Node(val); 63 | if (head == NULL) 64 | { 65 | head = newNode; 66 | tail = newNode; 67 | return; 68 | } 69 | newNode->next = head; 70 | head->prev = newNode; 71 | head = newNode; 72 | } 73 | void insert_tail(Node *&head, Node *&tail, int val) 74 | { 75 | Node *newNode = new Node(val); 76 | if (tail == NULL) 77 | { 78 | head = newNode; 79 | tail = newNode; 80 | return; 81 | } 82 | tail->next = newNode; 83 | newNode->prev = tail; 84 | tail = tail->next; 85 | } 86 | int main() 87 | { 88 | Node *head = new Node(10); 89 | Node *a = new Node(20); 90 | Node *b = new Node(30); 91 | Node *c = new Node(40); 92 | Node *tail = c; 93 | 94 | // connection 95 | head->next = a; 96 | a->prev = head; 97 | a->next = b; 98 | b->prev = a; 99 | b->next = c; 100 | c->prev = b; 101 | int pos, val; 102 | cin >> pos >> val; 103 | 104 | if (pos > size(head)) 105 | { 106 | cout << "Invalid" << endl; 107 | } 108 | else if (pos == 0) 109 | { 110 | insert_head(head, tail, val); 111 | } 112 | else if (pos == size(head)) 113 | { 114 | insert_tail(head, tail, val); 115 | } 116 | else 117 | { 118 | insert_at_position(head, pos, val); 119 | } 120 | print_normal(head); 121 | print_reverse(tail); 122 | 123 | return 0; 124 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Batch-4-Basic-Data-Structures- 2 | --------------------------------------------------------------------------------