commit 1fcb7a0c80f356a2e4c1f63a15057053e6ac48dd parent 97ac4c101458467535856d65e4e5e9e1ecd5cb81 Author: Amit Dutta <mail@amit.is-a.dev> Date: Thu, 27 Aug 2026 19:42:30 +0530 Merge pull request #91 from notamitgamer/edit-20260827-193516 completed the backlogs Diffstat:
| A | semester_2/algorithms/algo_040.md | | | 23 | +++++++++++++++++++++++ |
| A | semester_2/algorithms/algo_041.md | | | 23 | +++++++++++++++++++++++ |
| A | semester_2/algorithms/algo_042.md | | | 23 | +++++++++++++++++++++++ |
| A | semester_2/algorithms/algo_043.md | | | 24 | ++++++++++++++++++++++++ |
| A | semester_2/algorithms/algo_044.md | | | 27 | +++++++++++++++++++++++++++ |
| A | semester_2/algorithms/algo_045.md | | | 17 | +++++++++++++++++ |
| A | semester_2/algorithms/algo_046.md | | | 27 | +++++++++++++++++++++++++++ |
| A | semester_2/algorithms/algo_047.md | | | 17 | +++++++++++++++++ |
| A | semester_2/algorithms/algo_048.md | | | 17 | +++++++++++++++++ |
| A | semester_2/algorithms/algo_049.md | | | 17 | +++++++++++++++++ |
| A | semester_2/algorithms/algo_050.md | | | 19 | +++++++++++++++++++ |
| A | semester_2/algorithms/algo_051.md | | | 19 | +++++++++++++++++++ |
| A | semester_2/algorithms/algo_052.md | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_018.cpp | | | 47 | +++++++++++++++++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_019.cpp | | | 33 | +++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_020.cpp | | | 33 | +++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_021.cpp | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_022.cpp | | | 34 | ++++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_023.cpp | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_024.cpp | | | 57 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_025.cpp | | | 25 | +++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_026.cpp | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_027.cpp | | | 103 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | semester_2/eduincs/pgrm_028.cpp | | | 96 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 files changed, 878 insertions(+), 0 deletions(-)
diff --git a/semester_2/algorithms/algo_040.md b/semester_2/algorithms/algo_040.md @@ -0,0 +1,23 @@ +# algo_040 + +### Problem Statement + +> Write an algorithm to enqueue an element into a linear queue using linked representation. + +## Algorithm +``` +procedure enqueue(rear, front, val) +begin + ptr ← getNode(); + info(ptr) ← val; + next(ptr) ← NULL; + if (front = NULL and rear = NULL) then + front ← ptr; + rear ← ptr; + else + next(rear) ← ptr; + rear ← ptr; + endif + return(rear); +end procedure +``` diff --git a/semester_2/algorithms/algo_041.md b/semester_2/algorithms/algo_041.md @@ -0,0 +1,23 @@ +# algo_041 + +### Problem Statement + +> Write an algorithm to dequeue an element from a linear queue using linked representation. + +## Algorithm +``` +procedure dequeue(front, rear) +begin + if (front = NULL and rear = NULL) then + write("Queue empty, deletion not possible."); + else if (front = rear) then + delete(front); + front ← NULL; + rear ← NULL; + else + ptr ← front; + front ← next(ptr); + delete(ptr); + endif +end procedure +``` diff --git a/semester_2/algorithms/algo_042.md b/semester_2/algorithms/algo_042.md @@ -0,0 +1,23 @@ +# algo_042 + +### Problem Statement + +> Write an algorithm to enqueue an element into a circular queue using linked representation. + +## Algorithm +``` +procedure enqueue(cq, val) +begin + ptr ← getNode(); + info(ptr) ← val; + next(ptr) ← ptr; + if (cq = NULL) then + cq ← ptr; + else + next(ptr) ← next(cq); + next(cq) ← ptr; + cq ← ptr; + endif + return(cq); +end procedure +``` diff --git a/semester_2/algorithms/algo_043.md b/semester_2/algorithms/algo_043.md @@ -0,0 +1,24 @@ +# algo_043 + +### Problem Statement + +> Write an algorithm to dequeue an element from a circular queue using linked representation. + +## Algorithm +``` +procedure dequeue(cq) +begin + if (cq = NULL) then + write("Deletion not possible"); + else if (next(cq) = cq) then + ptr ← cq; + cq ← NULL; + delete(ptr); + else + ptr ← next(cq); + next(cq) ← next(ptr); + delete(ptr); + endif + return(cq); +end procedure +``` diff --git a/semester_2/algorithms/algo_044.md b/semester_2/algorithms/algo_044.md @@ -0,0 +1,27 @@ +# algo_044 + +### Problem Statement + +> Write a non-recursive algorithm for inorder traversal of a binary tree. + +## Algorithm +``` +procedure inorder(T) +begin + ptr ← T; + flag ← 1; + while (flag) do + while (ptr ≠ NULL) do + push(stack, ptr); + ptr ← LC(ptr); + endwhile + if (!isEmpty(stack)) then + ptr ← pop(stack); + write(INFO(ptr)); + ptr ← RC(ptr); + else + flag ← 0; + endif + endwhile +end procedure +``` diff --git a/semester_2/algorithms/algo_045.md b/semester_2/algorithms/algo_045.md @@ -0,0 +1,17 @@ +# algo_045 + +### Problem Statement + +> Write a recursive algorithm for inorder traversal of a binary tree. + +## Algorithm +``` +procedure inorder(ptr) +begin + if (ptr ≠ NULL) then + inorder(LC(ptr)); + write(INFO(ptr)); + inorder(RC(ptr)); + endif +end procedure +``` diff --git a/semester_2/algorithms/algo_046.md b/semester_2/algorithms/algo_046.md @@ -0,0 +1,27 @@ +# algo_046 + +### Problem Statement + +> Write a non-recursive algorithm for preorder traversal of a binary tree. + +## Algorithm +``` +procedure preorder(T) +begin + ptr ← T; + flag ← 1; + while (flag = 1) do + while (ptr ≠ NULL) do + write(INFO(ptr)); + push(stack, ptr); + ptr ← LC(ptr); + endwhile + if (!isEmpty(stack)) then + ptr ← pop(stack); + ptr ← RC(ptr); + else + flag ← 0; + endif + endwhile +end procedure +``` diff --git a/semester_2/algorithms/algo_047.md b/semester_2/algorithms/algo_047.md @@ -0,0 +1,17 @@ +# algo_047 + +### Problem Statement + +> Write a recursive algorithm for preorder traversal of a binary tree. + +## Algorithm +``` +procedure preorder(ptr) +begin + if (ptr ≠ NULL) then + write(INFO(ptr)); + preorder(LC(ptr)); + preorder(RC(ptr)); + endif +end procedure +``` diff --git a/semester_2/algorithms/algo_048.md b/semester_2/algorithms/algo_048.md @@ -0,0 +1,17 @@ +# algo_048 + +### Problem Statement + +> Write a recursive algorithm for postorder traversal of a binary tree. + +## Algorithm +``` +procedure postorder(ptr) +begin + if (ptr ≠ NULL) then + postorder(LC(ptr)); + postorder(RC(ptr)); + write(INFO(ptr)); + endif +end procedure +``` diff --git a/semester_2/algorithms/algo_049.md b/semester_2/algorithms/algo_049.md @@ -0,0 +1,17 @@ +# algo_049 + +### Problem Statement + +> Write an algorithm to count the total number of nodes in a binary tree. + +## Algorithm +``` +procedure countNode(T) +begin + if (T = NULL) then + return 0; + else + return 1 + countNode(LC(T)) + countNode(RC(T)); + endif +end procedure +``` diff --git a/semester_2/algorithms/algo_050.md b/semester_2/algorithms/algo_050.md @@ -0,0 +1,19 @@ +# algo_050 + +### Problem Statement + +> Write an algorithm to count the number of internal nodes in a binary tree. + +## Algorithm +``` +procedure countInternal(T) +begin + if (T = NULL) then + return 0; + else if (LC(T) = NULL and RC(T) = NULL) then + return 0; + else + return 1 + countInternal(LC(T)) + countInternal(RC(T)); + endif +end procedure +``` diff --git a/semester_2/algorithms/algo_051.md b/semester_2/algorithms/algo_051.md @@ -0,0 +1,19 @@ +# algo_051 + +### Problem Statement + +> Write an algorithm to count the number of leaf nodes in a binary tree. + +## Algorithm +``` +procedure countLeaf(T) +begin + if (T = NULL) then + return 0; + else if (LC(T) = NULL and RC(T) = NULL) then + return 1; + else + return countLeaf(LC(T)) + countLeaf(RC(T)); + endif +end procedure +``` diff --git a/semester_2/algorithms/algo_052.md b/semester_2/algorithms/algo_052.md @@ -0,0 +1,40 @@ +# algo_052 + +### Problem Statement + +> Write an algorithm to insert a node (an element) into a Binary Search Tree (BST). + +## Algorithm +``` +procedure insert_BST(root, val) +begin + nptr ← getNode(); + INFO(nptr) ← val; + LC(nptr) ← NULL; + RC(nptr) ← NULL; + if (root = NULL) then + root ← nptr; + return(root); + endif + ptr ← root; + parent ← NULL; + while (ptr ≠ NULL) do + parent ← ptr; + if (val < INFO(ptr)) then + ptr ← LC(ptr); + else if (val > INFO(ptr)) then + ptr ← RC(ptr); + else + write("Duplicate value not allowed"); + delete(nptr); + return(root); + endif + endwhile + if (val < INFO(parent)) then + LC(parent) ← nptr; + else + RC(parent) ← nptr; + endif + return(root); +end procedure +``` diff --git a/semester_2/eduincs/pgrm_018.cpp b/semester_2/eduincs/pgrm_018.cpp @@ -0,0 +1,47 @@ +/* Operator overloading */ + +#include<iostream> +using namespace std; +class complex { +private: + int real; + int img; +public: + complex(float r = 0, float i = 0) { + real = r; img = i; + } + + // overloading the `+` operator + complex operator +(const complex &obj) { + complex temp; + temp.real = real + obj.real; + temp.img = img + obj.img; + return temp; + } + + complex add_complex(const complex &obj) { + complex temp; + temp.real = real + obj.real; + temp.img = img + obj.img; + return temp; + } + + void output() { + cout << "Complex number: " << real << "+" << img << "i" << endl; + } +}; + +int main() { + complex complex1(10, 20), complex2(20, 30), result; + cout << "First complex number: "; + complex1.output(); + cout << "Second complex number: "; + complex2.output(); + cout << "After addition: "; + result = complex1 + complex2; + result.output(); + cout << "After addition: "; + result = complex1.add_complex(complex2); + result.output(); + return 0; +} diff --git a/semester_2/eduincs/pgrm_019.cpp b/semester_2/eduincs/pgrm_019.cpp @@ -0,0 +1,33 @@ +/* Overload `++` when used as prefix */ + +#include<iostream> +using namespace std; +class count { +private: + int val1; + int val2; +public: + count() { + val1 = 5; + val2 = 6; + } + + void operator ++() { + ++val1; + ++val2; + } + + void display() { + cout << "value1: " << val1 << " value2: " << val2 << endl; + } +}; + +int main() { + count obj; + cout << "Before overloading ++: " << endl; + obj.display(); + ++obj; + cout << "After overloading ++: " << endl; + obj.display(); + return 0; +} diff --git a/semester_2/eduincs/pgrm_020.cpp b/semester_2/eduincs/pgrm_020.cpp @@ -0,0 +1,33 @@ +/* Overload `++` when used as postfix */ + +#include<iostream> +using namespace std; +class count { +private: + int val1; + int val2; +public: + count() { + val1 = 5; + val2 = 6; + } + + void operator ++(int) { + val1++; + val2++; + } + + void display() { + cout << "value1: " << val1 << " value2: " << val2 << endl; + } +}; + +int main() { + count obj; + cout << "Before overloading ++: " << endl; + obj.display(); + obj++; + cout << "After overloading ++: " << endl; + obj.display(); + return 0; +} diff --git a/semester_2/eduincs/pgrm_021.cpp b/semester_2/eduincs/pgrm_021.cpp @@ -0,0 +1,39 @@ +/* operator overloading using friend function */ + +#include <iostream> +using namespace std; + +class Complex { +private: + int real, img; + +public: + Complex (int r = 0, int i = 0) { + real = r; img = i; + } + + friend Complex operator + (const Complex &obj1, const Complex &obj2); + + void output() { + cout << "Complex number: " << real << " + " << img << "i"; + } +}; + +Complex operator + (const Complex &obj1, const Complex &obj2) { + Complex temp; + temp.real = obj1.real + obj2.real; + temp.img = obj1.img + obj2.img; + return temp; +} + +int main() { + Complex complex1(10, 20), complex2(20, 30), result; + cout << "First complex number: " << endl; + complex1.output(); + cout << endl << "Second complex number: " << endl; + complex2.output(); + cout << endl << "After Addition: " << endl; + result = complex1 + complex2; + result.output(); + return 0; +} diff --git a/semester_2/eduincs/pgrm_022.cpp b/semester_2/eduincs/pgrm_022.cpp @@ -0,0 +1,34 @@ +/* overloading of extraction operator and insertion operator */ + +#include <iostream> +using namespace std; + +class Height { +private: + int feet, inches; + +public: + Height(int f = 0, int i = 0) { + feet = f; inches = i; + } + + friend ostream /* output stream */ &operator << (ostream &output, const Height &D) { + output << D.feet << " ft " << D.inches << " inch"; + return output; + } + + friend istream /* input stream */ &operator >> (istream &input, Height &D) { + input >> D.feet >> D.inches; + return input; + } +}; + +int main() { + Height D1(5, 10), D2(5, 11), D3; + cout << "First height: " << D1 << endl; + cout << "Second height: " << D2 << endl; + cout << "Enter third height: " << endl; + cin >> D3; + cout << "Third height: " << D3 << endl; + return 0; +} diff --git a/semester_2/eduincs/pgrm_023.cpp b/semester_2/eduincs/pgrm_023.cpp @@ -0,0 +1,55 @@ +/* WAP to implement linear search template class and template function */ + +#include<iostream> +using namespace std; + +template <class T> +class LinearSearch { +private: + T *arr; + int size; + T key; + +public: + LinearSearch(int n) { + size = n; + arr = new T[size]; + + cout << "Array Size: " << size << endl; + + cout << "Enter elements: "; + for(int i = 0; i < size; i++) { + cin >> arr[i]; + } + + cout << "Enter key: "; + cin >> key; + } + + int search() { + for(int i = 0; i < size; i++) { + if(arr[i] == key) + return i; + } + return -1; + } +}; + +int main() { + + LinearSearch<int> obj(5); + int index = obj.search(); + if(index != -1) + cout << "Key found at: " << index << endl; + else + cout << "Key not found!" << endl; + + LinearSearch<double> obj2(5); + index = obj2.search(); + if(index != -1) + cout << "Key found at: " << index << endl; + else + cout << "Key not found!" << endl; + + return 0; +} diff --git a/semester_2/eduincs/pgrm_024.cpp b/semester_2/eduincs/pgrm_024.cpp @@ -0,0 +1,57 @@ +/* WAP to find max from an array using template class and no-inline template function */ + +#include <iostream> +using namespace std; + +template <class T> +class Max { +private: + T *a; + int s; + +public: + Max() { + cout << "Array size: "; + cin >> s; + a = new T[s]; + } + + void getData(); + T find_max(); + + ~Max() { + delete[] a; + cout << "obj deleted."; + } +}; + +template <class T> +void Max<T>::getData() { + cout << "Enter elements: "; + for (int i = 0; i < s; ++i) { + cin >> a[i]; + } +} + +template <class T> +T Max<T>::find_max() { + T m = a[0]; + for (int i = 1; i < s; i++) { + if (m < a[i]) { + m = a[i]; + } + } + return m; +} + +int main() { + Max<int> obj; + obj.getData(); + cout << "Max element: " << obj.find_max() << endl; + + Max<float> obj2; + obj2.getData(); + cout << "Max element: " << obj2.find_max() << endl; + + return 0; +} diff --git a/semester_2/eduincs/pgrm_025.cpp b/semester_2/eduincs/pgrm_025.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <string> +using namespace std; + +template <typename T, typename U> +class MyClass { +public: + T m_val1; + U m_val2; + + MyClass(T val1, U val2) { + m_val1 = val1; + m_val2 = val2; + } + + MyClass() : m_val1{}, m_val2{} {} +}; + +int main() { + MyClass<int, string> myobj(5, "Hello"); + cout << myobj.m_val1 << " " << myobj.m_val2 << endl; + MyClass<int, string> myobj2; + cout << myobj2.m_val1 << " " << myobj2.m_val2 << endl; + return 0; +} diff --git a/semester_2/eduincs/pgrm_026.cpp b/semester_2/eduincs/pgrm_026.cpp @@ -0,0 +1,63 @@ +#include <iostream> +using namespace std; + +class Node { +public: + int info; + Node *next; + + Node(int val) { + info = val; + next = NULL; + } +}; + +class SinglyList { +private: + Node *head; + +public: + SinglyList() { + head = NULL; + } + + void insertBegin(int val) { + Node *ptr = new Node(val); + ptr->next = head; + head = ptr; + cout << "Inserted " << val << " at the beggining"; + } + + void insertEnd(int val) { + Node *ptr = new Node(val); + if (head == NULL) { + head = ptr; + } else { + Node *t = head; + while (t->next != NULL) { + t = t->next; + } + t->next = ptr; + } + cout << endl << "Inserted " << val << " at the end"; + } + + void display() { + Node *t = head; cout << endl << "The list: "; + while(t != NULL) { + cout << t -> info << " "; + t = t -> next; + } + } +}; + +int main() { + SinglyList obj; + obj.insertBegin(10); + obj.insertBegin(20); + obj.display(); + obj.insertEnd(40); + obj.insertEnd(50); + obj.display(); + return 0; +} diff --git a/semester_2/eduincs/pgrm_027.cpp b/semester_2/eduincs/pgrm_027.cpp @@ -0,0 +1,103 @@ +/* Double linked list */ + +#include <iostream> +using namespace std; + +struct Node { + int info; + Node *next = nullptr; + Node *prev = nullptr; +}; + +class doublelist { +private: + Node *head = nullptr; + +public: + void insertBeg(); + void insertEnd(); + void display(); + ~doublelist(); +}; + +void doublelist::insertBeg() { + Node *newNode = new Node; + cout << "Enter data: "; + cin >> newNode->info; + + if (head == nullptr) { + head = newNode; + } else { + head->prev = newNode; + newNode->next = head; + head = newNode; + } + cout << "Node added successfully!\n"; +} + +void doublelist::insertEnd() { + Node *newNode = new Node; + cout << "Enter data: "; + cin >> newNode->info; + + if (head == nullptr) { + head = newNode; + } else { + Node *tmp = head; + while (tmp->next != nullptr) { + tmp = tmp->next; + } + tmp->next = newNode; + newNode->prev = tmp; + } + cout << "Node added successfully!\n"; +} + +void doublelist::display() { + cout << "The list: "; + Node *tmp = head; + while (tmp != nullptr) { + cout << tmp->info << " "; + tmp = tmp->next; + } + cout << "nullptr\n"; +} + +doublelist::~doublelist() { + Node *tmp; + while (head != nullptr) { + tmp = head; + head = head->next; + delete tmp; + } + cout << "List deleted\n"; +} + +int main() { + doublelist obj; + int ch; + + while (1) { + cout << "\nMenu:\n1. Insert Beg\n2. Insert End\n3. Display\n4. Exit\n"; + cout << "Enter choice: "; + cin >> ch; + + switch (ch) { + case 1: + obj.insertBeg(); + break; + case 2: + obj.insertEnd(); + break; + case 3: + obj.display(); + break; + case 4: + return 0; + default: + cout << "Wrong choice\n"; + } + } + + return 0; +} diff --git a/semester_2/eduincs/pgrm_028.cpp b/semester_2/eduincs/pgrm_028.cpp @@ -0,0 +1,96 @@ +/* Circluar linked list */ + +#include <iostream> +using namespace std; + +struct Node { + int info; + Node *next = this; +}; + +class circularlist { +private: + Node *cl = nullptr; + +public: + void insertEnd(); + void display(); + ~circularlist(); +}; + +typedef class circularlist cl; + +void circularlist::insertEnd() { + Node *newNode = new Node; + cout << "Enter info: "; + cin >> newNode->info; + + if (cl == nullptr) { + cl = newNode; + cl->next = cl; + } else { + newNode->next = cl->next; + cl->next = newNode; + cl = newNode; + } + cout << endl << "Inserted " << newNode->info << " at the end\n"; +} + +void circularlist::display() { + if (cl == nullptr) { + cout << "List is empty\n"; + return; + } + + Node *t = cl->next; + cout << "Circular List: "; + do { + cout << t->info << " -> "; + t = t->next; + } while (t != cl->next); + cout << "(head)\n"; +} + +circularlist::~circularlist() { + if (cl == nullptr) return; + + Node *t = cl->next; + Node *p; + while (t != cl) { + p = t->next; + delete t; + t = p; + } + delete cl; + cl = nullptr; +} + +int main() { + circularlist list; + int choice; + + while (true) { + cout << "\n--- Circular Linked List Menu ---\n"; + cout << "1. Insert at End\n"; + cout << "2. Display List\n"; + cout << "3. Exit\n"; + cout << "Enter your choice: "; + cin >> choice; + + switch (choice) { + case 1: + list.insertEnd(); + break; + case 2: + list.display(); + break; + case 3: + cout << "Exiting program...\n"; + return 0; + default: + cout << "Invalid choice! Please try again.\n"; + } + } + + return 0; +}