C++ DATA STRUCTURES 5 For this question, the information known about a person is
ID: 3734715 • Letter: C
Question
C++ DATA STRUCTURES
5 For this question, the information known about a person is: First name Last name . Age (in days) Define structs or classes suitable for representing people and linked lists of people; give each one a suitable constructor. Define a method for adding a person to a linked list. Define a method that finds, and returns as its result, the oldest person in a linked list. Read part d before writing your solution. d. Define a method that finds, and returns as its result, the oldest person in a linked list, and which also removes that person from the li If you wrote your solution to part c clearly, and left suitable spaces in it, you may save time by writing this answer as additions to the previous one. Make sure I can still tell which part is which nked list. Using your previous work, define a method that sorts a linked list of people so that they appear in order of increasing age.Explanation / Answer
#include<iostream>
using namespace std;
struct person{
string fname;
string lname;
int age;
};
class Node{
person p;
Node *next;
public:
Node(){
next = nullptr;
p.age = 0;
p.fname = "";
p.lname = "";
}
Node(person p,Node *nex){
this->p.age = p.age;
this->p.fname = p.fname;
this->p.lname = p.lname;
next = nex;
}
void setPerson(person p){
this->p.age = p.age;
this->p.fname = p.fname;
this->p.lname = p.lname;
}
person getPerson()const{
return p;
}
Node* getNext()const{
return next;
}
void setNext(Node *n){
next = n;
}
};
class LinkList{
Node *head;
int size;
public:
LinkList(){
head = nullptr;
size = 0;
}
void add(person p){
Node *temp = new Node;
temp->setPerson(p);
temp->setNext(head);
head = temp;
size++;
}
int getSize()const{
return size;
}
Node* get(int i){
if(isEmpty()){
return nullptr;
}
if(i < 0 || i >= size){
return nullptr;
}
Node *temp = head;
for(int j = 0;j<i;j++){
temp = temp->getNext();
}
return temp;
}
void del(person p){
if(isEmpty()){
return;
}
Node *temp = head;
Node *prev = nullptr;
while(temp != nullptr){
if(p.fname == temp->getPerson().fname && p.lname == temp->getPerson().lname){
if(prev == nullptr){
head = head->getNext();
delete temp;
size--;
return;
}
prev->setNext(temp->getNext());
delete temp;
size--;
return;
}
prev = temp;
temp = temp->getNext();
}
}
person findOldest(){
if(isEmpty()){
return person();
}
person tp = head->getPerson();
Node *temp = head;
while(temp != nullptr){
if(tp.age < temp->getPerson().age){
tp = temp->getPerson();
}
temp = temp->getNext();
}
return tp;
}
person findAndDeleteOldPerson(){
person p = findOldest();
del(p);
return p;
}
bool search(person p){
if(isEmpty()){
return false;
}
Node *temp = head;
while(temp != nullptr){
if(p.fname == temp->getPerson().fname && p.lname == temp->getPerson().lname){
return true;
}
temp = temp->getNext();
}
return false;
}
bool isEmpty(){
if(size == 0){
return true;
}
return false;
}
void print(){
if(isEmpty()){
return;
}
Node *temp = head;
while(temp != nullptr){
cout<<" Name : "<<temp->getPerson().fname<<" "<<temp->getPerson().lname;
cout<<" Age : "<<temp->getPerson().age;
cout<<endl;
temp = temp->getNext();
}
}
};
void sort(LinkList &l){
LinkList temp;
int n = l.getSize();
for(int i = 0;i<n;i++){
temp.add(l.findAndDeleteOldPerson());
}
for(int i = 0;i<n;i++){
l.add(temp.findAndDeleteOldPerson());
}
}
int main(){
LinkList ll;
person p1,p2,p3,p4;
p1.age = 10;
p1.fname = "dkp";
p1.lname = "kumar";
p2.age = 100;
p2.fname = "dkp1";
p2.lname = "kumar";
p3.age = 50;
p3.fname = "dkp2";
p3.lname = "kumar";
p4.age = 5;
p4.fname = "dkp3";
p4.lname = "kumar";
ll.add(p1);
ll.add(p2);
ll.add(p3);
ll.add(p4);
ll.print();
cout<<" Sorted Records ";
sort(ll);
ll.print();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.