I have a job to do. I've managed to write some code, but I'm having trouble with the rest.
Is anyone able to analyze the code and help me with this?
Read in from the keyboard n (n - read in by the user) subsequent data about persons (name,
name, age) and create a one-way list of them in the order in which they are loaded.
Print the created list using the function that prints the list beginning with some
address.
Insert new people into the list before every second person older than the penultimate person on
leaves. Fill in the data of new people with the data loaded by the user.
Print the list again
Delete the created list using the function that deletes the list starting at some address.
Print the list again.
Note 1: The word "some" means a function parameter.
Note 2: When performing various operations on the list (points 2-6), assume that we do not know the number of elements
list, so be careful not to refer to the next field of an element that doesn't exist (i.e. has a
NULL address); in particular, you should check if the list is not empty (head address ==NULL) and if it has
it has enough elements (so as not to perform impossible operations).
Note 3: Please put one person in front of each person who meets the condition.
#include<iostream>#include<string>usingnamespacestd;// A structure that stores information about a personstructPerson{stringfirstName;stringlastName;intage;Person*next;};// A function that prints a one-way list with a given startvoidprintList(Person*head){// Check if the list is not emptyif(head==NULL){cout<<"Lista jest pusta"<<endl;return;}// Printing each list itemcout<<"Lista: "<<endl;Person*curr=head;while(curr!=NULL){cout<<curr->firstName<<" "<<curr->lastName<<", wiek: "<<curr->age<<endl;curr=curr->next;}}// A function that adds new people to the list before every second person older than the penultimate person on the listvoidaddPersons(Person*head){// Check if the list has at least 3 elementsif(head==NULL||head->next==NULL||head->next->next==NULL){cout<<"The list is too short to perform this operation"<<endl;return;}// Load data of new peoplecout<<"Enter the data of new people (name, surname, age):"<<endl;Person*prev=head->next;Person*curr=head->next->next;while(curr!=NULL){if(curr->age>prev->age){// Tworzenie nowej osobyPerson*newPerson=newPerson();cin>>newPerson->firstName>>newPerson->lastName>>newPerson->age;newPerson->next=curr;// Adding a new person to the listprev->next=newPerson;// Movement of pointersprev=curr;curr=curr->next;}else{prev=curr;curr=curr->next;if(curr!=NULL){prev=curr;curr=curr->next;}}}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It looks like you've made a good start on your code. Here's a continuation and completion of the code to address the requirements you've mentioned:
#include<iostream>#include<string>usingnamespacestd;// A structure that stores information about a personstructPerson{stringfirstName;stringlastName;intage;Person*next;};// A function that prints a one-way list with a given startvoidprintList(Person*head){// Check if the list is not emptyif(head==NULL){cout<<"The list is empty"<<endl;return;}// Printing each list itemcout<<"List: "<<endl;Person*curr=head;while(curr!=NULL){cout<<curr->firstName<<" "<<curr->lastName<<", age: "<<curr->age<<endl;curr=curr->next;}}// A function that adds new people to the list before every second person older than the penultimate person on the listvoidaddPersons(Person*head){// Check if the list has at least 3 elementsif(head==NULL||head->next==NULL||head->next->next==NULL){cout<<"The list is too short to perform this operation"<<endl;return;}// Load data of new peoplecout<<"Enter the data of new people (name, surname, age):"<<endl;Person*prev=head->next;Person*curr=head->next->next;while(curr!=NULL){if(curr->age>prev->age){// Creating a new personPerson*newPerson=newPerson();cin>>newPerson->firstName>>newPerson->lastName>>newPerson->age;newPerson->next=curr;// Adding a new person to the listprev->next=newPerson;// Moving pointersprev=curr;curr=curr->next;}else{prev=curr;curr=curr->next;if(curr!=NULL){prev=curr;curr=curr->next;}}}}// Function to delete the created list starting from a given addressvoiddeleteList(Person*head){while(head!=NULL){Person*temp=head;head=head->next;deletetemp;}}intmain(){// Reading the number of persons from the userintn;cout<<"Enter the number of persons: ";cin>>n;// Reading data about persons and creating the listPerson*head=newPerson();cout<<"Enter the data of persons (name, surname, age):"<<endl;cin>>head->firstName>>head->lastName>>head->age;head->next=NULL;Person*curr=head;for(inti=1;i<n;++i){Person*newPerson=newPerson();cin>>newPerson->firstName>>newPerson->lastName>>newPerson->age;newPerson->next=NULL;curr->next=newPerson;curr=newPerson;}// Printing the initial listprintList(head);// Adding new persons to the listaddPersons(head);// Printing the list after adding new personsprintList(head);// Deleting the listdeleteList(head);// Printing the list after deletionprintList(head);return0;}
This code takes user input to create a list of persons and then adds new persons before every second person older than the penultimate person. It then prints the list before and after adding new persons and deletes the list. Remember to handle memory management properly to avoid memory leaks in a larger program.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have a job to do. I've managed to write some code, but I'm having trouble with the rest.
Is anyone able to analyze the code and help me with this?
name, age) and create a one-way list of them in the order in which they are loaded.
address.
leaves. Fill in the data of new people with the data loaded by the user.
Note 1: The word "some" means a function parameter.
Note 2: When performing various operations on the list (points 2-6), assume that we do not know the number of elements
list, so be careful not to refer to the next field of an element that doesn't exist (i.e. has a
NULL address); in particular, you should check if the list is not empty (head address ==NULL) and if it has
it has enough elements (so as not to perform impossible operations).
Note 3: Please put one person in front of each person who meets the condition.
It looks like you've made a good start on your code. Here's a continuation and completion of the code to address the requirements you've mentioned:
This code takes user input to create a list of persons and then adds new persons before every second person older than the penultimate person. It then prints the list before and after adding new persons and deletes the list. Remember to handle memory management properly to avoid memory leaks in a larger program.