Initial commit. Pruned old testing branch.
This commit is contained in:
parent
b9aa1b6eb9
commit
9e9ad1b42d
@ -1,211 +0,0 @@
|
||||
"""
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client's telephone number. Make use of two collision handling techniques and compare them using number of comparisons required to find a set of telephone numbers.
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
"""
|
||||
|
||||
# BEGINNING OF CODE
|
||||
class HashTable1:
|
||||
"""linear Probing Without Replacement"""
|
||||
|
||||
def __init__(self, size: int) -> None:
|
||||
self.record = []
|
||||
self.m = size
|
||||
|
||||
# initialize all records with 0
|
||||
for _ in range(size):
|
||||
self.record.append([0, ""]) # [tel,name]
|
||||
|
||||
def display_table(self) -> None:
|
||||
print("Hash table using linear probing (without replacement)")
|
||||
for i in range(len(self.record)):
|
||||
print(i, self.record[i])
|
||||
|
||||
def hash_function(self, tel: int) -> int:
|
||||
key = (tel % self.m)
|
||||
return key
|
||||
|
||||
def generate_table(self, recs: list[list]) -> None:
|
||||
for rec in recs:
|
||||
self.insert_rec(rec)
|
||||
|
||||
def insert_rec(self, rec: list) -> None:
|
||||
key = self.hash_function(rec[0])
|
||||
if (self.record[key][0] == 0):
|
||||
# no collision
|
||||
self.record[key][0] = rec[0]
|
||||
self.record[key][1] = rec[1]
|
||||
else: # collision
|
||||
while (self.record[key][0] != 0):
|
||||
key = ((key+1) % self.m)
|
||||
|
||||
self.record[key][0] = rec[0]
|
||||
self.record[key][1] = rec[1]
|
||||
|
||||
|
||||
class HashTable2:
|
||||
"""linear Probing With Replacement"""
|
||||
|
||||
def __init__(self, size: int) -> None:
|
||||
self.record = []
|
||||
self.m = size
|
||||
|
||||
# initialize all records with 0 and -1 link
|
||||
for _ in range(size):
|
||||
self.record.append([0, "", -1]) # [tel,name,link]
|
||||
|
||||
def display_table(self) -> None:
|
||||
print("Hash table using linar probing (with replacement)")
|
||||
for i in range(len(self.record)):
|
||||
print(i, self.record[i])
|
||||
|
||||
def hash_function(self, tel: int) -> int:
|
||||
key = (tel % self.m)
|
||||
return key
|
||||
|
||||
def generate_table(self, recs: list[list]) -> None:
|
||||
for rec in recs:
|
||||
self.insert_rec(rec)
|
||||
|
||||
def insert_rec(self, rec: list) -> None:
|
||||
key = self.hash_function(rec[0])
|
||||
if (self.record[key][0] == 0):
|
||||
# no collision
|
||||
self.record[key][0] = rec[0]
|
||||
self.record[key][1] = rec[1]
|
||||
self.record[key][2] = -1
|
||||
else: # collision
|
||||
if (self.hash_function(self.record[key][0]) == key):
|
||||
# create link
|
||||
last_elmt = key
|
||||
while (self.record[last_elmt][2] != -1):
|
||||
last_elmt = self.record[last_elmt][2]
|
||||
k = last_elmt
|
||||
while (self.record[k][0] != 0):
|
||||
k = ((k+1) % self.m)
|
||||
self.record[last_elmt][2] = k
|
||||
self.record[k][0] = rec[0]
|
||||
self.record[k][1] = rec[1]
|
||||
self.record[k][2] = -1
|
||||
else: # replacement
|
||||
# find last link
|
||||
for i in range(self.m):
|
||||
if (self.record[i][2] == key):
|
||||
prev_link_key = i
|
||||
|
||||
old_rec_tel = self.record[key][0]
|
||||
old_rec_name = self.record[key][1]
|
||||
old_rec_link = self.record[key][2]
|
||||
|
||||
self.record[key][0] = rec[0]
|
||||
self.record[key][1] = rec[1]
|
||||
self.record[key][2] = -1
|
||||
|
||||
k = key
|
||||
while (self.record[k][0] != 0):
|
||||
k = ((k+1) % self.m)
|
||||
|
||||
self.record[prev_link_key][2] = k
|
||||
self.record[k][0] = old_rec_tel
|
||||
self.record[k][1] = old_rec_name
|
||||
self.record[k][2] = old_rec_link
|
||||
|
||||
|
||||
class HashTable3:
|
||||
"""Double hashing"""
|
||||
|
||||
def __init__(self, size: int) -> None:
|
||||
self.record = []
|
||||
self.m = size
|
||||
|
||||
# initialize all records with 0
|
||||
for _ in range(size):
|
||||
self.record.append([0, ""]) # [tel,name]
|
||||
|
||||
if (size <= 3):
|
||||
self.prime = size
|
||||
else:
|
||||
prime = [2, 3]
|
||||
for i in range(size):
|
||||
for j in prime:
|
||||
if (i % j == 0):
|
||||
p = False
|
||||
break
|
||||
if (p):
|
||||
prime.append(i)
|
||||
self.prime = prime[-1]
|
||||
|
||||
def hash1(self, key: int) -> int:
|
||||
return (key % self.m)
|
||||
|
||||
def hash2(self, key: int) -> int:
|
||||
return (self.prime - (key % self.prime))
|
||||
|
||||
def display_table(self) -> None:
|
||||
print("Hash table using double hashing")
|
||||
for i in range(len(self.record)):
|
||||
print(i, self.record[i])
|
||||
|
||||
def generate_table(self, recs: list[list]) -> None:
|
||||
for rec in recs:
|
||||
self.insert_rec(rec)
|
||||
|
||||
def insert_rec(self, rec: list) -> None:
|
||||
i = 0
|
||||
key = self.hash1(rec[0])
|
||||
k2 = (key + i*self.hash2(rec[0])) % self.m
|
||||
while (self.record[k2][0] != 0):
|
||||
k2 = (key + i*self.hash2(rec[0])) % self.m
|
||||
i += 1
|
||||
self.record[k2][0] = rec[0]
|
||||
self.record[k2][1] = rec[1]
|
||||
|
||||
|
||||
def input_records(n: int) -> list[list]:
|
||||
records = []
|
||||
for i in range(n):
|
||||
name = input("Name of the person is:\t")
|
||||
tel = int(input("Telephone number is:\t"))
|
||||
records.append([tel, name])
|
||||
return records
|
||||
|
||||
|
||||
def main() -> None:
|
||||
n = int(input("Total number of records are:\t"))
|
||||
records = input_records(n)
|
||||
ch = 1
|
||||
while(ch != 5):
|
||||
print("----- MAIN MENU -----")
|
||||
print("1 -> Input records")
|
||||
print("2 -> Use linear Probing (without replacement)")
|
||||
print("3 -> Use linear Probing (with replacement)")
|
||||
print("4 -> Use double hashing")
|
||||
print("5 -> Exit")
|
||||
|
||||
ch = int(input("Choose an option (1-5):\t"))
|
||||
match (ch):
|
||||
case 1:
|
||||
n = int(input("Total number of records are:\t"))
|
||||
records = input_records(n)
|
||||
case 2:
|
||||
t1 = HashTable1(n)
|
||||
t1.generate_table(records)
|
||||
t1.display_table()
|
||||
case 3:
|
||||
t2 = HashTable2(n)
|
||||
t2.generate_table(records)
|
||||
t2.display_table()
|
||||
case 4:
|
||||
t3 = HashTable3(n)
|
||||
t3.generate_table(records)
|
||||
t3.display_table()
|
||||
case 5:
|
||||
print("\n## END OF CODE\n")
|
||||
case default:
|
||||
print("Please choose a valid option.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
# END OF CODE
|
@ -1,165 +0,0 @@
|
||||
"""
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: To create ADT that implement the "set" concept.
|
||||
a. Add (newElement) -> Place a value into the set
|
||||
b. Remove (element) -> Remove the value
|
||||
c. Contains (element) -> Return true if element is in collection
|
||||
d. Size () -> Return number of values in collection
|
||||
Iterator () -> Return an iterator used to loop over collection
|
||||
e. Intersection of two sets
|
||||
f. Union of two sets
|
||||
g. Difference between two sets
|
||||
h. Subset
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
"""
|
||||
|
||||
# BEGINNING OF CODE
|
||||
class sets:
|
||||
"""Implementation using list"""
|
||||
def __init__(self) -> None:
|
||||
self.data = []
|
||||
|
||||
def get_iter(self) -> iter:
|
||||
return iter(self.data)
|
||||
|
||||
def add(self, elmt: any) -> None:
|
||||
if (elmt not in self.data):
|
||||
self.data.append(elmt)
|
||||
|
||||
def remove(self, elmt: any) -> bool:
|
||||
if (elmt in self.data):
|
||||
self.data.remove(elmt)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def contains(self, elmt: any) -> bool:
|
||||
return (elmt in self.data)
|
||||
|
||||
def size(self) -> int:
|
||||
l = 0
|
||||
iter1 = self.get_iter()
|
||||
for elmt in iter1:
|
||||
l += 1
|
||||
return l
|
||||
|
||||
def intersection(self, input_set: any) -> list:
|
||||
ans = []
|
||||
set1 = list(input_set)
|
||||
for elmt in set1:
|
||||
if (elmt in self.data):
|
||||
ans.append(elmt)
|
||||
return ans
|
||||
|
||||
def union(self, input_set: any) -> list:
|
||||
ans = self.data.copy()
|
||||
set1 = list(input_set)
|
||||
for elmt in set1:
|
||||
if (elmt not in self.data):
|
||||
ans.append(elmt)
|
||||
return ans
|
||||
|
||||
def difference(self, input_set: any) -> list:
|
||||
ans = self.data.copy()
|
||||
set1 = list(input_set)
|
||||
for elmt in set1:
|
||||
if elmt in ans:
|
||||
ans.remove(elmt)
|
||||
return ans
|
||||
|
||||
def subset(self, input_set: any) -> bool:
|
||||
for e in input_set:
|
||||
if e not in self.data:
|
||||
return False
|
||||
return True
|
||||
|
||||
def display(self) -> None:
|
||||
str_set = str(self.data)
|
||||
print(str_set.replace('[', '{').replace(']', '}'))
|
||||
|
||||
|
||||
def input_set() -> set:
|
||||
ans = set()
|
||||
n = int(input("Total elements in set are:\t"))
|
||||
for i in range(n):
|
||||
ans.add(int(input(f"Enter element {i+1}:\t")))
|
||||
return ans
|
||||
|
||||
|
||||
def print_set(lst: list) -> None:
|
||||
str_set = str(lst)
|
||||
print(str_set.replace('[', '{').replace(']', '}'))
|
||||
|
||||
|
||||
def menu():
|
||||
print("----- MAIN MENU -----")
|
||||
print("0 -> Display main menu")
|
||||
print("1 -> Add new element")
|
||||
print("2 -> Remove an element")
|
||||
print("3 -> Search for an element")
|
||||
print("4 -> Display size of set")
|
||||
print("5 -> Intersection of sets")
|
||||
print("6 -> Union of sets")
|
||||
print("7 -> Difference of sets")
|
||||
print("8 -> Check if subset of a set")
|
||||
print("9 -> Display set")
|
||||
print("10 -> Exit")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
s1 = sets()
|
||||
choice = 1
|
||||
menu()
|
||||
while (choice != 10):
|
||||
choice = int(input("Choose an option (0-10):\t"))
|
||||
match (choice):
|
||||
case (1):
|
||||
n = int(input("Number to add:\t"))
|
||||
s1.add(n)
|
||||
print("Element added sucessfully.")
|
||||
case (2):
|
||||
n = int(input("Number to remove:\t"))
|
||||
if (s1.remove(n)):
|
||||
print("Element removed sucessfully.")
|
||||
else:
|
||||
print("Element not present in set.")
|
||||
case (3):
|
||||
n = int(input("Number to search:\t"))
|
||||
if (s1.contains(n)):
|
||||
print("Element is present in the set.")
|
||||
else:
|
||||
print("Element does not exist in the set.")
|
||||
case (4):
|
||||
print("Size of the set is: ", s1.size())
|
||||
case (5):
|
||||
s = input_set()
|
||||
print("Intersection of sets is: ", end="")
|
||||
print_set(s1.intersection(s))
|
||||
case (6):
|
||||
s = input_set()
|
||||
print("Union of sets is: ", end="")
|
||||
print_set(s1.union(s))
|
||||
case (7):
|
||||
s = input_set()
|
||||
print("Difference of sets is: ", end="")
|
||||
print_set(s1.difference(s))
|
||||
case (8):
|
||||
s = input_set()
|
||||
if (s1.subset(s)):
|
||||
print("Set is s subset.")
|
||||
else:
|
||||
print("Set is NOT a subset.")
|
||||
case (9):
|
||||
s1.display()
|
||||
case (10):
|
||||
print("\n## END OF CODE\n")
|
||||
case (0):
|
||||
menu()
|
||||
case default:
|
||||
print("Please choose a valid option (0-10)")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
# END OF CODE
|
@ -1,365 +0,0 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: A Dictionary stores keywords & its meanings. Provide facility for adding new keywords, deleting keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/descending order. Also find how many maximum comparisons may require for finding any keyword. Use Binary Search Tree for implementation.
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
public:
|
||||
string key, value;
|
||||
node *left, *right;
|
||||
node();
|
||||
node(string, string);
|
||||
};
|
||||
|
||||
node::node() {
|
||||
key = "";
|
||||
value = "";
|
||||
left = NULL;
|
||||
right = NULL;
|
||||
}
|
||||
|
||||
node::node(string key, string value) {
|
||||
this->key = key;
|
||||
this->value = value;
|
||||
left = NULL;
|
||||
right = NULL;
|
||||
}
|
||||
|
||||
class bst {
|
||||
public:
|
||||
node *root;
|
||||
bst();
|
||||
bst(string, string);
|
||||
bool insert(string, string);
|
||||
string search(string);
|
||||
bool update(string, string);
|
||||
bool delete_key(string);
|
||||
void display(node *cur);
|
||||
void display_asc(node *cur);
|
||||
};
|
||||
|
||||
bst::bst() { root = NULL; }
|
||||
|
||||
bst::bst(string key, string value) { root = new node(key, value); }
|
||||
|
||||
bool bst::insert(string key, string value) {
|
||||
if (root == NULL) {
|
||||
root = new node(key, value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
node *temp, *prev;
|
||||
prev = root;
|
||||
temp = root;
|
||||
while (temp != NULL) {
|
||||
prev = temp;
|
||||
if (temp->key == key) {
|
||||
return 0;
|
||||
} else if (temp->key < key) {
|
||||
temp = temp->right;
|
||||
} else {
|
||||
temp = temp->left;
|
||||
}
|
||||
}
|
||||
if (prev->key < key) {
|
||||
prev->right = new node(key, value);
|
||||
} else {
|
||||
prev->left = new node(key, value);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
string bst::search(string key) {
|
||||
node *temp = root;
|
||||
while (temp != NULL) {
|
||||
if (temp->key == key) {
|
||||
return temp->value;
|
||||
} else if (temp->key < key) {
|
||||
temp = temp->right;
|
||||
} else {
|
||||
temp = temp->left;
|
||||
}
|
||||
}
|
||||
return "\0"; // not present
|
||||
}
|
||||
|
||||
bool bst::update(string key, string value) {
|
||||
node *temp;
|
||||
temp = root;
|
||||
while (temp != NULL) {
|
||||
if (temp->key == key) {
|
||||
temp->value = value;
|
||||
return 1;
|
||||
} else if (temp->key < key) {
|
||||
temp = temp->right;
|
||||
} else {
|
||||
temp = temp->left;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool bst::delete_key(string key) {
|
||||
if (root == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
node *temp, *prev;
|
||||
prev = root;
|
||||
temp = root;
|
||||
|
||||
if (temp->key == key) {
|
||||
// delete root case
|
||||
if (temp->left == NULL && temp->right == NULL) {
|
||||
// no child
|
||||
root = NULL;
|
||||
delete temp;
|
||||
} else if (temp->left != NULL && temp->right == NULL) {
|
||||
// single child left
|
||||
root = temp->left;
|
||||
delete temp;
|
||||
} else if (temp->left == NULL && temp->right != NULL) {
|
||||
// single child right
|
||||
root = temp->right;
|
||||
delete temp;
|
||||
} else {
|
||||
// two child
|
||||
// using left largest
|
||||
node *l_temp = temp->left;
|
||||
node *l_prev = temp;
|
||||
if (l_temp->right == NULL) {
|
||||
l_prev->left = l_temp->left;
|
||||
} else {
|
||||
while (l_temp->right != NULL) {
|
||||
l_prev = l_temp;
|
||||
l_temp = l_temp->right;
|
||||
}
|
||||
l_prev->right = l_temp->left;
|
||||
}
|
||||
|
||||
// deleting temp
|
||||
l_temp->right = temp->right;
|
||||
l_temp->left = temp->left;
|
||||
root = l_temp;
|
||||
delete temp;
|
||||
}
|
||||
return 1;
|
||||
} else if (temp->key < key) {
|
||||
temp = temp->right;
|
||||
} else {
|
||||
temp = temp->left;
|
||||
}
|
||||
|
||||
while (temp != NULL) {
|
||||
// delete non root node
|
||||
if (temp->key == key) {
|
||||
if (temp->left == NULL && temp->right == NULL) {
|
||||
// no child
|
||||
if (temp->key < prev->key) {
|
||||
// left child
|
||||
prev->left = NULL;
|
||||
} else {
|
||||
// right child
|
||||
prev->right = NULL;
|
||||
}
|
||||
delete temp;
|
||||
} else if (temp->left != NULL && temp->right == NULL) {
|
||||
// single child left
|
||||
if (temp->key < prev->key) {
|
||||
// left child
|
||||
prev->left = temp->left;
|
||||
delete temp;
|
||||
} else {
|
||||
// right child
|
||||
prev->right = temp->left;
|
||||
delete temp;
|
||||
}
|
||||
} else if (temp->left == NULL && temp->right != NULL) {
|
||||
// single child right
|
||||
if (temp->key < prev->key) {
|
||||
// left child
|
||||
prev->left = temp->right;
|
||||
delete temp;
|
||||
} else {
|
||||
// right child
|
||||
prev->right = temp->right;
|
||||
delete temp;
|
||||
}
|
||||
} else {
|
||||
// two child
|
||||
// using left largest
|
||||
node *l_temp = temp->left;
|
||||
node *l_prev = temp;
|
||||
if (l_temp->right == NULL) {
|
||||
l_prev->left = l_temp->left;
|
||||
} else {
|
||||
while (l_temp->right != NULL) {
|
||||
l_prev = l_temp;
|
||||
l_temp = l_temp->right;
|
||||
}
|
||||
l_prev->right = l_temp->left;
|
||||
}
|
||||
|
||||
// deleting temp
|
||||
if (temp->key < prev->key) {
|
||||
// left child
|
||||
prev->left = l_temp;
|
||||
} else {
|
||||
// right child
|
||||
prev->right = l_temp;
|
||||
}
|
||||
l_temp->left = temp->left;
|
||||
l_temp->right = temp->right;
|
||||
delete temp;
|
||||
}
|
||||
return 1;
|
||||
} else if (temp->key < key) {
|
||||
prev = temp;
|
||||
temp = temp->right;
|
||||
} else {
|
||||
prev = temp;
|
||||
temp = temp->left;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bst::display(node *cur) {
|
||||
if (cur == NULL) {
|
||||
return;
|
||||
}
|
||||
display(cur->left);
|
||||
cout << cur->key << " : " << cur->value << endl;
|
||||
display(cur->right);
|
||||
}
|
||||
|
||||
void bst::display_asc(node *cur) {
|
||||
if (cur == NULL) {
|
||||
return;
|
||||
}
|
||||
display_asc(cur->right);
|
||||
cout << cur->key << " : " << cur->value << endl;
|
||||
display_asc(cur->left);
|
||||
}
|
||||
|
||||
/*
|
||||
// void printTree(node *root, int space = 0, int height = 20) {
|
||||
if (root == nullptr) return;
|
||||
space += height;
|
||||
printTree(root->right, space);
|
||||
cout << endl;
|
||||
for (int i = height; i < space; i++) cout << " ";
|
||||
cout << root->key << endl;
|
||||
printTree(root->left, space);
|
||||
}
|
||||
*/
|
||||
|
||||
int main() {
|
||||
bst tree;
|
||||
int ch;
|
||||
string k, v, ans;
|
||||
do {
|
||||
cout << "--- MAIN MENU ---" << endl;
|
||||
cout << "1 -> Insert" << endl;
|
||||
cout << "2 -> Search" << endl;
|
||||
cout << "3 -> Update" << endl;
|
||||
cout << "4 -> Delete" << endl;
|
||||
cout << "5 -> Display Descending" << endl;
|
||||
cout << "6 -> Display Ascending" << endl;
|
||||
cout << "0 -> Exit" << endl;
|
||||
cout << "Choose an option (0-6):\t";
|
||||
cin >> ch;
|
||||
switch (ch) {
|
||||
case 1:
|
||||
cout << "Key to insert:\t";
|
||||
cin >> k;
|
||||
cout << "Enter value:\t";
|
||||
cin >> v;
|
||||
if (tree.insert(k, v)) {
|
||||
cout << "Element inserted successfully." << endl;
|
||||
} else {
|
||||
cout << "Element already exists." << endl;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
cout << "Key to search:\t";
|
||||
cin >> k;
|
||||
ans = tree.search(k);
|
||||
if (ans == "\0") {
|
||||
cout << "Element not found" << endl;
|
||||
} else {
|
||||
cout << "Value is:\t" << ans << endl;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
cout << "Key to update:\t";
|
||||
cin >> k;
|
||||
cout << "Enter new value:";
|
||||
cin >> v;
|
||||
if (tree.update(k, v)) {
|
||||
cout << "Element updated successfully." << endl;
|
||||
} else {
|
||||
cout << "Element not present." << endl;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
cout << "Key to delete:\t";
|
||||
cin >> k;
|
||||
if (tree.delete_key(k)) {
|
||||
cout << "Element deleted successfully." << endl;
|
||||
} else {
|
||||
cout << "Element not present." << endl;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
cout << "Data in descending order is:\t" << endl;
|
||||
tree.display(tree.root);
|
||||
break;
|
||||
case 6:
|
||||
cout << "Data in ascending order is:\t" << endl;
|
||||
tree.display_asc(tree.root);
|
||||
break;
|
||||
case 0:
|
||||
cout << "\n// END OF CODE\n\n";
|
||||
break;
|
||||
default:
|
||||
cout << "Please choose a valid option (0-6)." << endl;
|
||||
break;
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
||||
|
||||
/*
|
||||
Sample output:
|
||||
|
||||
Choose an option (0-6): 1
|
||||
Key to insert: Test1
|
||||
Enter value: 8
|
||||
Element inserted successfully.
|
||||
|
||||
Choose an option (0-6): 1
|
||||
Key to insert: Test2
|
||||
Enter value: 9
|
||||
Element inserted successfully.
|
||||
|
||||
Choose an option (0-6): 5
|
||||
Data in descending order is:
|
||||
Test1 : 8
|
||||
Test2 : 9
|
||||
|
||||
Choose an option (0-6): 0
|
||||
|
||||
// END OF CODE
|
||||
|
||||
*/
|
@ -1,242 +0,0 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method.
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define MAX_NUM_OF_CHILD 5
|
||||
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
public:
|
||||
string data;
|
||||
node* child[MAX_NUM_OF_CHILD];
|
||||
int size;
|
||||
|
||||
node() {
|
||||
size = 0;
|
||||
for (int i = 0; i < MAX_NUM_OF_CHILD; i++) {
|
||||
child[i] = NULL;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class BookTree {
|
||||
node* root = NULL;
|
||||
|
||||
void printTree(node*& curnode, int tab = 0) {
|
||||
if (curnode == NULL) return;
|
||||
|
||||
for (int i = 0; i < tab; i++) cout << '\t';
|
||||
switch (tab) {
|
||||
case 0:
|
||||
cout << "Book: ";
|
||||
break;
|
||||
|
||||
case 1:
|
||||
cout << "Chapter: ";
|
||||
break;
|
||||
|
||||
case 2:
|
||||
cout << "Section: ";
|
||||
break;
|
||||
|
||||
case 3:
|
||||
cout << "Subsection: ";
|
||||
break;
|
||||
}
|
||||
cout << curnode->data << endl;
|
||||
for (int i = 0; i < MAX_NUM_OF_CHILD; i++) {
|
||||
printTree(curnode->child[i], tab + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
BookTree() {
|
||||
root = new node;
|
||||
cout << "Name of the book is:\t";
|
||||
// cin.ignore();
|
||||
getline(cin, root->data);
|
||||
}
|
||||
|
||||
void insertChap(string chName) {
|
||||
if (root->size >= MAX_NUM_OF_CHILD) {
|
||||
cout << "Chapter limit has been reached. Cannot add more chapters." << endl;
|
||||
return;
|
||||
} else {
|
||||
for (int i = 0; i < root->size; i++) {
|
||||
if (chName == root->child[i]->data) {
|
||||
cout << "Chapter already exists." << endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
root->child[root->size] = new node;
|
||||
root->child[root->size]->data = chName;
|
||||
cout << "Chapter has been added successfully." << endl;
|
||||
(root->size)++;
|
||||
}
|
||||
}
|
||||
|
||||
void insertSec(string secName, int chIndex) {
|
||||
node* chapter = root->child[chIndex];
|
||||
if ((chapter->size) >= MAX_NUM_OF_CHILD) {
|
||||
cout << "Section limit has been reached. Cannot add more sections."
|
||||
<< endl;
|
||||
return;
|
||||
} else {
|
||||
for (int i = 0; i < chapter->size; i++) {
|
||||
if (secName == chapter->child[i]->data) {
|
||||
cout << "Section already exists." << endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
chapter->child[chapter->size] = new node;
|
||||
chapter->child[chapter->size]->data = secName;
|
||||
cout << "Section has been added successfully." << endl;
|
||||
(chapter->size)++;
|
||||
}
|
||||
}
|
||||
|
||||
void insertSubSec(string subSecName, int chIndex, int secIndex) {
|
||||
node* chapter = root->child[chIndex];
|
||||
if (secIndex >= (chapter->size) || secIndex < 0) {
|
||||
cout << "Section not found. Please enter a valid section index." << endl;
|
||||
}
|
||||
node* section = chapter->child[secIndex];
|
||||
if ((section->size) >= MAX_NUM_OF_CHILD) {
|
||||
cout << "Sub-section limit has been reached. Cannot add more sub-sections."
|
||||
<< endl;
|
||||
return;
|
||||
} else {
|
||||
for (int i = 0; i < section->size; i++) {
|
||||
if (subSecName == section->child[i]->data) {
|
||||
cout << "Sub-section already added." << endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
section->child[section->size] = new node;
|
||||
section->child[section->size]->data = subSecName;
|
||||
cout << "Sub-section added successfully." << endl;
|
||||
(section->size)++;
|
||||
}
|
||||
}
|
||||
|
||||
int getChapIndex() {
|
||||
int chIndex;
|
||||
if (root->size <= 0) {
|
||||
cout << "No chapters found." << endl;
|
||||
return -1;
|
||||
}
|
||||
retryChindex:
|
||||
cout << "Chapters" << endl;
|
||||
for (int i = 0; i < root->size; i++) {
|
||||
cout << i + 1 << "." << root->child[i]->data << endl;
|
||||
}
|
||||
cout << "Chapter index is:\t";
|
||||
cin >> chIndex;
|
||||
chIndex--;
|
||||
if (chIndex >= (root->size) || chIndex < 0) {
|
||||
cout << "Chapter not found. Please enter a valid chapter index." << endl;
|
||||
goto retryChindex;
|
||||
}
|
||||
return chIndex;
|
||||
}
|
||||
|
||||
int getSecIndex(int chIndex) {
|
||||
int secIndex;
|
||||
node* chapter = root->child[chIndex];
|
||||
if (chapter->size <= 0) {
|
||||
cout << "No sections found." << endl;
|
||||
return -1;
|
||||
}
|
||||
retrySecindex:
|
||||
cout << "Sections" << endl;
|
||||
for (int i = 0; i < chapter->size; i++) {
|
||||
cout << i + 1 << "." << chapter->child[i]->data << endl;
|
||||
}
|
||||
cout << "Section index is:\t";
|
||||
cin >> secIndex;
|
||||
secIndex--;
|
||||
|
||||
if (secIndex >= (chapter->size) || secIndex < 0) {
|
||||
cout << "Section not found. Please enter a valid section index." << endl;
|
||||
goto retrySecindex;
|
||||
}
|
||||
|
||||
return secIndex;
|
||||
}
|
||||
|
||||
void disaply_tree() { printTree(root); }
|
||||
};
|
||||
|
||||
int main() {
|
||||
BookTree tree;
|
||||
int choice;
|
||||
string ChName, secname, subsecname;
|
||||
int chindex, secindex;
|
||||
do {
|
||||
cout << "----- MAIN MENU -----" << endl;
|
||||
cout << "1 -> Insert Chapter" << endl;
|
||||
cout << "2 -> Insert Section" << endl;
|
||||
cout << "3 -> Insert Subsection" << endl;
|
||||
cout << "4 -> Display Tree" << endl;
|
||||
cout << "5 -> Exit" << endl;
|
||||
cout << "Choose an option (1-5):\t";
|
||||
cin >> choice;
|
||||
|
||||
switch (choice) {
|
||||
case (1):
|
||||
cout << "Name of the new chapter is:\t";
|
||||
cin.ignore();
|
||||
getline(cin, ChName);
|
||||
|
||||
tree.insertChap(ChName);
|
||||
break;
|
||||
case (2):
|
||||
chindex = tree.getChapIndex();
|
||||
if (chindex == -1) {
|
||||
break;
|
||||
}
|
||||
cout << "Name of the new section is:\t";
|
||||
cin.ignore();
|
||||
getline(cin, secname);
|
||||
tree.insertSec(secname, chindex);
|
||||
break;
|
||||
case (3):
|
||||
chindex = tree.getChapIndex();
|
||||
if (chindex == -1) {
|
||||
break;
|
||||
}
|
||||
secindex = tree.getSecIndex(chindex);
|
||||
if (secindex == -1) {
|
||||
break;
|
||||
}
|
||||
cout << "Name of the new sub-section is:\t";
|
||||
cin.ignore();
|
||||
getline(cin, subsecname);
|
||||
tree.insertSubSec(subsecname, chindex, secindex);
|
||||
|
||||
break;
|
||||
case (4):
|
||||
tree.disaply_tree();
|
||||
break;
|
||||
case (5):
|
||||
cout << "\n// END OF CODE\n\n";
|
||||
break;
|
||||
default:
|
||||
cout << "Please choose a valid option (1-5)." << endl;
|
||||
break;
|
||||
}
|
||||
|
||||
} while (choice != 5);
|
||||
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
@ -1,112 +0,0 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: Construct an expression tree from the given prefix expression eg. +--a*bc/def and traverse it using postordertraversal(non recursive) and then delete the entire tree.
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
public:
|
||||
char data;
|
||||
node *left, *right;
|
||||
node() {
|
||||
left = NULL;
|
||||
right = NULL;
|
||||
}
|
||||
|
||||
node(char ch) {
|
||||
data = ch;
|
||||
left = NULL;
|
||||
right = NULL;
|
||||
}
|
||||
};
|
||||
|
||||
class tree {
|
||||
private:
|
||||
node *root;
|
||||
stack<node *> nodestack;
|
||||
|
||||
bool isOperator(char ch) {
|
||||
switch (ch) {
|
||||
case '+':
|
||||
case '-':
|
||||
case '*':
|
||||
case '/':
|
||||
return 1;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void insert(char ch) {
|
||||
if (isOperator(ch)) {
|
||||
node *newnode = new node(ch);
|
||||
newnode->left = nodestack.top();
|
||||
nodestack.pop();
|
||||
newnode->right = nodestack.top();
|
||||
nodestack.pop();
|
||||
nodestack.push(newnode);
|
||||
} else {
|
||||
node *newnode = new node(ch);
|
||||
nodestack.push(newnode);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void input_prefix(string exp) {
|
||||
for (int i = exp.length() - 1; i > -1; i--) {
|
||||
insert(exp[i]);
|
||||
}
|
||||
root = nodestack.top();
|
||||
nodestack.pop();
|
||||
}
|
||||
|
||||
void display_postfix() {
|
||||
node *temp = root;
|
||||
|
||||
stack<node *> stack1, stack2;
|
||||
|
||||
stack1.push(root);
|
||||
|
||||
while (!stack1.empty()) {
|
||||
temp = stack1.top();
|
||||
stack1.pop();
|
||||
stack2.push(temp);
|
||||
if (temp->left != NULL) {
|
||||
stack1.push(temp->left);
|
||||
}
|
||||
if (temp->right != NULL) {
|
||||
stack1.push(temp->right);
|
||||
}
|
||||
}
|
||||
while (!stack2.empty()) {
|
||||
temp = stack2.top();
|
||||
stack2.pop();
|
||||
cout << temp->data;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
tree exp_tree;
|
||||
string e;
|
||||
cout << "Prefix expression is:\t";
|
||||
cin >> e;
|
||||
exp_tree.input_prefix(e);
|
||||
exp_tree.display_postfix();
|
||||
return 0;
|
||||
}
|
||||
// +--a*bc/def
|
||||
// END OF CODE
|
@ -1,115 +0,0 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: Represent a given graph using adjacency matrix/list to perform DFS and using adjacency list to perform BFS. Use the map of the area around the college as the graph. Identify the prominent land marks as nodes and perform DFS and BFS on that.
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include <iostream>
|
||||
#include<vector>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
|
||||
void dfs(vector<vector<int>> &g, int start, vector<int> &vis) {
|
||||
vis[start] = 1;
|
||||
cout<<start<<" ";
|
||||
for(auto child: g[start]){
|
||||
if(!vis[child]){
|
||||
dfs(g,child,vis);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bfs(vector<vector<int>>& g, int start) {
|
||||
vector<int> vis(g.size(),0);
|
||||
queue<int> q;
|
||||
q.push(start);
|
||||
vis[start]=1;
|
||||
|
||||
while(!q.empty()){
|
||||
int cur=q.front();
|
||||
q.pop();
|
||||
cout<<cur<< " ";
|
||||
for(auto child: g[cur]){
|
||||
if(!vis[child]){
|
||||
q.push(child);
|
||||
vis[child]=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void displayAdjacencyMatrix(const vector<vector<int>>& graph) {
|
||||
int V = graph.size();
|
||||
vector<vector<int>> adjacencyMatrix(V, vector<int>(V, 0));
|
||||
|
||||
for (int i = 0; i < V; i++) {
|
||||
for (int j : graph[i]) {
|
||||
adjacencyMatrix[i][j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Adjacency Matrix:\n";
|
||||
for (int i = 0; i < V; i++) {
|
||||
for (int j = 0; j < V; j++) {
|
||||
cout << adjacencyMatrix[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int V,E;
|
||||
|
||||
cout << "Number of vertices:\t";
|
||||
cin >> V;
|
||||
vector<vector<int>> g;
|
||||
|
||||
|
||||
for(int i=0;i<V;i++){
|
||||
vector<int> temp;
|
||||
g.push_back(temp);
|
||||
}
|
||||
|
||||
cout << "Number of edges:\t";
|
||||
cin >> E;
|
||||
|
||||
for(int i=0;i<E;i++){
|
||||
int a,b;
|
||||
cout<<"Start node:\t";
|
||||
cin>>a;
|
||||
cout<<"End node:\t";
|
||||
cin>>b;
|
||||
g[a].push_back(b);
|
||||
g[b].push_back(a);
|
||||
}
|
||||
displayAdjacencyMatrix(g);
|
||||
vector<int> vis(V,0);
|
||||
cout<<endl<<"DFS:\t";
|
||||
dfs(g,0,vis);
|
||||
cout<<endl<<"BFS:\t";
|
||||
bfs(g,0);
|
||||
cout<<"\n\n// END OF CODE\n\n";
|
||||
}
|
||||
// END OF CODE
|
||||
|
||||
/*
|
||||
SAMPLE OUTPUT:
|
||||
Number of vertices: 3
|
||||
Number of edges: 2
|
||||
Start node: 1
|
||||
End node: 2
|
||||
Start node: 2
|
||||
End node: 3
|
||||
Adjacency Matrix:
|
||||
0 0 0
|
||||
0 0 1
|
||||
0 1 0
|
||||
|
||||
DFS: 0
|
||||
BFS: 0
|
||||
*/
|
@ -1,130 +0,0 @@
|
||||
// THIS CODE IS A LONG CODE.
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class graph {
|
||||
int no_of_vertices;
|
||||
int no_of_edges;
|
||||
vector<int> vertices;
|
||||
vector<vector<int>> adj_list;
|
||||
vector<vector<int>> adj_matrix;
|
||||
|
||||
public:
|
||||
graph() {
|
||||
cout << "Number of vertices are:\t";
|
||||
cin >> no_of_vertices;
|
||||
cout << "Number of edges are:\t";
|
||||
cin >> no_of_edges;
|
||||
vertices = {};
|
||||
adj_list.resize(no_of_vertices);
|
||||
for (int i = 0; i < no_of_vertices; i++) {
|
||||
adj_matrix.push_back(vector<int>(no_of_vertices, 0));
|
||||
}
|
||||
|
||||
for (int i = 0; i < no_of_vertices; i++) {
|
||||
add_vertex(i);
|
||||
}
|
||||
for (int i = 0; i < no_of_edges; i++) {
|
||||
cout << "Vertices of the edge are (Vi, Vj):\t";
|
||||
int vertex1, vertex2;
|
||||
cin >> vertex1 >> vertex2;
|
||||
add_edge(vertex1, vertex2);
|
||||
}
|
||||
}
|
||||
void add_vertex(int vertex) { vertices.push_back(vertex); }
|
||||
void add_edge(int vertex1, int vertex2) {
|
||||
adj_list[vertex1].push_back(vertex2);
|
||||
adj_list[vertex2].push_back(vertex1);
|
||||
adj_matrix[vertex1][vertex2] = 1;
|
||||
adj_matrix[vertex2][vertex1] = 1;
|
||||
}
|
||||
void print_adj_list() {
|
||||
for (int i = 0; i < no_of_vertices; i++) {
|
||||
cout << vertices[i] << "->";
|
||||
for (int j = 0; j < adj_list[i].size() - 1; j++) {
|
||||
cout << adj_list[i][j] << "->";
|
||||
}
|
||||
cout << adj_list[i].back() << endl;
|
||||
}
|
||||
}
|
||||
void print_adj_matrix() {
|
||||
for (int i = 0; i < no_of_vertices; i++) {
|
||||
for (int j = 0; j < no_of_vertices; j++) {
|
||||
cout << adj_matrix[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void bfs(int start_vertex) {
|
||||
vector<int> visited(no_of_vertices, 0);
|
||||
// vector<int> queue;
|
||||
queue<int> q;
|
||||
q.push(start_vertex);
|
||||
visited[start_vertex] = 1;
|
||||
while (!q.empty()) {
|
||||
int vertex = q.front();
|
||||
cout << vertex << " ";
|
||||
q.pop();
|
||||
for (int i = 0; i < adj_list[vertex].size(); i++) {
|
||||
if (visited[adj_list[vertex][i]] == 0) {
|
||||
q.push(adj_list[vertex][i]);
|
||||
visited[adj_list[vertex][i]] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dfs(int start_vertex) {
|
||||
vector<int> visited(no_of_vertices, 0);
|
||||
stack<int> s;
|
||||
s.push(start_vertex);
|
||||
visited[start_vertex] = 1;
|
||||
while (!s.empty()) {
|
||||
int vertex = s.top();
|
||||
s.pop();
|
||||
cout << vertex << " ";
|
||||
for (int i = 0; i < no_of_vertices; i++) {
|
||||
if (i != vertex && adj_matrix[vertex][i] == 1 &&
|
||||
visited[i] == 0) {
|
||||
s.push(i);
|
||||
visited[i] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
graph g;
|
||||
cout << endl;
|
||||
cout << "Adjacency list representation is:\t" << endl;
|
||||
g.print_adj_list();
|
||||
cout << endl;
|
||||
cout << "Matrix representation is:\t" << endl;
|
||||
g.print_adj_matrix();
|
||||
cout << endl;
|
||||
cout << "Enter the starting vertex for BFS:\t";
|
||||
int start_vertex;
|
||||
cin >> start_vertex;
|
||||
cout << "BFS:\t";
|
||||
g.bfs(start_vertex);
|
||||
cout << endl;
|
||||
cout << endl;
|
||||
cout << "Enter the starting vertex for DFS:\t";
|
||||
cin >> start_vertex;
|
||||
cout << "DFS:\t";
|
||||
g.dfs(start_vertex);
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
||||
|
||||
// 7 11 0 1 0 3 1 5 1 2 1 3 2 5 1 6 2 3 2 4 3 4 4 6 1 1
|
@ -1,171 +0,0 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: You have a business with several offices; you want to lease phone lines to connect them up with each other; and the phone company charges different amounts of money to connect different pairs of cities. You want a set of lines that connects all your offices with a minimum total cost. Solve the problem by suggesting appropriate data structures.
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#define MAX_NUM_CITIES 10
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct edge {
|
||||
int start;
|
||||
int end;
|
||||
int wt;
|
||||
};
|
||||
|
||||
class graph {
|
||||
int adj_mat[MAX_NUM_CITIES][MAX_NUM_CITIES] = {0};
|
||||
string city_names[MAX_NUM_CITIES];
|
||||
int city_count;
|
||||
edge mst[MAX_NUM_CITIES - 1];
|
||||
void add_to_list(vector<edge> &, edge);
|
||||
int cost;
|
||||
|
||||
public:
|
||||
graph();
|
||||
|
||||
void prims_algo(int);
|
||||
void display_mst();
|
||||
};
|
||||
|
||||
void graph::add_to_list(vector<edge> &list, edge e) {
|
||||
list.push_back(e);
|
||||
for (int i = list.size() - 1; i > 0; i--) {
|
||||
if (list[i].wt < list[i - 1].wt) {
|
||||
swap(list[i], list[i - 1]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
graph::graph() {
|
||||
cost = 0;
|
||||
cout << "Number of cities are (1-" << MAX_NUM_CITIES << "):\t";
|
||||
cin >> city_count;
|
||||
city_count = (city_count > MAX_NUM_CITIES) ? MAX_NUM_CITIES : city_count;
|
||||
|
||||
for (int i = 0; i < city_count; i++) {
|
||||
cout << "Enter city:\n" << i + 1 << ":\t";
|
||||
cin >> city_names[i];
|
||||
}
|
||||
|
||||
// initialize all matrix with max value
|
||||
for (int i = 0; i < city_count; i++)
|
||||
for (int j = 0; j < city_count; j++) adj_mat[i][j] = INT32_MAX;
|
||||
|
||||
int num_pairs;
|
||||
cout << "Number of city pairs are:\t";
|
||||
cin >> num_pairs;
|
||||
cout << "City codes are:\t" << endl;
|
||||
for (int i = 0; i < city_count; i++) {
|
||||
cout << i << " - " << city_names[i] << endl;
|
||||
}
|
||||
int x, y, wt;
|
||||
for (int i = 0; i < num_pairs; i++) {
|
||||
cout << "Enter pair:\n" << i + 1 << ":\t";
|
||||
cin >> x >> y;
|
||||
cout << "Enter cost between city " << city_names[x] << " & city "
|
||||
<< city_names[y] << ":\t";
|
||||
cin >> wt;
|
||||
adj_mat[x][y] = wt;
|
||||
adj_mat[y][x] = wt;
|
||||
}
|
||||
}
|
||||
|
||||
void graph::prims_algo(int start) {
|
||||
bool visited[MAX_NUM_CITIES] = {0};
|
||||
int visited_count = 1;
|
||||
visited[start] = 1;
|
||||
vector<edge> adj;
|
||||
for (int i = 0; i < city_count; i++) {
|
||||
if (adj_mat[start][i] != INT32_MAX) {
|
||||
edge e;
|
||||
e.start = start;
|
||||
e.end = i;
|
||||
e.wt = adj_mat[start][i];
|
||||
add_to_list(adj, e);
|
||||
}
|
||||
}
|
||||
|
||||
while (visited_count != city_count) {
|
||||
edge m = adj.front();
|
||||
adj.erase(adj.begin());
|
||||
if (!visited[m.end]) {
|
||||
mst[visited_count - 1] = m;
|
||||
cost += m.wt;
|
||||
for (int i = 0; i < city_count; i++) {
|
||||
if (adj_mat[m.end][i] != INT32_MAX) {
|
||||
edge e;
|
||||
e.start = m.end;
|
||||
e.end = i;
|
||||
e.wt = adj_mat[e.start][i];
|
||||
add_to_list(adj, e);
|
||||
}
|
||||
}
|
||||
visited[m.end] = 1;
|
||||
visited_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void graph::display_mst() {
|
||||
cout << "Most efficient network is:\t" << endl;
|
||||
|
||||
for (int i = 0; i < city_count - 1; i++) {
|
||||
cout << city_names[mst[i].start] << " to " << city_names[mst[i].end]
|
||||
<< " of weight " << mst[i].wt << endl;
|
||||
}
|
||||
cout << endl << "The cost of network is:\t" << cost << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// prims algo
|
||||
graph g;
|
||||
int start;
|
||||
cout << "Enter beginning city:\t";
|
||||
cin >> start;
|
||||
start = (start > MAX_NUM_CITIES - 1) ? 0 : start;
|
||||
g.prims_algo(start);
|
||||
g.display_mst();
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
||||
|
||||
/*
|
||||
SAMPLE OUTPUT:
|
||||
|
||||
Number of cities are (1-10): 3
|
||||
Enter city:
|
||||
1: Paris
|
||||
Enter city:
|
||||
2: Pune
|
||||
Enter city:
|
||||
3: Nagar
|
||||
Number of city pairs are: 2
|
||||
City codes are:
|
||||
0 - Paris
|
||||
1 - Pune
|
||||
2 - Nagar
|
||||
Enter pair:
|
||||
1: 1
|
||||
2
|
||||
Enter cost between city Pune & city Nagar: 5
|
||||
Enter pair:
|
||||
2: 0
|
||||
1
|
||||
Enter cost between city Paris & city Pune: 10
|
||||
Enter beginning city: Pune
|
||||
Most efficient network is:
|
||||
Paris to Pune of weight 10
|
||||
Pune to Nagar of weight 5
|
||||
|
||||
The cost of network is: 15
|
||||
*/
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: Given sequence k = k1 <k2 < … <kn of n sorted keys, with a search probability pi for each key ki . Build the Binary search tree that has the least search cost given the access probability for each key?
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
#define MAX 10
|
||||
int find(int,int);
|
||||
void print(int,int);
|
||||
int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],i,j,k,n,m;
|
||||
char idnt[7][10];
|
||||
|
||||
int main()
|
||||
{
|
||||
cout<<"Number of identifiers:\t";
|
||||
cin>>n;
|
||||
cout<<"Enter identifiers:"<<endl;
|
||||
for(i=1;i<=n;i++) {
|
||||
cout<<"Identifier "<<i<<":\t";
|
||||
cin>>idnt[i];
|
||||
}
|
||||
cout<<"Enter success probability for identifiers:\n";
|
||||
for(i=1;i<=n;i++) {
|
||||
cout<<"Identifier "<<i<<":\t";
|
||||
cin>>p[i];
|
||||
}
|
||||
cout<<"Enter failure probability for identifiers:\n";
|
||||
for(i=0;i<=n;i++) {
|
||||
cout<<"Identifier "<<i<<":\t";
|
||||
cin>>q[i];
|
||||
}
|
||||
cout<<"\n Weight Cost Root \n";
|
||||
|
||||
for(i=0;i<=n;i++)
|
||||
{
|
||||
w[i][i]=q[i];
|
||||
c[i][i]=r[i][i]=0;
|
||||
cout<<"\n"<<w[i][i]<<" "<<c[i][i]<<" "<<r[i][i];
|
||||
cout<<"-------------------------------------------------------------------------";
|
||||
}
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
j=i+1;
|
||||
w[i][j]= p[j]+q[i]+q[j]; // w(i,j)= p[j]+q[i]+w(i,j-1)
|
||||
c[i][j]=q[i]+c[i][j-1]+c[j][j]; // c(i,j)= min { c (i,k-1)+c(k+1, j) }+ w(i,j))
|
||||
r[i][j]=j;
|
||||
cout<<"\n"<<w[i][j]<<" "<<c[i][j]<<" "<<r[i][j];
|
||||
cout<<"-------------------------------------------------------------------------";
|
||||
}
|
||||
for(m=2;m<=n;m++)
|
||||
{
|
||||
for(i=0;i<=n-m;i++)
|
||||
{
|
||||
j=i+m;
|
||||
w[i][j]=w[i][j-1]+p[j]+q[j];
|
||||
k=find(i,j);
|
||||
r[i][j]=k;
|
||||
c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
|
||||
cout<<"\n"<<w[i][j]<<" "<<c[i][j]<<" "<<r[i][j];
|
||||
}
|
||||
}
|
||||
cout<<"-------------------------------------------------------------------------";
|
||||
cout<<endl<<endl<<"THE FINAL OBST IS:";
|
||||
print(0,n);
|
||||
return 0;
|
||||
}
|
||||
int find(int i,int j)
|
||||
{
|
||||
int min=2000,m,l; //c[i][j];
|
||||
for(m=i+1;m<=j;m++)
|
||||
if(c[i][m-1]+c[m][j]<min)
|
||||
{
|
||||
min=c[i][m-1]+c[m][j];
|
||||
l=m;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
void print(int i,int j)
|
||||
{
|
||||
if(i<j)
|
||||
cout<<"\n"<<idnt[r[i][j]];
|
||||
else
|
||||
return;
|
||||
print(i,r[i][j]-1);
|
||||
print(r[i][j],j);
|
||||
}
|
||||
// END OF CODE
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: Given sequence k = k1 <k2 < … <kn of n sorted keys, with a search probability pi for each key ki . Build the Binary search tree that has the least search cost given the access probability for each key?
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
#define MAX 10
|
||||
int find(int,int);
|
||||
void print(int,int);
|
||||
int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],i,j,k,n,m;
|
||||
char idnt[7][10];
|
||||
|
||||
int main()
|
||||
{
|
||||
cout<<"Number of identifiers:\t";
|
||||
cin>>n;
|
||||
cout<<"Enter identifiers:"<<endl;
|
||||
for(i=1;i<=n;i++) {
|
||||
cout<<"Identifier "<<i<<":\t";
|
||||
cin>>idnt[i];
|
||||
}
|
||||
cout<<"Enter success probability for identifiers:\n";
|
||||
for(i=1;i<=n;i++) {
|
||||
cout<<"Identifier "<<i<<":\t";
|
||||
cin>>p[i];
|
||||
}
|
||||
cout<<"Enter failure probability for identifiers:\n";
|
||||
for(i=0;i<=n;i++) {
|
||||
cout<<"Identifier "<<i<<":\t";
|
||||
cin>>q[i];
|
||||
}
|
||||
cout<<"\n Weight Cost Root \n";
|
||||
|
||||
for(i=0;i<=n;i++)
|
||||
{
|
||||
w[i][i]=q[i];
|
||||
c[i][i]=r[i][i]=0;
|
||||
cout<<"\n"<<w[i][i]<<" "<<c[i][i]<<" "<<r[i][i];
|
||||
cout<<"-------------------------------------------------------------------------";
|
||||
}
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
j=i+1;
|
||||
w[i][j]= p[j]+q[i]+q[j]; // w(i,j)= p[j]+q[i]+w(i,j-1)
|
||||
c[i][j]=q[i]+c[i][j-1]+c[j][j]; // c(i,j)= min { c (i,k-1)+c(k+1, j) }+ w(i,j))
|
||||
r[i][j]=j;
|
||||
cout<<"\n"<<w[i][j]<<" "<<c[i][j]<<" "<<r[i][j];
|
||||
cout<<"-------------------------------------------------------------------------";
|
||||
}
|
||||
for(m=2;m<=n;m++)
|
||||
{
|
||||
for(i=0;i<=n-m;i++)
|
||||
{
|
||||
j=i+m;
|
||||
w[i][j]=w[i][j-1]+p[j]+q[j];
|
||||
k=find(i,j);
|
||||
r[i][j]=k;
|
||||
c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
|
||||
cout<<"\n"<<w[i][j]<<" "<<c[i][j]<<" "<<r[i][j];
|
||||
}
|
||||
}
|
||||
cout<<"-------------------------------------------------------------------------";
|
||||
cout<<endl<<endl<<"THE FINAL OBST IS:";
|
||||
print(0,n);
|
||||
return 0;
|
||||
}
|
||||
int find(int i,int j)
|
||||
{
|
||||
int min=2000,m,l; //c[i][j];
|
||||
for(m=i+1;m<=j;m++)
|
||||
if(c[i][m-1]+c[m][j]<min)
|
||||
{
|
||||
min=c[i][m-1]+c[m][j];
|
||||
l=m;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
void print(int i,int j)
|
||||
{
|
||||
if(i<j)
|
||||
cout<<"\n"<<idnt[r[i][j]];
|
||||
else
|
||||
return;
|
||||
print(i,r[i][j]-1);
|
||||
print(r[i][j],j);
|
||||
}
|
||||
// END OF CODE
|
@ -1,358 +0,0 @@
|
||||
// THIS IS A LONG CODE
|
||||
// Problem Statement: A Dictionary stores keywords & its meanings. Provide facility for adding new keywords, deleting keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/ Descending order. Also find how many maximum comparisons may require for finding any keyword. Use Height balance tree and find the complexity for finding a keyword.
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
public:
|
||||
string key;
|
||||
string value;
|
||||
node* left;
|
||||
node* right;
|
||||
int height;
|
||||
node(string key = "", string value = "") {
|
||||
this->key = key;
|
||||
this->value = value;
|
||||
this->left = NULL;
|
||||
this->right = NULL;
|
||||
this->height = 1;
|
||||
}
|
||||
};
|
||||
|
||||
class AVL {
|
||||
node* root = NULL;
|
||||
int height(node* n) {
|
||||
if (n == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return n->height;
|
||||
}
|
||||
|
||||
int balanceFactor(node* n) {
|
||||
if (n == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return (height(n->left) - height(n->right));
|
||||
}
|
||||
|
||||
node* rightRotate(node* y) {
|
||||
// get changing nodes
|
||||
node* x = y->left;
|
||||
node* t2 = x->right;
|
||||
|
||||
// change the nodes
|
||||
x->right = y;
|
||||
y->left = t2;
|
||||
|
||||
// change height
|
||||
x->height = 1 + max(height(x->left), height(x->right));
|
||||
y->height = 1 + max(height(y->left), height(y->right));
|
||||
|
||||
// return
|
||||
return x;
|
||||
}
|
||||
node* leftRotate(node* x) {
|
||||
// get changing nodes
|
||||
node* y = x->right;
|
||||
node* t2 = y->left;
|
||||
|
||||
// change the nodes
|
||||
y->left = x;
|
||||
x->right = t2;
|
||||
|
||||
// change height
|
||||
x->height = 1 + max(height(x->left), height(x->right));
|
||||
y->height = 1 + max(height(y->left), height(y->right));
|
||||
|
||||
// return
|
||||
return y;
|
||||
}
|
||||
|
||||
// recursive insertion function which will be called by insert function
|
||||
node* insertion(node* temp, string key, string value) {
|
||||
if (temp == NULL) {
|
||||
node* nn = new node(key, value);
|
||||
return nn;
|
||||
}
|
||||
if (key < temp->key) {
|
||||
temp->left = insertion(temp->left, key, value);
|
||||
}
|
||||
if (key > temp->key) {
|
||||
temp->right = insertion(temp->right, key, value);
|
||||
}
|
||||
temp->height = 1 + max(height(temp->left), height(temp->right));
|
||||
int bf = balanceFactor(temp);
|
||||
// LL Rotation
|
||||
// if (bf > 1 && key < temp->left->key) {
|
||||
if (bf > 1 && balanceFactor(root->left) >= 0) {
|
||||
return rightRotate(temp);
|
||||
}
|
||||
// RR Rotation
|
||||
// if (bf < -1 && key > temp->right->key) {
|
||||
if (bf < -1 && balanceFactor(root->right) <= 0) {
|
||||
return leftRotate(temp);
|
||||
}
|
||||
// LR Rotation
|
||||
// if (bf > 1 && key > temp->left->key) {
|
||||
if (bf > 1 && balanceFactor(root->left) < 0) {
|
||||
temp->left = leftRotate(temp->left);
|
||||
return rightRotate(temp);
|
||||
}
|
||||
// RL Rotation
|
||||
// if (bf < -1 && key < temp->right->key) {
|
||||
if (bf < -1 && balanceFactor(root->right) > 0) {
|
||||
temp->right = rightRotate(temp->right);
|
||||
return leftRotate(temp);
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
void ascending(node* n) {
|
||||
if (n != NULL) {
|
||||
ascending(n->left);
|
||||
cout << n->key << ":" << n->value << endl;
|
||||
ascending(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
void descending(node* n) {
|
||||
if (n != NULL) {
|
||||
descending(n->right);
|
||||
cout << n->key << ":" << n->value << endl;
|
||||
descending(n->left);
|
||||
}
|
||||
}
|
||||
bool isPresent(string key) {
|
||||
node* temp = root;
|
||||
while (temp != NULL) {
|
||||
if (temp->key == key) {
|
||||
return true;
|
||||
} else if (temp->key < key) {
|
||||
temp = temp->right;
|
||||
} else {
|
||||
temp = temp->left;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
node* minNode(node* root) {
|
||||
node* n = root;
|
||||
while (n->left != NULL) {
|
||||
n = n->left;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
node* remove(node*& temp_root, string key) {
|
||||
// step1 bst deletion
|
||||
if (temp_root == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (key < temp_root->key) {
|
||||
temp_root->left = remove(temp_root->left, key);
|
||||
} else if (key > temp_root->key) {
|
||||
temp_root->right = remove(temp_root->right, key);
|
||||
} else {
|
||||
node* temp1 = temp_root;
|
||||
// one and no child
|
||||
if (temp_root->left == NULL || temp_root->right == NULL) {
|
||||
if (temp_root->left == NULL) {
|
||||
temp_root = temp_root->right;
|
||||
} else {
|
||||
temp_root = temp_root->left;
|
||||
}
|
||||
delete temp1;
|
||||
} else {
|
||||
// two child
|
||||
node* temp2 = minNode(temp_root->right);
|
||||
temp_root->key = temp2->key;
|
||||
temp_root->value = temp2->value;
|
||||
|
||||
temp_root->right = remove(temp_root->right, temp2->key);
|
||||
}
|
||||
}
|
||||
|
||||
// check if root is NULL
|
||||
if (temp_root == NULL) return temp_root;
|
||||
|
||||
// step2 avl balancing
|
||||
temp_root->height =
|
||||
1 + max(height(temp_root->left), height(temp_root->right));
|
||||
|
||||
int bf = balanceFactor(temp_root);
|
||||
|
||||
// LL Rotation
|
||||
// if (bf > 1 && key < temp_root->left->key) {
|
||||
if (bf > 1 && balanceFactor(root->left) >= 0) {
|
||||
return rightRotate(temp_root);
|
||||
}
|
||||
// RR Rotation
|
||||
// if (bf < -1 && key > temp_root->right->key) {
|
||||
if (bf < -1 && balanceFactor(root->right) <= 0) {
|
||||
return leftRotate(temp_root);
|
||||
}
|
||||
// LR Rotation
|
||||
// if (bf > 1 && key > temp_root->left->key) {
|
||||
if (bf > 1 && balanceFactor(root->left) < 0) {
|
||||
temp_root->left = leftRotate(temp_root->left);
|
||||
return rightRotate(temp_root);
|
||||
}
|
||||
// RL Rotation
|
||||
// if (bf < -1 && key < temp_root->right->key) {
|
||||
if (bf < -1 && balanceFactor(root->right) > 0) {
|
||||
temp_root->right = rightRotate(temp_root->right);
|
||||
return leftRotate(temp_root);
|
||||
}
|
||||
return temp_root;
|
||||
}
|
||||
|
||||
public:
|
||||
string search(string key) {
|
||||
node* temp = root;
|
||||
while (temp != NULL) {
|
||||
if (temp->key == key) {
|
||||
return temp->value;
|
||||
} else if (temp->key < key) {
|
||||
temp = temp->right;
|
||||
} else {
|
||||
temp = temp->left;
|
||||
}
|
||||
}
|
||||
return "\0";
|
||||
}
|
||||
|
||||
bool insert(string key, string value) {
|
||||
if (isPresent(key)) {
|
||||
return false;
|
||||
} else {
|
||||
root = insertion(root, key, value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool update(string key, string value) {
|
||||
node* temp = root;
|
||||
while (temp != NULL) {
|
||||
if (temp->key == key) {
|
||||
temp->value = value;
|
||||
return true;
|
||||
} else if (temp->key < key) {
|
||||
temp = temp->right;
|
||||
} else {
|
||||
temp = temp->left;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool remove(string key) {
|
||||
if (!isPresent(key)) {
|
||||
return false;
|
||||
} else {
|
||||
root = remove(root, key);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
void ascending() {
|
||||
if (root == NULL) {
|
||||
cout << "Tree is empty." << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "Ascending traversal is:\t" << endl;
|
||||
ascending(root);
|
||||
}
|
||||
|
||||
void descending() {
|
||||
if (root == NULL) {
|
||||
cout << "Tree is empty." << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "Descending traversal is:\t" << endl;
|
||||
descending(root);
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
// implement AVL tree
|
||||
AVL tree;
|
||||
int ch;
|
||||
string k, v, ans;
|
||||
do {
|
||||
cout << endl;
|
||||
cout << "--- MAIN MENU ---" << endl;
|
||||
cout << "1 -> Insert" << endl;
|
||||
cout << "2 -> Search" << endl;
|
||||
cout << "3 -> Update" << endl;
|
||||
cout << "4 -> Delete" << endl;
|
||||
cout << "5 -> Display Descending" << endl;
|
||||
cout << "6 -> Display Ascending" << endl;
|
||||
cout << "0 -> Exit" << endl;
|
||||
cout << "Choose an option (0-6):\t";
|
||||
cin >> ch;
|
||||
switch (ch) {
|
||||
case 1:
|
||||
cout << "Key to insert (word):\t";
|
||||
cin >> k;
|
||||
cout << "Value (meaning):\t";
|
||||
cin >> v;
|
||||
if (tree.insert(k, v)) {
|
||||
cout << "Element insertion successful." << endl;
|
||||
} else {
|
||||
cout << "Element already exisits." << endl;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
cout << "Key (word) to search:\t";
|
||||
cin >> k;
|
||||
ans = tree.search(k);
|
||||
if (ans == "\0") {
|
||||
cout << "Element not found." << endl;
|
||||
} else {
|
||||
cout << "Value is:\t" << ans << endl;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
cout << "Key (word) to update:\t";
|
||||
cin >> k;
|
||||
cout << "New value (meaning) is:\t";
|
||||
cin >> v;
|
||||
if (tree.update(k, v)) {
|
||||
cout << "Element updated." << endl;
|
||||
} else {
|
||||
cout << "Element does not exist." << endl;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
cout << "Key to delete:\t";
|
||||
cin >> k;
|
||||
if (tree.remove(k)) {
|
||||
cout << "Element deletion successful." << endl;
|
||||
} else {
|
||||
cout << "Element does not exist." << endl;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
cout << "Data in descending order:\t" << endl;
|
||||
tree.descending();
|
||||
break;
|
||||
case 6:
|
||||
cout << "Data in ascending order:\t" << endl;
|
||||
tree.ascending();
|
||||
break;
|
||||
case 0:
|
||||
cout << endl << "// END OF CODE" << endl;
|
||||
break;
|
||||
default:
|
||||
cout << "Please choose a valid option (1-6)." << endl;
|
||||
break;
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
@ -1,135 +0,0 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: Consider a scenario for Hospital to cater services to different kinds of patients as Serious (top priority), b) non-serious (medium priority), c) General Checkup (Least priority). Implement the priority queue to cater services to the patients.
|
||||
|
||||
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include <iostream>
|
||||
#define MAX_SIZE 10
|
||||
|
||||
using namespace std;
|
||||
|
||||
class priority_queue {
|
||||
private:
|
||||
string queue[MAX_SIZE];
|
||||
int priority_val[MAX_SIZE];
|
||||
int front;
|
||||
int rear;
|
||||
|
||||
public:
|
||||
priority_queue() {
|
||||
front = -1;
|
||||
rear = -1;
|
||||
}
|
||||
|
||||
bool is_empty() { return front == -1; }
|
||||
|
||||
bool is_full() { return rear == MAX_SIZE - 1; }
|
||||
|
||||
void enqueue(string data, int priority) {
|
||||
if (is_full()) {
|
||||
cout << "Queue is full." << endl;
|
||||
return;
|
||||
}
|
||||
if (is_empty()) {
|
||||
front = 0;
|
||||
rear = 0;
|
||||
queue[rear] = data;
|
||||
priority_val[rear] = priority;
|
||||
} else {
|
||||
int i;
|
||||
rear++;
|
||||
for (i = rear-1; i >= front; i--) {
|
||||
if (priority_val[i] < priority) {
|
||||
queue[i + 1] = queue[i];
|
||||
priority_val[i + 1] = priority_val[i];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
queue[i + 1] = data;
|
||||
priority_val[i + 1] = priority;
|
||||
}
|
||||
}
|
||||
|
||||
string dequeue() {
|
||||
if (is_empty()) {
|
||||
cout << "Queue is empty." << endl;
|
||||
return "";
|
||||
}
|
||||
string data = queue[front];
|
||||
if (front == rear) {
|
||||
front = -1;
|
||||
rear = -1;
|
||||
} else {
|
||||
front++;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
int get_priority() {
|
||||
if (is_empty()) {
|
||||
return -1;
|
||||
}
|
||||
return priority_val[front];
|
||||
}
|
||||
|
||||
void display() {
|
||||
if (is_empty()) {
|
||||
cout << "Queue is empty." << endl;
|
||||
return;
|
||||
}
|
||||
cout << "Queue is:" << endl;
|
||||
for (int i = front; i <= rear; i++) {
|
||||
cout << priority_val[i] << ":\t" << queue[i] << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
priority_queue queue;
|
||||
int ch = 0;
|
||||
int priority;
|
||||
string name;
|
||||
do {
|
||||
cout << "--- MAIN MENU ---" << endl;
|
||||
cout << "1 -> Add patient" << endl;
|
||||
cout << "2 -> Remove patient" << endl;
|
||||
cout << "3 -> Display queue" << endl;
|
||||
cout << "4 -> Exit" << endl;
|
||||
cout << "Choose an option (1-4):\t";
|
||||
cin >> ch;
|
||||
switch (ch) {
|
||||
case 1:
|
||||
cout << "Patient name:\t";
|
||||
cin >> name;
|
||||
cout << "Priority (0: General Checkup; 1: Non-serious; 2: Serious):\t";
|
||||
cin >> priority;
|
||||
queue.enqueue(name, priority);
|
||||
cout << "Patient added successfully." << endl;
|
||||
break;
|
||||
case 2:
|
||||
priority = queue.get_priority();
|
||||
name = queue.dequeue();
|
||||
cout << "Patient '" << name << "' with priority '" << priority
|
||||
<< "' removed." << endl;
|
||||
break;
|
||||
case 3:
|
||||
queue.display();
|
||||
break;
|
||||
case 4:
|
||||
cout << "\n\n// END OF CODE\n\n" << endl;
|
||||
break;
|
||||
default:
|
||||
cout << "Please choose a valid option (1-4)." << endl;
|
||||
break;
|
||||
}
|
||||
} while (ch != 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,139 +0,0 @@
|
||||
// THIS IS A LONG CODE
|
||||
/*Consider a scenario for Hospital to cater services to different kinds of
|
||||
patients as Serious (top priority), b) non-serious (medium priority), c) General
|
||||
Checkup (Least priority). Implement the priority queue to cater services to the
|
||||
patients.*/
|
||||
|
||||
#include <iostream>
|
||||
#define MAX_SIZE 10
|
||||
|
||||
using namespace std;
|
||||
|
||||
class priority_queue {
|
||||
private:
|
||||
string queue[MAX_SIZE];
|
||||
int priority_val[MAX_SIZE];
|
||||
|
||||
int front;
|
||||
int rear;
|
||||
|
||||
public:
|
||||
priority_queue() {
|
||||
front = -1;
|
||||
rear = -1;
|
||||
}
|
||||
|
||||
bool is_empty() { return front == -1; }
|
||||
|
||||
bool is_full() { return rear == MAX_SIZE - 1; }
|
||||
|
||||
void enqueue(string data, int priority) {
|
||||
if (is_full()) {
|
||||
cout << "Queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
if (is_empty()) {
|
||||
front = 0;
|
||||
rear = 0;
|
||||
queue[rear] = data;
|
||||
priority_val[rear] = priority;
|
||||
} else {
|
||||
int i;
|
||||
rear++;
|
||||
for (i = rear-1; i >= front; i--) {
|
||||
if (priority_val[i] < priority) {
|
||||
queue[i + 1] = queue[i];
|
||||
priority_val[i + 1] = priority_val[i];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
queue[i + 1] = data;
|
||||
priority_val[i + 1] = priority;
|
||||
}
|
||||
}
|
||||
|
||||
string dequeue() {
|
||||
if (is_empty()) {
|
||||
cout << "Queue is empty" << endl;
|
||||
return "";
|
||||
}
|
||||
string data = queue[front];
|
||||
if (front == rear) {
|
||||
front = -1;
|
||||
rear = -1;
|
||||
} else {
|
||||
front++;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
int get_priority() {
|
||||
if (is_empty()) {
|
||||
return -1;
|
||||
}
|
||||
return priority_val[front];
|
||||
}
|
||||
|
||||
void display() {
|
||||
if (is_empty()) {
|
||||
cout << "Queue is empty" << endl;
|
||||
return;
|
||||
}
|
||||
cout << "Queue is:" << endl;
|
||||
for (int i = front; i <= rear; i++) {
|
||||
cout << priority_val[i] << " : " << queue[i] << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
priority_queue queue;
|
||||
int ch = 0;
|
||||
int priority;
|
||||
string name;
|
||||
cout<<"Priorities are 0: General Checkup, 1: Non-Serious, 2: Serious"<<endl;
|
||||
do {
|
||||
cout << "------: MENU :------" << endl;
|
||||
cout << "1. Add Patient" << endl;
|
||||
cout << "2. Remove Patient" << endl;
|
||||
cout << "3. Display Queue" << endl;
|
||||
cout << "4. Exit" << endl;
|
||||
cout << "Enter your choice: ";
|
||||
cin >> ch;
|
||||
switch (ch) {
|
||||
case 1:
|
||||
cout << "Enter Patient Name: ";
|
||||
cin >> name;
|
||||
cout << "Enter Priority: ";
|
||||
cin >> priority;
|
||||
queue.enqueue(name, priority);
|
||||
cout << "Patient Added Successfully" << endl;
|
||||
break;
|
||||
case 2:
|
||||
priority = queue.get_priority();
|
||||
name = queue.dequeue();
|
||||
cout << "Patient '" << name << "' with priority '" << priority
|
||||
<< "' removed" << endl;
|
||||
break;
|
||||
case 3:
|
||||
queue.display();
|
||||
break;
|
||||
case 4:
|
||||
cout << "Thank You" << endl;
|
||||
break;
|
||||
default:
|
||||
cout << "Invalid Choice" << endl;
|
||||
break;
|
||||
}
|
||||
} while (ch != 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
1 ram 2
|
||||
1 shyam 0
|
||||
1 hari 1
|
||||
*/
|
@ -1,203 +0,0 @@
|
||||
// THIS IS A LONG CODE
|
||||
/*
|
||||
Department maintains a student information. The file contains roll number, name,
|
||||
division and address. Allow user to add, delete information of student. Display
|
||||
information of particular student. If record of student does not exist an
|
||||
appropriate message is displayed. If it is, then the system displays the student
|
||||
details. Use sequential file to main the data.
|
||||
*/
|
||||
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
typedef struct student {
|
||||
int rollNo;
|
||||
char name[50];
|
||||
char div;
|
||||
char address[100];
|
||||
} student;
|
||||
|
||||
class studentDatabase {
|
||||
string fileName = "student_data.dat";
|
||||
|
||||
public:
|
||||
studentDatabase() {
|
||||
fstream fileObj(fileName);
|
||||
if (fileObj.fail()) {
|
||||
fileObj.open(fileName, ios::out);
|
||||
cout << "New File Created" << endl;
|
||||
} else {
|
||||
cout << "Existing File Found" << endl;
|
||||
}
|
||||
fileObj.close();
|
||||
}
|
||||
|
||||
void addStudent();
|
||||
void searchStudent();
|
||||
void deleteStudent();
|
||||
void displayAll();
|
||||
};
|
||||
|
||||
void studentDatabase::searchStudent() {
|
||||
int roll;
|
||||
student s;
|
||||
bool status = false;
|
||||
|
||||
// take input of roll number to delete
|
||||
cout << "Enter roll number to delete:";
|
||||
cin >> roll;
|
||||
|
||||
// opening files to delete a record
|
||||
ifstream readFile;
|
||||
readFile.open(fileName, ios::in | ios::binary);
|
||||
|
||||
// looking for record
|
||||
while (readFile.read((char*)&s, sizeof(student))) {
|
||||
if (s.rollNo == roll) {
|
||||
status = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
readFile.close();
|
||||
|
||||
if (status) {
|
||||
cout << "Found record with details" << endl;
|
||||
cout << "Roll No:" << s.rollNo << endl;
|
||||
cout << "Name:" << s.name << endl;
|
||||
cout << "Division:" << s.div << endl;
|
||||
cout << "Address:" << s.address << endl;
|
||||
} else {
|
||||
cout << "No record found" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void studentDatabase::deleteStudent() {
|
||||
int roll;
|
||||
student s;
|
||||
bool status = false;
|
||||
|
||||
// take input of roll number to delete
|
||||
cout << "Enter roll number to delete:";
|
||||
cin >> roll;
|
||||
|
||||
// opening files to delete a record
|
||||
ifstream readFile;
|
||||
readFile.open(fileName, ios::in | ios::binary);
|
||||
ofstream writeFile;
|
||||
writeFile.open("~" + fileName, ios::out | ios::binary);
|
||||
writeFile.clear();
|
||||
|
||||
// looking for record
|
||||
while (readFile.read((char*)&s, sizeof(student))) {
|
||||
if (s.rollNo == roll) {
|
||||
status = true;
|
||||
} else {
|
||||
writeFile.write((char*)&s, sizeof(student)) << flush;
|
||||
}
|
||||
}
|
||||
readFile.close();
|
||||
writeFile.close();
|
||||
|
||||
// moving temp file back to original file
|
||||
if (status) {
|
||||
readFile.open("~" + fileName, ios::in | ios::binary);
|
||||
writeFile.open(fileName, ios::out | ios::binary);
|
||||
writeFile.clear();
|
||||
|
||||
writeFile << readFile.rdbuf();
|
||||
readFile.close();
|
||||
writeFile.close();
|
||||
|
||||
// remove("~"+fileName);
|
||||
cout << "Deleted record" << endl;
|
||||
} else {
|
||||
cout << "No record found" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void studentDatabase::addStudent() {
|
||||
student s;
|
||||
cout << "Enter Roll number of student:";
|
||||
cin >> s.rollNo;
|
||||
cout << "Enter Name of student:";
|
||||
cin.ignore();
|
||||
cin.getline(s.name, 50);
|
||||
cout << "Enter Division of student:";
|
||||
// cin.ignore();
|
||||
cin >> s.div;
|
||||
cout << "Enter Address of student:";
|
||||
cin.ignore();
|
||||
cin.getline(s.address, 100);
|
||||
// cin.ignore();
|
||||
ofstream file(fileName, ios::out | ios::binary | ios::app);
|
||||
// file.seekp(ios::end);
|
||||
file.write((char*)&s, sizeof(student)) << flush;
|
||||
if (file.fail()) {
|
||||
cout << "Failed to add record" << endl;
|
||||
} else {
|
||||
cout << "Student record added successfully" << endl;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void studentDatabase::displayAll() {
|
||||
ifstream file;
|
||||
student s;
|
||||
int count = 0;
|
||||
file.open(fileName, ios::in | ios::binary);
|
||||
while (file.read((char*)&s, sizeof(student))) {
|
||||
count++;
|
||||
cout << count << ") ";
|
||||
cout << s.rollNo << "|";
|
||||
cout << s.name << "|";
|
||||
cout << s.div << "|";
|
||||
cout << s.address << endl;
|
||||
}
|
||||
if (count == 0) {
|
||||
cout << "No records Found" << endl;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
int main() {
|
||||
int ch;
|
||||
studentDatabase db;
|
||||
|
||||
// loop
|
||||
do {
|
||||
cout << endl;
|
||||
cout << "MENU" << endl;
|
||||
cout << "1. Add Record" << endl;
|
||||
cout << "2. Delete Record" << endl;
|
||||
cout << "3. Search Record" << endl;
|
||||
cout << "4. Display All Records" << endl;
|
||||
cout << "0. Exit" << endl << endl;
|
||||
cout << "Enter Your Choice :";
|
||||
cin >> ch;
|
||||
switch (ch) {
|
||||
case 0:
|
||||
cout << "Thank You" << endl;
|
||||
break;
|
||||
case 1:
|
||||
db.addStudent();
|
||||
break;
|
||||
case 2:
|
||||
db.deleteStudent();
|
||||
break;
|
||||
case 3:
|
||||
db.searchStudent();
|
||||
break;
|
||||
case 4:
|
||||
cout << "Records in File are" << endl;
|
||||
db.displayAll();
|
||||
break;
|
||||
|
||||
default:
|
||||
cout << "Enter a valid Choice" << endl;
|
||||
break;
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,309 +0,0 @@
|
||||
// THIS IS A LONG CODE
|
||||
/*
|
||||
Company maintains employee information as employee ID, name, designation and
|
||||
salary. Allow user to add, delete information of employee. Display information
|
||||
of particular employee. If employee does not exist an appropriate message is
|
||||
displayed. If it is, then the system displays the employee details. Use index
|
||||
sequential file to maintain the data.
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
typedef struct employee {
|
||||
int empId;
|
||||
char name[50];
|
||||
char designation[50];
|
||||
int salary;
|
||||
} employee;
|
||||
|
||||
typedef struct index_pair {
|
||||
int key;
|
||||
int position;
|
||||
} index_pair;
|
||||
|
||||
class employeeDatabase {
|
||||
string data_file_name = "ind_employee_data.dat";
|
||||
string index_file_name = "ind_employee_index.dat";
|
||||
|
||||
public:
|
||||
employeeDatabase();
|
||||
void addEmployee(int eid, char name[50], char dest[50], int sal);
|
||||
void searchEmployee(int);
|
||||
void deleteEmployee(int);
|
||||
bool isPresent(int);
|
||||
void display_all();
|
||||
employee readEmp(int pos);
|
||||
};
|
||||
|
||||
employee employeeDatabase::readEmp(int pos) {
|
||||
fstream data_file(data_file_name, ios::binary | ios::in | ios::ate);
|
||||
employee emp;
|
||||
if (pos >= data_file.tellg() || pos == -1) {
|
||||
emp.empId = -1;
|
||||
return emp;
|
||||
}
|
||||
|
||||
data_file.seekg(pos, ios::beg);
|
||||
data_file.read((char *)&emp, sizeof(emp));
|
||||
|
||||
data_file.close();
|
||||
return emp;
|
||||
}
|
||||
|
||||
employeeDatabase::employeeDatabase() {
|
||||
fstream data_file(data_file_name);
|
||||
fstream index_file(index_file_name);
|
||||
if (data_file.fail() | index_file.fail()) {
|
||||
cout << "New File Created" << endl;
|
||||
data_file.open(data_file_name, ios::binary | ios::out | ios::app);
|
||||
index_file.open(index_file_name, ios::binary | ios::out | ios::app);
|
||||
} else {
|
||||
cout << "Existing File Found" << endl;
|
||||
}
|
||||
data_file.close();
|
||||
index_file.close();
|
||||
}
|
||||
|
||||
void employeeDatabase::addEmployee(int eid, char name[50], char dest[50],
|
||||
int sal) {
|
||||
fstream data_file(data_file_name, ios::binary | ios::out | ios::app);
|
||||
fstream index_file(index_file_name, ios::binary | ios::in);
|
||||
index_pair current, temp;
|
||||
char *all_data;
|
||||
employee emp;
|
||||
int pos, ipos;
|
||||
bool update = false;
|
||||
|
||||
while (index_file.read((char *)¤t, sizeof(index_pair))) {
|
||||
// cout << current.key << " - " << current.position << endl;
|
||||
if (current.key == eid) {
|
||||
if (current.position == -1) {
|
||||
ipos = (int)index_file.tellg() - sizeof(current);
|
||||
update = true;
|
||||
break;
|
||||
} else {
|
||||
cout << "Employee ID Already Present, Can Not Add record"
|
||||
<< endl;
|
||||
goto exit_addEmployee;
|
||||
}
|
||||
}
|
||||
}
|
||||
index_file.close();
|
||||
|
||||
emp.empId = eid;
|
||||
strcpy(emp.name, name);
|
||||
strcpy(emp.designation, dest);
|
||||
emp.salary = sal;
|
||||
|
||||
data_file.seekp(0, ios::end);
|
||||
pos = data_file.tellp();
|
||||
data_file.write((char *)&emp, sizeof(emp));
|
||||
current.key = eid;
|
||||
current.position = pos;
|
||||
// cout << pos << endl;
|
||||
if (update) {
|
||||
index_file.open(index_file_name, ios::binary | ios::out);
|
||||
index_file.seekp(ipos, ios::beg);
|
||||
index_file.write((char *)¤t, sizeof(current));
|
||||
} else {
|
||||
bool written = false;
|
||||
// inserting record in sorted order
|
||||
vector<index_pair> index_pairs;
|
||||
index_file.open(index_file_name, ios::binary | ios::in);
|
||||
while (index_file.read((char *)&temp, sizeof(index_pair))) {
|
||||
if (!written and temp.key > eid) {
|
||||
written = true;
|
||||
index_pairs.push_back(current);
|
||||
}
|
||||
index_pairs.push_back(temp);
|
||||
}
|
||||
if (!written) {
|
||||
index_pairs.push_back(current);
|
||||
}
|
||||
|
||||
index_file.clear();
|
||||
index_file.close();
|
||||
|
||||
// write records
|
||||
index_file.open(index_file_name, ios::binary | ios::out);
|
||||
|
||||
for (int i = 0; i < index_pairs.size(); i++) {
|
||||
current = index_pairs[i];
|
||||
if (current.position != -1) {
|
||||
index_file.write((char *)¤t, sizeof(index_pair));
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << "Employee added successfully" << endl;
|
||||
// close and exit
|
||||
exit_addEmployee:
|
||||
data_file.close();
|
||||
index_file.close();
|
||||
}
|
||||
|
||||
void display_emp(employee emp) {
|
||||
cout << "Emp ID:" << emp.empId << endl;
|
||||
cout << "Emp Name:" << emp.name << endl;
|
||||
cout << "Emp Designation:" << emp.designation << endl;
|
||||
cout << "Emp Salary:" << emp.salary << endl;
|
||||
}
|
||||
|
||||
void employeeDatabase::searchEmployee(int eid) {
|
||||
fstream index_file(index_file_name, ios::binary | ios::in);
|
||||
index_pair current;
|
||||
int pos = -1;
|
||||
|
||||
while (index_file.read((char *)¤t, sizeof(index_pair))) {
|
||||
if (current.key == eid) {
|
||||
pos = current.position;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
employee emp = readEmp(pos);
|
||||
if (emp.empId == -1) {
|
||||
cout << "Employee Not Found" << endl;
|
||||
} else {
|
||||
cout << "Employee Details are :" << endl;
|
||||
display_emp(emp);
|
||||
}
|
||||
|
||||
index_file.close();
|
||||
}
|
||||
|
||||
bool employeeDatabase::isPresent(int eid) {
|
||||
fstream index_file(index_file_name, ios::binary | ios::in);
|
||||
index_pair current;
|
||||
|
||||
while (index_file.read((char *)¤t, sizeof(index_pair))) {
|
||||
if (current.key == eid) {
|
||||
if (current.position == -1) {
|
||||
index_file.close();
|
||||
return false;
|
||||
} else {
|
||||
index_file.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
index_file.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
void employeeDatabase::deleteEmployee(int eid) {
|
||||
fstream index_file(index_file_name, ios::binary | ios::in);
|
||||
index_pair current;
|
||||
bool deleted = false;
|
||||
vector<index_pair> pairs;
|
||||
|
||||
while (index_file.read((char *)¤t, sizeof(index_pair))) {
|
||||
if (current.key == eid) {
|
||||
deleted = true;
|
||||
} else {
|
||||
pairs.push_back(current);
|
||||
}
|
||||
}
|
||||
index_file.close();
|
||||
if (deleted) {
|
||||
index_file.open(index_file_name, ios::binary | ios::out);
|
||||
index_file.clear();
|
||||
index_file.seekp(0, ios::beg);
|
||||
|
||||
for (int i = 0; i < pairs.size(); i++) {
|
||||
current = pairs[i];
|
||||
index_file.write((char *)¤t, sizeof(index_pair));
|
||||
}
|
||||
index_file.close();
|
||||
cout << "Employee Deleted successfully" << endl;
|
||||
} else {
|
||||
cout << "Employee Not Found To Delete" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void employeeDatabase::display_all() {
|
||||
fstream index_file(index_file_name, ios::binary | ios::in);
|
||||
fstream data_file(data_file_name, ios::binary | ios::in);
|
||||
index_pair current;
|
||||
employee emp;
|
||||
|
||||
while (index_file.read((char *)¤t, sizeof(index_pair))) {
|
||||
if (current.position != -1) {
|
||||
data_file.seekg(current.position, ios::beg);
|
||||
data_file.read((char *)&emp, sizeof(emp));
|
||||
cout << emp.empId << " | " << emp.name << " | " << emp.designation
|
||||
<< " | " << emp.salary << endl;
|
||||
}
|
||||
}
|
||||
|
||||
index_file.close();
|
||||
data_file.close();
|
||||
}
|
||||
|
||||
int main() {
|
||||
employeeDatabase db;
|
||||
int eid, sal;
|
||||
char name[50], dest[50];
|
||||
int ch;
|
||||
do {
|
||||
cout << endl << "--: MENU :--" << endl;
|
||||
cout << "1. Add Employee" << endl;
|
||||
cout << "2. Search Employee" << endl;
|
||||
cout << "3. Delete Employee" << endl;
|
||||
cout << "4. Display All" << endl;
|
||||
cout << "5. Exit" << endl;
|
||||
cout << "Enter your Choice:";
|
||||
cin >> ch;
|
||||
switch (ch) {
|
||||
case 1:
|
||||
reenter_eid:
|
||||
cout << "Enter Employee ID:";
|
||||
cin >> eid;
|
||||
if (db.isPresent(eid)) {
|
||||
cout << "Employee ID already Present Please Re-enter ID."
|
||||
<< endl;
|
||||
goto reenter_eid;
|
||||
}
|
||||
cout << "Enter Employee Name:";
|
||||
cin >> name;
|
||||
cout << "Enter Employee Destination:";
|
||||
cin >> dest;
|
||||
cout << "Enter Employee Salary:";
|
||||
cin >> sal;
|
||||
|
||||
db.addEmployee(eid, name, dest, sal);
|
||||
break;
|
||||
case 2:
|
||||
cout << "Enter Employee ID:";
|
||||
cin >> eid;
|
||||
db.searchEmployee(eid);
|
||||
break;
|
||||
case 3:
|
||||
cout << "Enter Employee ID:";
|
||||
cin >> eid;
|
||||
db.deleteEmployee(eid);
|
||||
break;
|
||||
case 4:
|
||||
db.display_all();
|
||||
break;
|
||||
case 5:
|
||||
cout << "Thank You" << endl;
|
||||
break;
|
||||
default:
|
||||
cout << "Invalid Choice" << endl;
|
||||
break;
|
||||
}
|
||||
} while (ch != 5);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
1 1 1 1 1
|
||||
1 2 2 2 2
|
||||
1 3 3 3 3
|
||||
1 5 5 5 5
|
||||
*/
|
11
README.md
11
README.md
@ -1,10 +1,3 @@
|
||||
# longCodes branch
|
||||
|
||||
This branch contains long codes with minimal description.
|
||||
|
||||
## Choose your poison:
|
||||
|
||||
- [main branch (all the codes are tested and relatively small in size)](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/main/Codes)
|
||||
- [longCodes (long codes with minimal description)](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/longCodes) **CURRENT BRANCH**
|
||||
- [testing (untested codes)](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/testing/)
|
||||
# testing branch
|
||||
|
||||
⚠️⚠️⚠️ This branch contains untested codes. ⚠️⚠️⚠️
|
||||
|
@ -1,413 +0,0 @@
|
||||
commit 69fdd6ddf4c3c2b658d18053efa30db57748f883
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Wed May 8 21:31:01 2024 +0530
|
||||
|
||||
moved obst to long codes
|
||||
|
||||
commit 1cfd25a5c66ef9b85ca1c44e8f1a9cdbbae19792
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Wed May 8 08:40:18 2024 +0530
|
||||
|
||||
moved c13 from main long codes
|
||||
|
||||
commit af15edf433ed1853735e4c5741c814f3f0703381
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue May 7 21:41:35 2024 +0530
|
||||
|
||||
moved e20 v2 from main branch to long codes branch
|
||||
|
||||
commit 5dee7350337fb6ca773e4f19a1451f0fbf17a689
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 20:02:10 2024 +0530
|
||||
|
||||
fixed link and added desc
|
||||
|
||||
commit 1efd00b93c4834d7c51cb009190833e05e39a2d2
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 18:18:27 2024 +0530
|
||||
|
||||
added all long codes
|
||||
|
||||
commit 39c936ec8901218d1964e9e55beb53c3444b0e8d
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 18:14:20 2024 +0530
|
||||
|
||||
fixed readme
|
||||
|
||||
commit a5138d106cb3d13168a0e0c27c4b3b7cdd45c12f
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 18:13:10 2024 +0530
|
||||
|
||||
updated readme
|
||||
|
||||
commit efffdbde62972f1a214ebc2d3967153e9240a5f8
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 18:10:30 2024 +0530
|
||||
|
||||
created a new branch. this one contains long codes. updated readme. deleted unnecessary content
|
||||
|
||||
commit a8a5d55b70b240e63049dd926f0e912b0e4c1e42
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 18:02:23 2024 +0530
|
||||
|
||||
added Practical-D19
|
||||
|
||||
commit f646fc8b17eecb0980c81fe21aef5a08d352f563
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 17:50:29 2024 +0530
|
||||
|
||||
updated readme
|
||||
|
||||
commit 8ef24cd0f7244480d12aa5c39f65945d6f593d3d
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 17:44:12 2024 +0530
|
||||
|
||||
fixed and tested Practical-D18 code
|
||||
|
||||
commit 71dde49eebc16c730a2852ae9e3808c5feff9ba1
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 17:35:46 2024 +0530
|
||||
|
||||
added, fixed and tested c15 code
|
||||
|
||||
commit 9f03a5fb418a576f1836ce46d187133eabc3d124
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 17:28:47 2024 +0530
|
||||
|
||||
fixed folder name for unit 2 and updated readme
|
||||
|
||||
commit 8736745a1e83206a34b9a605bbb851fcb37fc45d
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 17:19:13 2024 +0530
|
||||
|
||||
added and tested Practical-B11 code
|
||||
|
||||
commit f6751e45c6f2b62ec7150d5a9795ac6251f9626e
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 28 17:05:50 2024 +0530
|
||||
|
||||
added and tested Practical-B7 code
|
||||
|
||||
commit 9166d86bc55e4a7d1deb088e82bd29ecbd60bd0b
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue Apr 23 13:01:30 2024 +0530
|
||||
|
||||
updated readme
|
||||
|
||||
commit ea0526179aedc8c5dc105893a0e170996091c738
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue Apr 23 12:53:18 2024 +0530
|
||||
|
||||
added write-up for D19, E20, F23, F24
|
||||
|
||||
commit d6c2e441202d79235ba8a8236a2e67d17b3833b3
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue Apr 23 10:18:36 2024 +0530
|
||||
|
||||
added reference for testing branch in README files
|
||||
|
||||
commit dc21de59a7e7a89c029c4d830647c4244ff9687e
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue Apr 23 10:13:00 2024 +0530
|
||||
|
||||
added tested Practical-B5 code and updated readme
|
||||
|
||||
commit 815e5d19282d24918ddd06b935f7a8efae32136f
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue Apr 23 09:43:35 2024 +0530
|
||||
|
||||
added tested code for Practical-A4 and updated readme
|
||||
|
||||
commit d34dc48cf429baf66d0c16ceb1e4da9b9861bed5
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue Apr 23 09:34:45 2024 +0530
|
||||
|
||||
added tested code for Practical-A1 and updated readme
|
||||
|
||||
commit 5b4644f302ccfb78e5d19fcf3598cfb9b2d29007
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue Apr 23 09:31:42 2024 +0530
|
||||
|
||||
moved untested codes to testing branch (https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/testing/Codes/)
|
||||
|
||||
commit df26d18472c14ce3ee354181c8df782e7bb43d50
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Apr 21 01:14:06 2024 +0530
|
||||
|
||||
kalas says this should be b11, haven't checked it
|
||||
|
||||
commit 26cdf0645d22d97d3de68188ad4519e9298b9c1f
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Mon Apr 15 22:08:29 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit 37eb1bb2a249de5747c6a8534706da6b3623cdf8
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Mon Apr 15 22:05:50 2024 +0530
|
||||
|
||||
d18 writeup added
|
||||
|
||||
commit 782308552c9c06b6fcff6482ef5a4ad428de9bb7
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Mon Apr 15 17:25:14 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit c83a54c7b46edde4e9d508aea30324ef36f156cc
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Mon Apr 15 17:23:17 2024 +0530
|
||||
|
||||
d18 code added
|
||||
|
||||
commit 982d35f3f92a47f716d203a7d3a4605c22c093ce
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Fri Apr 12 10:23:11 2024 +0530
|
||||
|
||||
updated readme
|
||||
|
||||
commit 05f35427c6f0a73d4903cd9818a42cbfe01fcfe8
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Fri Apr 12 10:17:46 2024 +0530
|
||||
|
||||
added ppt for u2 and added unit name in folders
|
||||
|
||||
commit e10f27dbcc19b8e540b915513bcfa1befed2a8ed
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Sun Apr 7 10:28:54 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit 215de628d0bc8f67648c56a2f67d689fe5c1ced1
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Sun Apr 7 10:25:06 2024 +0530
|
||||
|
||||
c13 writeup added
|
||||
|
||||
commit 8b59de4e805eb9f0e1b22b22fbd639030089bfb4
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Wed Apr 3 19:38:57 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit 17fac60a5ba9564e0a0b8d3f1790015cee3017dc
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Wed Apr 3 19:36:33 2024 +0530
|
||||
|
||||
b11 writup added
|
||||
|
||||
commit b29764306e61e4f3492a52aa7649c1cffa9daa6d
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Thu Mar 14 22:17:58 2024 +0530
|
||||
|
||||
added question bank
|
||||
|
||||
commit c0b2f0f4295063bcbccc254e4d51ab6c7177f4ee
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Thu Mar 14 21:58:14 2024 +0530
|
||||
|
||||
changed names for ppts to avoid confusion
|
||||
|
||||
commit 84fca23ba6e0368a6a9aa0de8967d0c1d69fba78
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Thu Mar 14 18:36:36 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit d48b2b2b0aa23c5d1b04fb16b1aa53653780bd64
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Thu Mar 14 18:32:44 2024 +0530
|
||||
|
||||
Notes By Deore Ma'am Added
|
||||
|
||||
commit af5653289a1b2f37e7604c41875a3c3f0c673d92
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Mon Mar 11 08:27:17 2024 +0530
|
||||
|
||||
added erp link
|
||||
|
||||
commit 4f0ed9d48bb85662967200b1d8be5e7656a3e97c
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Mar 10 19:29:56 2024 +0530
|
||||
|
||||
added class notes for unit 1 and 2 (handwritten by Kalaskar)
|
||||
|
||||
commit 13c448f06e1b9ed7b444e1f15f6c9d4c5c79829e
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Wed Mar 6 13:05:53 2024 +0530
|
||||
|
||||
updated readme
|
||||
|
||||
commit febbc775a76dc9b05b806bb15a761ad29122969a
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Wed Mar 6 13:04:43 2024 +0530
|
||||
|
||||
added write up for C15
|
||||
|
||||
commit acf72dcbea351a48f2cea5ddabe727a6d2df6b5f
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Tue Feb 27 18:46:27 2024 +0530
|
||||
|
||||
added disclaimer for erp assignments
|
||||
|
||||
commit feb014875a82bdad5cee4a3470625a77e0703b02
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Mon Feb 26 22:04:23 2024 +0530
|
||||
|
||||
added questions and updated readme
|
||||
|
||||
commit 067ba94352e66d4f5bb43dea9f5653be984c310d
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Mon Feb 26 21:49:55 2024 +0530
|
||||
|
||||
moved practical write-up to write-ups folder, added erp assignment 1 and updated readme
|
||||
|
||||
commit be62430a8b0bf5fd892f28ec5e893d8011d05206
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sat Feb 24 01:15:24 2024 +0530
|
||||
|
||||
final changes to A4 code
|
||||
|
||||
commit 736f047a3075115833a199cf3e4b249b2e17630b
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sat Feb 24 01:11:31 2024 +0530
|
||||
|
||||
updated readme
|
||||
|
||||
commit 21b3b3b1286805b494f8689bcf87ad1532e6f2bd
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sat Feb 24 01:07:52 2024 +0530
|
||||
|
||||
added A4
|
||||
|
||||
commit 537edadc186bf33d1fa4b6c1587701228c80751a
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sat Feb 24 01:05:11 2024 +0530
|
||||
|
||||
fixed A1 code
|
||||
|
||||
commit 3c720a40748f57d3bc92ed37421cf43819af1e3d
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Thu Feb 22 23:35:56 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit 962a5eca8c9ec423f1361f2a5c2d0d1a9e6db07b
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Thu Feb 22 23:34:02 2024 +0530
|
||||
|
||||
B7 added finally
|
||||
|
||||
commit c33f887e27ba2ca3885704f1f17d294f72882dd6
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sat Feb 17 18:39:00 2024 +0530
|
||||
|
||||
LH10 added iterator
|
||||
|
||||
commit 27069b283c822fd013897989920ffeeb5236161f
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Fri Feb 16 17:57:07 2024 +0530
|
||||
|
||||
fixed the file extension for Practical-A4 (in README too) and updated the code
|
||||
|
||||
commit 2312eb22eb4a2a0c923a0bb5ac907e8640427f1a
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Fri Feb 16 00:46:05 2024 +0530
|
||||
|
||||
a1 readme updated
|
||||
|
||||
commit 898a9217638823824b95abaf66024c3ec4d60eda
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Fri Feb 16 00:43:13 2024 +0530
|
||||
|
||||
a1 writup added
|
||||
|
||||
commit 1bec1716dffe77cf2a35699f8d149630acd6a1d1
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Thu Feb 15 09:47:56 2024 +0530
|
||||
|
||||
updated A4 to python
|
||||
|
||||
commit 4a3c14afefcb5f61d3ee407e2d4270d9adaac0be
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Wed Feb 14 18:59:22 2024 +0530
|
||||
|
||||
updated readme
|
||||
|
||||
commit be57577b33c85d8ecd3edf8516ab7ca7aa57483e
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Wed Feb 14 18:56:07 2024 +0530
|
||||
|
||||
Added maximum codes. All of them are experimental
|
||||
|
||||
commit a8611df79655dd43d1b7a5eb00bd01918a51f360
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Wed Feb 14 17:48:31 2024 +0530
|
||||
|
||||
Moved assignments to assignments folder
|
||||
|
||||
commit 09814817a79242db374aa270afa532ef8b49dd6d
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Tue Feb 13 20:21:01 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit 8ab9fd47a62ecccdf693ea871892af7cdbdc2ace
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Tue Feb 13 20:19:30 2024 +0530
|
||||
|
||||
trees b5 added
|
||||
|
||||
commit 493380ae3442c61a1b57c7f4a8c03c13810cca60
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Tue Feb 13 19:10:42 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit c0aac9b595f57f0bfda6c5337d62190bc3f74c42
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Tue Feb 13 19:04:50 2024 +0530
|
||||
|
||||
assgn2 sets added
|
||||
|
||||
commit ba4aa9148a5a3683a69ed5330e58ae13aff40196
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Mon Feb 12 23:11:45 2024 +0530
|
||||
|
||||
Added links for lab manuals and notes
|
||||
|
||||
commit 62ce9f7b39c86383c79c2f50b6d5f38131376c4e
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Mon Feb 12 23:03:59 2024 +0530
|
||||
|
||||
Added lab manuals and unit 2 notes (trees)
|
||||
|
||||
commit c5ae5966f68fd92efd6147171c85b7359432abd6
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Mon Feb 5 00:47:04 2024 +0530
|
||||
|
||||
Updated README
|
||||
|
||||
commit a41b889c86414780702610bb2cdd71e1f51f93d0
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Mon Feb 5 00:45:49 2024 +0530
|
||||
|
||||
Added PYQs
|
||||
|
||||
commit b65371317d5ee6fdcb2a69354c82e6007fcef7db
|
||||
Author: Kshitij <REDACTED_EMAIL>
|
||||
Date: Sun Feb 4 20:36:34 2024 +0530
|
||||
|
||||
Added DISCLAIMER, LICENSE file, updated README, moved unit 1 notes to Notes folder
|
||||
|
||||
commit fb6be4c703a4f44282bbb9ac4eea3b125ec7eff2
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Mon Jan 29 19:14:47 2024 +0530
|
||||
|
||||
readme updated
|
||||
|
||||
commit 879adeaef5f1aa11564b230c7ddc7a82b0ced9ab
|
||||
Author: TanmayMachkar <REDACTED_EMAIL>
|
||||
Date: Mon Jan 29 19:13:50 2024 +0530
|
||||
|
||||
u1 added
|
Loading…
Reference in New Issue
Block a user