You have an ordered linked list with 5 object of type person in it.
Explain with an example of how to add and delete a person in an ordered list. Your list should remain ordered after the insertion and after the deletion of the item.
Note: a Person in this case should be an object that has name, surname, age and gender
#include <iostream>
#include <string>
using namespace std;
struct Person{
string name;
string surname;
int age;
string gender;
};
struct LinkedList{
struct Person* person;
LinkedList *next;
};
void sortedInsert(struct LinkedList**LinkedListHead, struct Person* person){
LinkedList*temp = *LinkedListHead;
LinkedList *t = new LinkedList;
if(*LinkedListHead==NULL)
{
*LinkedListHead = new LinkedList;
(*LinkedListHead)->person = person;
(*LinkedListHead)->next = NULL;
}
else
{
if(person->surname.compare(temp->person->surname)<0)
{
t = new LinkedList;
t->person = person;
t->next = *LinkedListHead;
*LinkedListHead = t;
}else
{
while(temp->next != NULL && !(person->surname.compare(temp->next->person->surname)<0 && person->surname.compare(temp->person->surname)>=0))
temp=temp->next;
if(temp->next == NULL)
{
temp->next = new LinkedList;
temp = temp->next;
temp->person = person;
temp->next = NULL;
}
else
{
t = new LinkedList;
t->person = person;
t->next = temp->next;
temp->next = t;
}
}
}
}
void deleteFromLinkList(struct LinkedList**LinkedListHead, string name, string surname){
struct LinkedList*temp = *LinkedListHead;
if (temp->person->name.compare(name)==0 && temp->person->surname.compare(surname)==0)
{
temp =temp->next;
*LinkedListHead=temp;
}
else
{
LinkedList* tempNode = *LinkedListHead;
LinkedList* tempPrevious = *LinkedListHead;
bool isFound = false;
while (!(isFound = (tempNode->person->name.compare(name)==0 && tempNode->person->surname.compare(surname)==0)) && tempNode->next != NULL)
{
tempPrevious = tempNode;
tempNode = tempNode->next;
}
if (isFound)
{
tempPrevious->next = tempNode->next;
}
else
{
cout<<"Person not found!\n";
}
}
}
void printLinkedList(struct LinkedList**LinkedListHead)
{
struct LinkedList*temp=*LinkedListHead;
cout<<"All person sorted by surname:\n";
while(temp!=NULL)
{
cout<<"Name: "<<temp->person->name<<"\n";
cout<<"Surname: "<<temp->person->surname<<"\n";
cout<<"Gender: "<<temp->person->gender<<"\n";
cout<<"Age: "<<temp->person->age<<"\n\n";
temp=temp->next;
}
cout<<endl;
}
int main()
{
struct LinkedList *LinkedListHead = NULL;
struct Person* person1=new Person;
person1->name="Peter";
person1->surname ="Smith";
person1->age=35;
person1->gender="Male";
struct Person* person2=new Person;
person2->name="Mary";
person2->surname ="Clark";
person2->age=32;
person2->gender="Female";
struct Person* person3=new Person;
person3->name="Julia";
person3->surname ="Peterson";
person3->age=22;
person3->gender="Female";
struct Person* person4=new Person;
person4->name="Mike";
person4->surname ="Johnson";
person4->age=36;
person4->gender="Male";
struct Person* person5=new Person;
person5->name="Den";
person5->surname ="Andy";
person5->age=43;
person5->gender="Male";
sortedInsert(&LinkedListHead,person1);
sortedInsert(&LinkedListHead,person2);
sortedInsert(&LinkedListHead,person3);
sortedInsert(&LinkedListHead,person4);
sortedInsert(&LinkedListHead,person5);
printLinkedList(&LinkedListHead);
cout<<"Delete Mike Johnson from link list\n\n";
deleteFromLinkList(&LinkedListHead,"Mike","Johnson");
printLinkedList(&LinkedListHead);
cout<<"Delete Mike Johnson from link list\n\n";
deleteFromLinkList(&LinkedListHead,"Mike","Johnson");
printLinkedList(&LinkedListHead);
cout<<"Delete Den Andy from link list\n\n";
deleteFromLinkList(&LinkedListHead,"Den","Andy");
printLinkedList(&LinkedListHead);
int k;
cin>>k;
return 0;
}
Comments
Leave a comment