The document describes a C++ program that implements a linked list to store names and phone numbers. The program presents a menu with options to insert, modify, delete, find records in the linked list. The code includes function prototypes and implementations for populating, displaying, inserting, modifying and removing records from the linked list. The author is asking for help implementing the delete and find functions, as those functions have not been fully implemented in the code provided.
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
1. Hello Everyone!!!
I’m writing a c++ program that presents a menu to do the following options:
1. Create a linked list of names and phone numbers.
2. Insert a new structure in the linked list.
3. Modify an existing structure in the linked list.
4. Delete an existing structure in the linked list.
5. Find an existing structure from the linked list.
Comments:
* Inserting and modifying functios work perfect.
* Can you guys please tell me how to implement the deleting and the find functions. See my
code bellow, please run it and see what happens.
This is my code:
#include
#include
#include
#include
using namespace std;
struct TeleType
{
string Name;
string PhoneNum;
TeleType *NextAddr;
};
bool Check();
void Populate(TeleType *); // Populate Function Prototype.
void DisplayRecord(TeleType *); // DisplayRecord Function Prototype.
void InsertRecord(TeleType*, TeleType *); // InsertRecord Function Prototype.
void RemoveRecord(TeleType *, TeleType *); // RemoveRecord Function Prototype.
void ModifyRecord(TeleType *); // ModifyRecord Function Prototype.
void FindRecord(TeleType *); // Find Function Prototype.
TeleType *List; // Pointer
int main()
{
2. int Location = 0;
int Count = 0;
char Answery_n;
TeleType *Previous = NULL; //Pointer
TeleType *Record=NULL; // Pointer
TeleType *Current; // Pointer
List = new TeleType;
Current = List;
cout << "Please ";
do
{
Count++;
Populate(Current);
if (Check() == false)
{
cout << "Not storage available" << endl;
}
else
{
Current->NextAddr = new TeleType;
Current = Current->NextAddr;
cout << "Would you like to input more data? Y/N? :";
cin >> Answery_n;
cout << endl;
cin.get();
if (Answery_n != 'y')
{
Current->NextAddr = NULL;
break;
}
}
}
while (Answery_n == 'y');
cout << "The Linked list records: " << endl;
DisplayRecord(List);
cout << "There are " << Count << " records in the data file. " << endl<< endl;
3. while (1)
{
fflush(stdout);
cout << "Select from the menu" << endl;
cout << "1. Insert new structure in the linked list" << endl;
cout << "2. Modify an existing structure from the linked list" << endl;
cout << "3. Delete an existing structure from the linked list" << endl;
cout << "4. Find and existing structure from the linked list" << endl;
cout << "5. Exit from the program" << endl;
cin >> Answery_n;
// Inserting A New Structure.
if (Answery_n == '1')
{
cout << "Insert a new record after records 1, 2, 3..." << endl;
cin.get();
cin >> Location;
cout << endl;
Current = List;
if (Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1
<< endl;
}
else
{
for (int i = 0; Current -> NextAddr != NULL; i++)
{
if (i == Location)
{
InsertRecord(Previous, Record);
Count++;
break;
}
else
{
Previous = Current;
4. Current = Current->NextAddr;
}
}
cout << "The list after insertion holds the following records"<< endl << endl;
DisplayRecord(List);
cout << "There are " << Count << " records." << endl << endl;
}
}
// Modifying An Existing Structure.
else if (Answery_n == '2')
{
cout << "Modify a record after records 1, 2, 3..." << endl;
cin >> Location;
cout << endl;
Current = List;
if (Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1
<< endl;
}
else
{
for (int i = 0; Current -> NextAddr != NULL; i++)
{
if (i == Location)
{
ModifyRecord(Current);
}
else
{
Current = Current -> NextAddr;
}
}
cout << "The list after modification holds the following records"<< endl << endl;
DisplayRecord(List);
cout << "There are " << Count << " records." << endl << endl;
5. }
}
// Deleting An Existing Structure.
else if (Answery_n == '3')
{
cout << "Delete a record after records 1, 2, 3..." << endl;
cin >> Location;
cout << endl;
Current = List;
if (Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1 << endl;
}
else
{
for (int i = 1; Current -> NextAddr != NULL; i++)
{
if (i == Location)
{
RemoveRecord(Previous, Current);
}
else
{
Previous = Current;
Current = Current -> NextAddr;
}
}
cout << "The list after deletion holds the following records"<< endl << endl;
DisplayRecord(List);
cout << "There are " << Count << " records." << endl << endl;
}
}
// Finding An Existing Structure.
else if(Answery_n == '4')
{
cout << "Find a new record 1, 2, 3..." << endl;
6. cin >> Location;
cout << endl;
List = new TeleType;
if(Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1 << endl;
}
else
{
for(int i=1; List -> NextAddr != NULL; i++)
{
}
}
// Terminating Program.
} else if(Answery_n == '5')
{
cout << "Terminating the program" << endl;
exit(1);
}
else
{
cout << "Invalid Option entered" << endl;
}
}
while(Answery_n != 5);
return 0;
}
//Populate Function.
void Populate(TeleType *Record)
{
if(Record!=NULL)
{
cout << "Enter a name: ";
getline(cin, Record->Name);
cout << "Enter a phone number: ";
getline(cin, Record->PhoneNum);
7. }
}
//Display Record Function.
void DisplayRecord(TeleType *Contents)
{
while (Contents != NULL)
{
cout << endl << setiosflags(ios::left) << setw(30) << Contents->Name << setw(20)<<
Contents->PhoneNum;
Contents = Contents->NextAddr;
}
cout << endl;
return;
}
void InsertRecord(TeleType *Previous, TeleType *IR)//Insert Record Function.
{
cin.get();
IR = new TeleType;
Populate(IR);
if(Previous == NULL)
{
IR -> NextAddr = List;
List = IR;
}
else
{
IR -> NextAddr = Previous -> NextAddr;
Previous -> NextAddr = IR;
}
}
// Modify Record Function.
void ModifyRecord(TeleType *MR)
{
cin.get();
Populate(MR);
9. #include
#include
#include
using namespace std;
struct TeleType
{
string Name;
string PhoneNum;
TeleType *NextAddr;
};
bool Check();
void Populate(TeleType *); // Populate Function Prototype.
void DisplayRecord(TeleType *); // DisplayRecord Function Prototype.
void InsertRecord(TeleType*, TeleType *); // InsertRecord Function Prototype.
void RemoveRecord(TeleType *, TeleType*); // RemoveRecord Function Prototype.
void ModifyRecord(TeleType *); // ModifyRecord Function Prototype.
void FindRecord(TeleType *); // Find Function Prototype.
TeleType *List; // Pointer
int main()
{
int Location = 0;
int Count = 0;
char Answery_n;
TeleType *Previous = NULL; //Pointer
TeleType *Record=NULL; // Pointer
TeleType *Current; // Pointer
List = new TeleType;
Current = List;
cout << "Please ";
do
{
Count++;
Populate(Current);
if (Check() == false)
{
cout << "Not storage available" << endl;
10. }
else
{
Current->NextAddr = new TeleType;
Current = Current->NextAddr;
cout << "Would you like to input more data? Y/N? :";
cin >> Answery_n;
cout << endl;
cin.get();
if (Answery_n != 'y')
{
Current->NextAddr = NULL;
break;
}
}
}
while (Answery_n == 'y');
cout << "The Linked list records: " << endl;
DisplayRecord(List);
cout << "There are " << Count << " records in the data file. " << endl<< endl;
while (1)
{
fflush(stdout);
cout << "Select from the menu" << endl;
cout << "1. Insert new structure in the linked list" << endl;
cout << "2. Modify an existing structure from the linked list" << endl;
cout << "3. Delete an existing structure from the linked list" << endl;
cout << "4. Find and existing structure from the linked list" << endl;
cout << "5. Exit from the program" << endl;
cin >> Answery_n;
// Inserting A New Structure.
if (Answery_n == '1')
{
cout << "Insert a record after records 1, 2, 3..." << endl;
cin.get();
cin >> Location;
11. cout << endl;
Current = List;
if (Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1
<< endl;
}
else
{
for (int i = 0; Current -> NextAddr != NULL; i++)
{
if (i == Location)
{
InsertRecord(Previous, Record);
Count++;
break;
}
else
{
Previous = Current;
Current = Current->NextAddr;
}
}
cout << "The list after insertion holds the following records"<< endl << endl;
DisplayRecord(List);
cout << "There are " << Count << " records." << endl << endl;
}
}
// Modifying An Existing Structure.
else if (Answery_n == '2')
{
cout << "Modify a record after records 1, 2, 3..." << endl;
cin >> Location;
cout << endl;
Current = List;
if (Location > Count || Location < 1)
12. {
cout << "You must enter the value no grater than " << Count - 1
<< endl;
}
else
{
for (int i = 0; Current -> NextAddr != NULL; i++)
{
if (i == Location)
{
ModifyRecord(Current);
}
else
{
Current = Current -> NextAddr;
}
}
cout << "The list after modification holds the following records"<< endl << endl;
DisplayRecord(List);
cout << "There are " << Count << " records." << endl << endl;
}
}
// Deleting An Existing Structure.
else if (Answery_n == '3')
{
cout << "Delete a record after records 1, 2, 3..." << endl;
cin >> Location;
cout << endl;
Current = List;
if (Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1 << endl;
}
else
{
for (int i = 0; Current -> NextAddr != NULL; i++)
13. {
if (i == Location)
{
RemoveRecord(Previous, Current);
}
else
{
Previous = Current;
Current = Current -> NextAddr;
}
}
cout << "The list after deletion holds the following records"<< endl << endl;
DisplayRecord(List);
cout << "There are " << Count << " records." << endl << endl;
}
}
// Finding An Existing Structure.
else if(Answery_n == '4')
{
cout << "Find an existing record" << endl;
cin >> Location;
cout << endl;
List = new TeleType;
if(Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1 << endl;
}
else
{
for(int i=1; List -> NextAddr != NULL; i++)
{
}
}
// Terminating Program.
} else if(Answery_n == '5')
{
17. void RemoveRecord(TeleType *, TeleType *); // RemoveRecord Function Prototype.
void ModifyRecord(TeleType *); // ModifyRecord Function Prototype.
void FindRecord(TeleType *); // Find Function Prototype.
int Count1(TeleType *);
TeleType *List; // Pointer
int main()
{
int Location = 0;
int Count = 0;
char Answery_n;
TeleType *Previous = NULL; //Pointer
TeleType *Record=NULL; // Pointer
TeleType *Current; // Pointer
List = new TeleType;
Current = List;
cout << "Please ";
do
{
Count++;
Populate(Current);
if (Check() == false)
{
cout << "Not storage available" << endl;
}
else
{
Current->NextAddr = new TeleType;
Current = Current->NextAddr;
cout << "Would you like to input more data? Y/N? :";
cin >> Answery_n;
cout << endl;
cin.get();
if (Answery_n != 'y')
{
Current->NextAddr = NULL;
break;
18. }
}
}
while (Answery_n == 'y');
cout << "The Linked list records: " << endl;
DisplayRecord(List);
cout << "There are " << Count1(List) << " records in the data file. " << endl<< endl;
while (1)
{
fflush(stdout);
cout << "Select from the menu" << endl;
cout << "1. Insert new structure in the linked list" << endl;
cout << "2. Modify an existing structure from the linked list" << endl;
cout << "3. Delete an existing structure from the linked list" << endl;
cout << "4. Find and existing structure from the linked list" << endl;
cout << "5. Exit from the program" << endl;
cin >> Answery_n;
// Inserting A New Structure.
if (Answery_n == '1')
{
cout << "Insert a new record after records 1, 2, 3..." << endl;
cin.get();
cin >> Location;
cout << endl;
Current = List;
if (Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1
<< endl;
}
else
{
for (int i = 0; Current -> NextAddr != NULL; i++)
{
if (i == Location)
{
19. InsertRecord(Previous, Record);
Count++;
break;
}
else
{
Previous = Current;
Current = Current->NextAddr;
}
}
cout << "The list after insertion holds the following records"<< endl << endl;
DisplayRecord(List);
cout << "There are " << Count1(List) << " records." << endl << endl;
}
}
// Modifying An Existing Structure.
else if (Answery_n == '2')
{
cout << "Modify a record after records 1, 2, 3..." << endl;
cin >> Location;
cout << endl;
Current = List;
if (Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1
<< endl;
}
else
{
for (int i = 0; Current -> NextAddr != NULL; i++)
{
if (i == Location)
{
ModifyRecord(Current);
}
else
20. {
Current = Current -> NextAddr;
}
}
cout << "The list after modification holds the following records"<< endl << endl;
DisplayRecord(List);
cout << "There are " << Count1(List) << " records." << endl << endl;
}
}
// Deleting An Existing Structure.
else if (Answery_n == '3')
{
cout << "Delete a record after records 1, 2, 3..." << endl;
cin >> Location;
cout << endl;
Current = List;
Previous = List ;
if (Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1 << endl;
}
else
{
for (int i = 1; Current -> NextAddr != NULL; i++)
{
if (i == Location)
{
RemoveRecord(Previous, Current);
}
else
{
Previous = Current;
Current = Current -> NextAddr;
}
}
cout << "The list after deletion holds the following records"<< endl << endl;
21. DisplayRecord(List);
cout << "There are " << Count1(List) << " records." << endl << endl;
}
}
// Finding An Existing Structure.
else if(Answery_n == '4')
{
cout << "Find a new record 1, 2, 3..." << endl;
cin >> Location;
cout << endl;
//List = new TeleType;
TeleType *temp;
temp=List;
if(Location > Count || Location < 1)
{
cout << "You must enter the value no grater than " << Count - 1 << endl;
}
else
{
for(int i=1; iNextAddr;
}
cout<<"Element at "<< Location<<" is "<Name<<" and Phone is "<PhoneNum<Name);
cout << "Enter a phone number: ";
getline(cin, Record->PhoneNum);
}
}
//Display Record Function.
void DisplayRecord(TeleType *Contents)
{
while (Contents != NULL)
{
cout << endl << setiosflags(ios::left) << setw(30) << Contents->Name << setw(20)<< Contents-
>PhoneNum;
Contents = Contents->NextAddr;
}
cout << endl;
22. return;
}
void InsertRecord(TeleType *Previous, TeleType *IR)//Insert Record Function.
{
cin.get();
IR = new TeleType;
Populate(IR);
if(Previous == NULL)
{
IR -> NextAddr = List;
List = IR;
}
else
{
IR -> NextAddr = Previous -> NextAddr;
Previous -> NextAddr = IR;
}
}
// Modify Record Function.
void ModifyRecord(TeleType *MR)
{
cin.get();
Populate(MR);
return;
}
// Remove Record Function.
void RemoveRecord(TeleType* Previous, TeleType* RR)
{
cin.get();
TeleType *t,*t1;
t = Previous;
t1=RR;
if(t == t1)
{
List = t->NextAddr;