Soham Ghodke DSAL
Soham Ghodke DSAL
#include <list>
#include <vector>
using namespace std;
public:
return comparisons;
};
public:
}
}
table[index].emplace_back(name, phone);
return comparisons;
};
ChainingHashTable chainingTable;
int n;
chainingTable.insert(clients[i].first, clients[i].second);
return 0;
}
#include <iostream>
#include <list>
#include <string>
using namespace std;
public:
return;
else {
if (!table[index].empty()) { auto&
firstPair = table[index].front(); int exis
ngIndex = hashFunc on(firstPair.first); if
(exis ngIndex != index) {
// Displace the current one and rehash it
pair<string, string> displaced = firstPair;
table[index].pop_front(); table[index].emplace_front(key,
value); insert(displaced.first, displaced.second); // reinsert
displaced
} else {
table[index].emplace_back(key, value);
}
} else {
table[index].emplace_back(key, value);
cout << "Key '" << key << "' not found.\n";
cout << "Key '" << key << "' not found for dele on.\n";
}
void display() { cout << "\n--- Hash Table ---\n";
for (int i = 0; i < TABLE_SIZE; ++i) { cout << i << " -
> "; for (auto& pair : table[i]) { cout << "["
<< pair.first << ": " << pair.second << "] -> ";
}
};
HashTable dict(useReplacement);
int choice;
string key, value;
switch (choice) {
case 1:
cout << "Enter key: ";
cin >> key; cout <<
"Enter value: "; cin
>> value;
dict.insert(key, value);
break; case 2:
cout << "Enter key to find:
"; cin >> key;
dict.find(key); break;
case 3:
return 0;
}
#include <iostream>
#include <vector>
#include <string>
TreeNode(string n) : name(n) {}
};
}
// Func on to free memory (avoids memory leaks)
void deleteTree(TreeNode* node) {
if (!node) return; for (auto
child : node->children)
deleteTree(child); delete node;
}
int main() {
// Clean up memory
deleteTree(book);
return 0;
#include <iostream>
#include <algorithm>
using namespace std;
Node* right;
Node(int value) {
data = value; le =
right = nullptr;
}
};
// Inorder Traversal
else
// Clean up memory
deleteTree(root); return
0;
}
#include <iostream>
using namespace std;
class Node
{ public:
int data;
Node* le ;
Node* right;
bool isThreaded;
Node(int val)
{ data = val;
le = right = nullptr;
isThreaded = false;
}
};
// Converts binary tree to threaded binary tree using reverse inorder traversal
// Le subtree createThreaded(root-
>le , prev);
return root;
int main() {
return 0;}
#include <iostream>
#include <unordered_map>
#include <vector>
#include <queue>
#include <set> using
namespace std;
public:
set<string> visited;
queue<string> q;
};
int main() {
Graph flightMap;
flightMap.printGraph();
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
};
return true;
};
}
return totalCost;
{0, 2, 6},
{0, 3, 5},
{1, 3, 15},
{2, 3, 4}
};
cout << "Minimum cost to connect all offices: " << totalCost << endl; cout <<
"Connec ons used:\n"; for (auto& edge : mst) cout << "Office " << edge.u << " -
Office " << edge.v << " : Cost = " << edge.cost << endl;
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
// Right rotation
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
// Left rotation
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
// Left-right case
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right-left case
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
return root;
}
public:
AVLTree() : root(nullptr) {}
// Delete a keyword
void deleteKeyword(const string& key) {
root = deleteNode(root, key);
}
int main() {
AVLTree dictionary;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
class HeapSort
{ public:
void sort(vector<int>& arr) {
int n = arr.size();
}
void display(const vector<int>& arr) {
private:
if (largest != i)
{ swap(arr[i],
arr[largest]);
heapify(arr, n, largest);
}
};
#include <iostream>
#include <vector>
using namespace std;
class ShellSort
{ public:
void sort(vector<int>& arr) {
int n = arr.size();
arr[j] = temp;
};
return 0;
}
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class MarksAnalyzer
{ private:
priority_queue<int> maxHeap; // For maximum marks priority_queue<int,
vector<int>, greater<int>> minHeap; // For minimum marks
public:
int getMaxMarks()
{ if (!
maxHeap.empty())
return maxHeap.top();
return -1;
int getMinMarks()
{ if (!
minHeap.empty())
return minHeap.top();
return -1;
}
};
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
file.close();
cout << "Student added successfully." << endl;
}
ifstream file("students.txt");
if (!file) {
cout << "File could not be opened." << endl;
return;
}
string line;
bool found = false;
if (file_roll_no == roll_no) {
found = true; // Found the student to delete
continue;
}
temp << file_roll_no << "," << name << "," <<
division << "," << address << endl;
}
file.close();
temp.close();
if (found) {
remove("students.txt"); // Delete the original file
rename("temp.txt", "students.txt"); // Rename the
temp file to the original file name
cout << "Student record deleted successfully." <<
endl;
} else {
cout << "Student with Roll Number " << roll_no << "
not found." << endl;
}
}
ifstream file("students.txt");
if (!file) {
cout << "File could not be opened." << endl;
return;
}
string line;
bool found = false;
while (getline(file, line)) {
stringstream ss(line);
int file_roll_no;
string name, division, address;
char comma;
if (file_roll_no == roll_no) {
found = true;
cout << "Roll Number: " << file_roll_no << endl;
cout << "Name: " << name << endl;
cout << "Division: " << division << endl;
cout << "Address: " << address << endl;
break;
}
}
file.close();
if (!found) {
cout << "Student with Roll Number " << roll_no << "
not found." << endl;
}
}
while (true) {
cout << "\nStudent Information System\n";
cout << "1. Add Student\n";
cout << "2. Delete Student\n";
cout << "3. Display Student\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
displayStudent();
break;
case 4:
cout << "Exiting the program." << endl;
return 0;
default:
cout << "Invalid choice. Please try again."
<< endl;
}
}
return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
dataFile.close();
indexFile.close();
cout << "Employee added successfully." << endl;
}
tempIndexFile.write(reinterpret_cast<char*>(&indexEntry),
sizeof(indexEntry));
if (found) {
// Delete the original files and rename the
temporary files
dataFile.close();
indexFile.close();
tempDataFile.close();
tempIndexFile.close();
remove("employee.dat");
remove("index.dat");
rename("temp_employee.dat", "employee.dat");
rename("temp_index.dat", "index.dat");
if (!dataFile || !indexFile) {
cout << "File could not be opened." << endl;
return;
}
if (!found) {
cout << "Employee with ID " << empID << " not
found." << endl;
}
dataFile.close();
indexFile.close();
}
while (true) {
cout << "\nEmployee Information System\n";
cout << "1. Add Employee\n";
cout << "2. Delete Employee\n";
cout << "3. Display Employee\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addEmployee();
break;
case 2:
deleteEmployee();
break;
case 3:
displayEmployee();
break;
case 4:
cout << "Exiting the program." << endl;
return 0;
default:
cout << "Invalid choice. Please try again."
<< endl;
}
}
return 0;
}
import random
self.player1_position = 0
self.player2_position = 0
self.turn = 1 # Player 1 starts
# Function to roll the dice (returns a value between 1
and 6)
def roll_dice(self):
return random.randint(1, 6)
winner = self.check_winner()
if winner:
print(winner)
break