FDS Practicles
FDS Practicles
def display(self):
if not self.contacts:
print("Phonebook is empty.")
else:
for name, number in self.contacts:
print(f"{name}: {number}")
if __name__ == "__main__":
phonebook = Phonebook()
phonebook.insert("Alice", "123-456-7890")
phonebook.insert("Bob", "234-567-8901")
phonebook.insert("Charlie", "345-678-9012")
print("Phonebook:")
phonebook.display()
search_name = "Bob"
number = phonebook.search(search_name)
if number:
print(f"{search_name}'s number is {number}.")
else:
print(f"{search_name} not found in the phonebook.")
phonebook.insert("David", "456-789-0123")
phonebook.insert("Alice", "123-456-7890")
print("\nUpdated Phonebook:")
phonebook.display()
search_name = "Charlie"
pos = phonebook._binary_search_recursive(search_name, 0, len(phonebook.contacts) - 1)
if pos != -1:
print(f"Recursive search found {search_name} with number {phonebook.contacts[pos][1]}.")
else:
print(f"Recursive search did not find {search_name}.")
```OUTPUT:-
Phonebook:
Alice: 123-456-7890
Bob: 234-567-8901
Charlie: 345-678-9012
Bob's number is 234-567-8901.
Alice is already in the phonebook.
Updated Phonebook:
Alice: 123-456-7890
Bob: 234-567-8901
Charlie: 345-678-9012
David: 456-789-0123
Recursive search found Charlie with number 345-678-9012.
```
#Write a program to store second year percentage of students in array.
``` PRACTICLE NO 6:- Write a function for sorting array of floating
numbers in ascending order using
a)Insertion Sort
b)Shell Sort and display top five scores Function to perform Insertion Sort
```
def display_menu(percentages):
flag=1;
while flag==1:
print("\n---------------MENU---------------")
print("1. Insertion Sort of the marks")
print("2. Shell Sort of the marks")
print("3. Exit")
ch=int(input("\n\nEnter your choice (from 1 to 3) : "))
if ch==1:
sorted_by_insertion = insertion_sort(percentages.copy())
print("Sorted by Insertion Sort:", sorted_by_insertion)
a=input("\nDo you want to display top marks from the list (yes/no) : ")
if a=='yes':
display_top_five(sorted_by_insertion)
else:
print("\nThanks for using this program!")
flag=0
elif ch==2:
sorted_by_shell = shell_sort(percentages.copy())
print("Sorted by Shell Sort:", sorted_by_shell)
a = input("\nDo you want to display top five marks from the list (yes/no) : ")
if a == 'yes':
display_top_five(sorted_by_shell)
else:
print("\nThanks for using this program!")
flag = 0
elif ch==3:
print("\nThanks for using this program!!")
flag=0
else:
print("\nEnter a valid choice!!")
print("\nThanks for using this program!!")
flag=0
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
def shell_sort(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j=i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
return arr
def input_percentages():
percentages = []
n = int(input("Enter the number of students: "))
for i in range(n):
percentage = float(input(f"Enter percentage of student {i + 1}: "))
percentages.append(percentage)
return percentages
def display_top_five(arr):
print("Top five scores are:")
for score in sorted(arr, reverse=True)[:5]:
print(score)
def main():
percentages = input_percentages()
print("Original percentages:", percentages)
display_menu(percentages)
if __name__ == "__main__":
main()
```OUTPUT:-
Enter the number of students: 5
Enter percentage of student 1: 85
Enter percentage of student 2: 74
Enter percentage of student 3: 91
Enter percentage of student 4: 99
Enter percentage of student 5: 76
Original percentages: [85.0, 74.0, 91.0, 99.0, 76.0]
---------------MENU---------------
1. Insertion Sort of the marks
2. Shell Sort of the marks
3. Exit
Do you want to display top marks from the list (yes/no) : yes
Top five scores are:
99.0
91.0
85.0
76.0
74.0
---------------MENU---------------
1. Insertion Sort of the marks
2. Shell Sort of the marks
3. Exit
Do you want to display top five marks from the list (yes/no) : yes
Top five scores are:
99.0
91.0
85.0
76.0
74.0
---------------MENU---------------
1. Insertion Sort of the marks
2. Shell Sort of the marks
3. Exit
```
//Experiment 16: Department of Computer Engineering has student's
club named 'Pinnacle Club'.
//Students of Second, third and final year of department can be granted
membership on request.
//Similarly one may cancel the membership of club. First node is
reserved for president of club and last
//node is reserved for secretary of club. Write C++ program to maintain
club member's information
//using singly linked list. Store student PRN and Name. Write functions to
//a) Add and delete the members as well as president or even secretary.
//b) Compute total number of members of club
//c) Display members
//d) Two linked lists exists for two divisions. Concatenate two lists.
#include <iostream>
#include <string>
using namespace std;
struct Node {
string PRN;
string name;
Node* next;
};
void addMember(Node*& head, const string& PRN, const string& name) {
Node* newNode = new Node{PRN, name, nullptr};
if (!head) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}
void deleteMember(Node*& head, const string& PRN) {
if (!head) {
cout << "List is empty!" << endl;
return;
}
if (head->PRN == PRN) {
Node* temp = head;
head = head->next;
delete temp;
cout << "Member with PRN " << PRN << " deleted." << endl;
return;
}
Node* current = head;
while (current->next && current->next->PRN != PRN) {
current = current->next;
}
if (current->next) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
cout << "Member with PRN " << PRN << " deleted." << endl;
} else {
cout << "Member with PRN " << PRN << " not found." << endl;
}
}
void displayMembers(Node* head) {
if (!head) {
cout << "No members to display!" << endl;
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << "PRN: " << temp->PRN << ", Name: " << temp->name << endl;
temp = temp->next;
}
}
int totalMembers(Node* head) {
int count = 0;
Node* temp = head;
while (temp != nullptr) {
count++;
temp = temp->next;
}
return count;
}
Node* concatenateLists(Node* head1, Node* head2) {
if (!head1) return head2;
if (!head2) return head1;
Node* temp = head1;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = head2;
return head1;
}
int main() {
Node* division1 = nullptr;
Node* division2 = nullptr;
Adding some members to division 1
addMember(division1, "PRN001", "Alice");
addMember(division1, "PRN002", "Bob");
addMember(division1, "PRN003", "Charlie");
addMember(division2, "PRN004", "David");
addMember(division2, "PRN005", "Eve");
cout << "Members of Division 1:" << endl;
displayMembers(division1);
cout << "\nMembers of Division 2:" << endl;
displayMembers(division2);
Node* concatenatedList = concatenateLists(division1, division2);
cout << "\nMembers after concatenating Division 1 and 2:" << endl;
displayMembers(concatenatedList);
cout << "\nTotal number of members: " << totalMembers(concatenatedList) << endl;
deleteMember(concatenatedList, "PRN003");
cout << "\nMembers after deleting PRN003:" << endl;
displayMembers(concatenatedList);
return 0;
}
/*OUTPUT:-
Members of Division 1:
PRN: PRN001, Name: Alice
PRN: PRN002, Name: Bob
PRN: PRN003, Name: Charlie
Members of Division 2:
PRN: PRN004, Name: David
PRN: PRN005, Name: Eve
*/