You will be making a digital rolodex using a doubly linked list. Your program wi
ID: 3911042 • Letter: Y
Question
You will be making a digital rolodex using a doubly linked list. Your program will have the following criteria: 1. Each node in the list will contain the following information about the person: 1. First Name 2. Last Name 3. Address 4. Phone Number 5. Email Address 6. twitter handle. 2. When the program opens it will read contact information from a text file and create a doubly linked list with the information. 3. The user will be able to: search for and display specific user by any piece of information. Exact matches only is fine. Should be able to navigate through a list if there are multiple results. thus, you probably want to sort first. add a contact to the rolodex delete a user from the rolodex sort the rolodex by any piece of information and navigate through it, displaying one contact at a time. (they will need to be able to exit this navigation mode) quit the program. 4. Upon quitting, all contact information should be written back to the text file. The program should be in C++
Explanation / Answer
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
// Creates a structure to store information
struct digitalRolodex
{
string FirstName;
string LastName;
string Address;
string PhoneNumber;
string EmailAddress;
struct digitalRolodex *next;
struct digitalRolodex *prev;
}*start; // End of structure
// Defines a class for Double Linked List
class DoubleLinkedList
{
public:
// Prototype of functions
void createRecord();
void insRecordFirst();
void deleteRecord();
void searchRecord();
void displayRecord();
DoubleLinkedList();
};// End of class
// main function definition
int main()
{
// Local variable to store choice
int choice, element, position;
// DoubleLinkedList class object declared
DoubleLinkedList dl;
// Loops till user choice is not 5
while (1)
{
// Displays menu
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<" Digital Rolodex Operations"<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1. Create Record"<<endl;
cout<<"2. Insert Record"<<endl;
cout<<"3. Delete Record"<<endl;
cout<<"4. Display All Records"<<endl;
cout<<"5.Quit"<<endl;
// Accepts user choice
cout<<"Enter your choice : ";
cin>>choice;
// Checks use choice and calls appropriate function
switch (choice)
{
case 1:
cout<<" Enter record information: ";
dl.createRecord();
cout<<endl;
break;
case 2:
cout<<" Enter record information: ";
dl.insRecordFirst();
cout<<endl;
break;
case 3:
if (start == NULL)
{
cout<<"List empty, nothing to delete."<<endl;
break;
}
cout<<" Enter record information to DELETE: ";
dl.deleteRecord();
cout<<endl;
break;
case 4:
if (start == NULL)
{
cout<<"List empty, nothing to display."<<endl;
break;
}
dl.displayRecord();
cout<<endl;
break;
case 5:
exit(1);
default:
cout<<"Wrong choice! Try again."<<endl;
}// End of switch - chase
}// End of do - while loop
return 0;
}// End of main function
// Default constructor definition
DoubleLinkedList::DoubleLinkedList()
{
start = NULL;
}// End of default constructor
// Function to accept data
void accept(struct digitalRolodex *temp)
{
cout<<" Enter First Name: ";
cin>>temp->FirstName;
cout<<" Enter Last Name: ";
cin>>temp->LastName;
cout<<" Enter Address: ";
cin>>temp->Address;
cout<<" Enter Phone Number: ";
cin>>temp->PhoneNumber;
cout<<" Enter Email Address: ";
cin>>temp->EmailAddress;
}// End of function
// Function to display data
void display(struct digitalRolodex *temp)
{
cout<<" Enter First Name: "<<temp->FirstName;
cout<<" Enter Last Name: "<<temp->LastName;
cout<<" Enter Address: "<<temp->Address;
cout<<" Enter Phone Number: "<<temp->PhoneNumber;
cout<<" Enter Email Address: "<<temp->EmailAddress;
}// End of function
// Function to create a node
void DoubleLinkedList::createRecord()
{
struct digitalRolodex *s, *temp;
temp = new(struct digitalRolodex);
accept(temp);
temp->next = NULL;
if (start == NULL)
{
temp->prev = NULL;
start = temp;
}// End of if condition
else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}// End of else
}// End of function
// Function to insert a node at beginning
void DoubleLinkedList::insRecordFirst()
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
}// End of if condition
struct digitalRolodex *temp;
temp = new(struct digitalRolodex);
temp->prev = NULL;
accept(temp);
temp->next = start;
start->prev = temp;
start = temp;
cout<<"Element Inserted"<<endl;
}// End of function
// Function to delete a record
void DoubleLinkedList::deleteRecord()
{
struct digitalRolodex *temp, *q;
accept(temp);
// First node deletion
if (start->FirstName == temp->FirstName && start->LastName == temp->LastName)
{
temp = start;
start = start->next;
start->prev = NULL;
cout<<"Element Deleted"<<endl;
free(temp);
return;
}// End of if condition
q = start;
while (q->next->next != NULL)
{
/*Element deleted in between*/
if (q->next->FirstName == temp->FirstName && q->next->LastName == temp->LastName)
{
temp = q->next;
q->next = temp->next;
temp->next->prev = q;
cout<<"Element Deleted"<<endl;
free(temp);
return;
}// End of if condition
q = q->next;
}// End of while loop
// Last node deleted
if (q->next->FirstName == temp->FirstName && q->next->LastName == temp->LastName)
{
temp = q->next;
free(temp);
q->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}// End of if condition
cout<<"Element "<<temp->FirstName<<" not found"<<endl;
}// End of function
// Function to display all the records
void DoubleLinkedList::displayRecord()
{
struct digitalRolodex *q;
if (start == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}// End of if condition
q = start;
cout<<"The Doubly Link List is :"<<endl;
while (q != NULL)
{
display(q);
q = q->next;
}// End of while loop
cout<<"NULL"<<endl;
}// End of function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.