Answer to Question #237644 in C++ for Owen

Question #237644

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 linked 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 a name, surname, age and gende


1
Expert's answer
2021-09-15T17:19:42-0400
Inserting a person in ordered linked list.
Age is the comparison factor for the list.
Insertion takes place when the Insert() function is called.
The function checks the age of the person to be inserted and the age of the person that is existing in the linked list. If the age is larger the function places that person on the right of that person.
Deleting a person in ordered linked list.
Previous node of the person to be deleted is obtained.
The next of the previous node is changed .
Memory is freed that was allocated to the node.


Implementation



#include <iostream>
using namespace std;
 


class Node {
public:
    string name;
	string surName;
	int age;
	string gender;
    Node* next;
};
 
void Insert(Node** head, Node* newNode)
{
    Node* currentNode;
    
    if (*head == NULL|| (*head)->age >= newNode->age) {
        newNode->next = *head;
        *head = newNode;
    }
    else {
        
        currentNode = *head;
        while (currentNode->next != NULL && currentNode->next->age< newNode->age) {
            currentNode = currentNode->next;
        }
        newNode->next = currentNode->next;
        currentNode->next = newNode;
    }
}
 
Node* new_node(string name,string surName,	int age,	string gender)
{
    
    Node* newNode = new Node();
 
   
    newNode->name = name;
    newNode->surName = surName;
    newNode->age = age;
    newNode->gender = gender;
    newNode->next = NULL;
 
    return newNode;
}
 


void printList(Node* head)
{
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->name << " "<<temp->surName<<"  "<<temp->age<<"  "<<temp->gender<<endl;
        temp = temp->next;
    }
}
 
 
void deleteNode(Node** head_ref, int key)
{
     
    
    Node* temp = *head_ref;
    Node* prev = NULL;
     
    
    if (temp != NULL && temp->age == key)
    {
        *head_ref = temp->next; 
        delete temp;            
        return;
    }
 
    
      else
    {
    while (temp != NULL && temp->age != key)
    {
        prev = temp;
        temp = temp->next;
    }
 
    
    if (temp == NULL)
        return;
 
   
    prev->next = temp->next;
 
    
    delete temp;
    }
}


int main()
{
    
    Node* head = NULL;
    Node* node = new_node("Donald", "Brown", 67, "M" );
    Insert(&head, node);
    node = new_node("Jason", "Travis", 90, "F");
    Insert(&head, node);
    node = new_node("Max", "Black", 27, "M");
    Insert(&head, node);
    node = new_node("Bobi", "Frank", 17, "F");
    Insert(&head, node);
   
    cout << "The list is sorted by gender in ascending order\n";
    printList(head);
    cout<<"After deleting a person who age is 17 the list becomes \n";
    deleteNode(&head, 17);
     printList(head);
     
     cout << "When a person whose is 20 is inserted the linked list becomes\n";
     node = new_node("Victoria", "Riberry", 20, "M");
    Insert(&head, node);
    printList(head);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS