Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

I have to update this assignment to store the output into a binary tree instead of a linked list(which it does now).

1 answer below »
#include #include #include #include #include #include using namespace std;
struct person
{
int social;
int birth_day;
string first_name;
string last_name;
int zip_code;
person(int ssn, int bd, string fn, string ln, int zip)
{
XXXXXXXXXXsocial = ssn;
XXXXXXXXXXbirth_day = bd;
XXXXXXXXXXfirst_name = fn;
XXXXXXXXXXlast_name = ln;
XXXXXXXXXXzip_code = zip;
}
};
struct link
{
person *info;
link *next;
link(person *i = NULL, link *n = NULL)
{
XXXXXXXXXXinfo = i;
XXXXXXXXXXnext = n;
}
};
void print_list(link *content)
{
while (content != NULL)
{
XXXXXXXXXXcout
content->info->social
" "
content->info-
irth_day
" "
content->info->first_name
" "
content->info->last_name
" "
content->info->zip_code
"\n";
XXXXXXXXXXcontent = content->next;
}
}
person *find(link *content, string f, string l)
{
while (content != NULL)
{
XXXXXXXXXXif (content->info->first_name == f && content->info->last_name == l)
XXXXXXXXXXreturn content->info;
XXXXXXXXXXcontent = content->next;
}
return NULL;
}
void save_to_file(link *content) {
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast(now.time_since_epoch());
std::ostringstream filename;
filename
"database_"
timestamp.count()
".txt";
std::ofstream file(filename.str());
if (!file.is_open()) {
XXXXXXXXXXstd::ce

"E
or: Could not open file for writing!"
std::endl;
XXXXXXXXXXreturn;
}
while (content != NULL)
{
XXXXXXXXXXfile
content->info->social
" "
content->info-
irth_day
" "
content->info->first_name
" "
content->info->last_name
" "
content->info->zip_code
std::endl;
XXXXXXXXXXcontent = content->next;
}
file.close();
std::cout
"Saved database to "
filename.str()
std::endl;
}
void sort_name(link *content)
{
if (content == NULL || content->next == NULL) {
XXXXXXXXXXreturn;
}
link *cu
ent = content;
link *index = NULL;
person *temp = NULL;
while (cu
ent != NULL) {
XXXXXXXXXXindex = cu
ent->next;
XXXXXXXXXXwhile (index != NULL) {
XXXXXXXXXXif (cu
ent->info->last_name > index->info->last_name) {
XXXXXXXXXXtemp = cu
ent->info;
XXXXXXXXXXcu
ent->info = index->info;
XXXXXXXXXXindex->info = temp;
}
XXXXXXXXXXelse if (cu
ent->info->last_name == index->info->last_name &&
XXXXXXXXXXcu
ent->info->first_name > index->info->first_name) {
XXXXXXXXXXtemp = cu
ent->info;
XXXXXXXXXXcu
ent->info = index->info;
XXXXXXXXXXindex->info = temp;
}
XXXXXXXXXXindex = index->next;
}
XXXXXXXXXXcu
ent = cu
ent->next;
}
cout
"Sorted list based on name.\n";
save_to_file(content);
}
void delete_person(link *content, string f, string l) {
link *prev = NULL;
link *cu
= content;

while (cu
!= NULL)
{
XXXXXXXXXXif (cu
->info->first_name == f && cu
->info->last_name == l)
{
XXXXXXXXXXif (prev == NULL)
{
XXXXXXXXXXcontent = cu
->next;
}
XXXXXXXXXXelse
{
XXXXXXXXXXprev->next = cu
->next;
}
XXXXXXXXXXdelete cu
->info;
XXXXXXXXXXdelete cu
;
XXXXXXXXXXcout
"Person deleted successfully.\n";
XXXXXXXXXXsave_to_file(content);
XXXXXXXXXXreturn;
}
XXXXXXXXXXprev = cu
;
XXXXXXXXXXcu
= cu
->next;
}
cout
"Person not found with "
f
" and "
l
" in database.\n";
}
void find_all(link *content, string s)
{
while (content != NULL)
{
XXXXXXXXXXif (content->info->last_name == s)
XXXXXXXXXXcout
content->info->social
" "
content->info-
irth_day
" "
content->info->first_name
" "
content->info->last_name
" "
content->info->zip_code
"\n";
XXXXXXXXXXcontent = content->next;
}
}
void find_zip(link *content, int a)
{
link *allzip = NULL;
while (content != NULL)
{
XXXXXXXXXXif (content->info->zip_code == a)
XXXXXXXXXXallzip = new link(content->info, allzip);
XXXXXXXXXXcontent = content->next;
}
print_list(allzip);
}
void kill_zip(link *content, int zipcode)
{
if(content == NULL) {
XXXXXXXXXXreturn;
}
while(content != NULL && content->info->zip_code == zipcode) {
XXXXXXXXXXlink *temp = content;
XXXXXXXXXXcontent = content->next;
XXXXXXXXXXdelete temp;
}
link *cu
= content;
while (cu
!= NULL && cu
->next != NULL) {
XXXXXXXXXXif (cu
->next->info->zip_code == zipcode) {
XXXXXXXXXXlink *temp = cu
->next;
XXXXXXXXXXcu
->next = cu
->next->next;
XXXXXXXXXXdelete temp;
}
XXXXXXXXXXelse {
XXXXXXXXXXcu
= cu
->next;
}
}
cout
"Person found with "
zipcode
"got deleted.\n";
save_to_file(content);
}
int find_oldest(link *content)
{
int oldest = content->info-
irth_day;
while (content != NULL)
{
XXXXXXXXXXif (content->info-
irth_day < oldest)
XXXXXXXXXXoldest = content->info-
irth_day;
XXXXXXXXXXcontent = content->next;
}
return oldest;
}
void sort_age(link *content) {
bool swapped = true;
while (swapped) {
XXXXXXXXXXswapped = false;
XXXXXXXXXXlink* cu
ent = content;
XXXXXXXXXXlink* prev = nullptr;
XXXXXXXXXXwhile (cu
ent != nullptr && cu
ent->next != nullptr) {
XXXXXXXXXXif (cu
ent->info-
irth_day < cu
ent->next->info-
irth_day) {
XXXXXXXXXXlink* next = cu
ent->next;
XXXXXXXXXXcu
ent->next = next->next;
XXXXXXXXXXnext->next = cu
ent;
XXXXXXXXXXif (prev == nullptr) {
XXXXXXXXXXcontent = next;
} else {
XXXXXXXXXXprev->next = next;
}
XXXXXXXXXXprev = next;
XXXXXXXXXXswapped = true;
} else {
XXXXXXXXXXprev = cu
ent;
XXXXXXXXXXcu
ent = cu
ent->next;
}
}
}
cout
"Sorted list based on age.\n";
save_to_file(content);
}
void read_file(string fileName, link *content)
{
ifstream fin;
fin.open(fileName);
int ssn, bd, zip;
string fn, ln;
if (fin.fail())
{
XXXXXXXXXXcout
"could not open file"

"\n";
}
else
{
XXXXXXXXXXwhile (fin
ssn
bd
fn
ln
zip)
{
XXXXXXXXXXperson *p = new person(ssn, bd, fn, ln, zip);
XXXXXXXXXXlink *l = new link(p, content);
XXXXXXXXXXcontent = l;
}
}
fin.close();
link *all = NULL;
while (true)
{
XXXXXXXXXXcout
"\n Command list: "

"\n"

"Find - displays all information about the named person\n"

"\n"

"All - displays all information about all people who have the given last name\n"

"\n"

"Zip - make a new linked list of all people living in the given zip code, then displays the entire contents of that linked list.\n"

"\n"

"Oldest - print the name and zipcode of the oldest person in the database.\n"

"\n"

"Exit - exit from the program.\n"

"\n"

"Delete -
Answered 2 days After Apr 24, 2023

Solution

Sanskar answered on Apr 27 2023
27 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here