C++ code: using a Binary Search Tree structure instead of a doubly linked list

profileyxpan1996
Source.cpp

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<fstream> using namespace std; // Struct for node, containing data and links struct node { string fname; string lname; string address; string ph; string email; string twitter; struct node *next; struct node *prev; }*start; //The double linked list class containing functions on the linked list class double_llist { public: //Method declarations void create_list(); void add(); void deletecontact(); void search(); void display(); void writeback(); double_llist() { // Setting head node to null in the constructor start = NULL; } }; int main() { int choice, element, position; double_llist dl; // A while loop to iterate the menu everytime while (1) { // The menu for the program cout << endl << "----------------------------" << endl; cout << endl << "Operations on Doubly Rolodex" << endl; cout << endl << "----------------------------" << endl; cout << "1.Please Create the List first (Read from file)" << endl; cout << "2.Add a Contact for the person" << endl; cout << "3.Delete" << endl; cout << "4.Display" << endl; cout << "5.Search" << endl; cout << "6.Quit" << endl; cout << "Enter your choice : "; cin >> choice; // A switch case statement to handle choices of user switch (choice) { case 1: dl.create_list(); cout << "\nSuccessfully read from file.\n" << endl; break; case 2: dl.add(); cout << endl; break; case 3: if (start == NULL) { cout << "List empty,nothing to delete" << endl; break; } dl.deletecontact(); cout << endl; break; case 4: dl.display(); cout << endl; break; case 5: dl.search(); cout << endl; break; case 6: dl.writeback(); exit(1); default: cout << "Wrong choice" << endl; } } system("pause"); } // Fucntion to create the list the first time void double_llist::create_list() { ifstream in; in.open("data.txt"); while (!in.eof()) { struct node *s, *temp; temp = new(struct node); in >> temp->fname; in >> temp->lname; in >> temp->address; in >> temp->ph; in >> temp->email; in >> temp->twitter; temp->next = NULL; if (start == NULL) { temp->prev = NULL; start = temp; } else { s = start; while (s->next != NULL) s = s->next; s->next = temp; temp->prev = s; } } in.close(); } // Function to add a contact element to the list void double_llist::add() { if (start == NULL) { cout << "First Create the list." << endl; return; } struct node *temp; temp = new(struct node); temp->prev = NULL; cout << "\n\nEnter first name : "; cin >> temp->fname; cout << "Enter last name : "; cin >> temp->lname; cout << "Enter address : "; cin >> temp->address; cout << "Enter contact number : "; cin >> temp->ph; cout << "Enter email : "; cin >> temp->email; cout << "Enter twitter handle : "; cin >> temp->twitter; temp->next = start; start->prev = temp; start = temp; cout << "\n\nContact Inserted" << endl; } // Function to delete a contact from the list void double_llist::deletecontact() { struct node *tmp, *q; /*first element deletion*/ string email; cout << "\n\n1. Enter a unique email address to delete the user : "; cin >> email; if (start->email == email) { tmp = start; start = start->next; start->prev = NULL; cout << "Contact Deleted" << endl; free(tmp); return; } q = start; while (q->next->next != NULL) { if (q->next->email == email) { tmp = q->next; q->next = tmp->next; tmp->next->prev = q; cout << "Contact Deleted" << endl; free(tmp); return; } q = q->next; } /*last element deleted*/ if (q->next->email == email) { tmp = q->next; free(tmp); q->next = NULL; cout << "Contact Deleted" << endl; return; } cout << "\nContact with Email: " << email << " not found!" << endl; } // To display the contents of the linked list void double_llist::display() { struct node *q; if (start == NULL) { cout << "List empty,nothing to display" << endl; return; } q = start; cout << "The Doubly Link List is :" << endl; while (q != NULL) { cout << " " << q->fname << " " << q->lname << " " << q->address << " " << q->ph << " " << q->email << " " << q->twitter << " \n"; q = q->next; } cout << "NULL" << endl; } // Upon closing the application, this function writes back to file void double_llist::writeback() { ofstream out; out.open("data.txt"); struct node *q; if (start == NULL) { cout << "List empty,nothing to write back" << endl; return; } q = start; while (q != NULL) { out << q->fname << " " << q->lname << " " << q->address << " " << q->ph << " " << q->email << " " << q->twitter << "\n"; q = q->next; } cout << "\nDone writing to the file!" << endl; out.close(); } // To search for a specific creteria in the linked list void double_llist::search() { cout << "1. Search by First name" << endl; cout << "2. Search by Last name" << endl; cout << "3. Search by Address" << endl; cout << "4. Search by Contact Number" << endl; cout << "5. Search by Email" << endl; cout << "6. Search by Twitter handle" << endl; int choice; cout << "Choice : "; cin >> choice; switch (choice) { case 1: { cout << "Enter first name: "; string fname; cin >> fname; struct node* q; if (start->fname == fname) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = start; while (q->next != NULL) { if (q->next->fname == fname) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = q->next; } cout << "\n\nContact not found!"; } case 2: { cout << "Enter last name: "; string lname; cin >> lname; struct node* q; if (start->lname == lname) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = start; while (q->next != NULL) { if (q->next->lname == lname) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = q->next; } cout << "\n\nContact not found!"; } case 3: { cout << "Enter address: "; string addr; cin >> addr; struct node* q; if (start->address == addr) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = start; while (q->next != NULL) { if (q->next->address == addr) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = q->next; } cout << "\n\nContact not found!"; } case 4: { cout << "Enter contact #: "; string ph; cin >> ph; struct node* q; if (start->ph == ph) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = start; while (q->next != NULL) { if (q->next->ph == ph) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = q->next; } cout << "\n\nContact not found!"; } case 5: { cout << "Enter email: "; string email; cin >> email; struct node* q; if (start->email == email) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = start; while (q->next != NULL) { if (q->next->email == email) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = q->next; } cout << "\n\nContact not found!"; } case 6: { cout << "Enter twitter handle: "; string twitter; cin >> twitter; struct node* q; if (start->twitter == twitter) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = start; while (q->next != NULL) { if (q->next->twitter == twitter) { cout << "\n\nContact Found!"; cout << "\nFirst name: " << start->fname; cout << "\nLast name: " << start->lname; cout << "\nAddress: " << start->address; cout << "\nContact #: " << start->ph; cout << "\nEmail: " << start->email; cout << "\nTwitter Handle: " << start->twitter; return; } q = q->next; } cout << "\n\nContact not found!"; } default: { cout << "\n\nInvalid entry!"; break; } } }