Question: / / main . cpp #include SinglyLinkedList.h #include #include #include using namespace std; void listRemoveAfter ( LinkedList * list, Node * curNode ) {

//main.cpp
#include "SinglyLinkedList.h"
#include
#include
#include
using namespace std;
void listRemoveAfter(LinkedList* list, Node* curNode){
if (list == nullptr || list->head == nullptr){
// The list is empty or curNode is nullptr (head), nothing to remove.
return;
}
if (curNode == nullptr){
// If curNode is nullptr, remove the head node.
Node* temp = list->head;
list->head = list->head->next;
if (list->head == nullptr){
// If the list becomes empty, update the tail as well.
list->tail = nullptr;
}
delete temp;
} else {
// Remove the node after curNode.
Node* temp = curNode->next;
if (temp != nullptr){
curNode->next = temp->next;
if (temp == list->tail){
// If the removed node was the tail, update the tail.
list->tail = curNode;
}
delete temp;
}
}
}
string listDescriptor(LinkedList* list){
if (list == nullptr || list->head == nullptr){
return "";
}
ostringstream oss;
Node* current = list->head;
while (current != nullptr){
oss current->person.descriptor() endl;
current = current->next;
}
return oss.str();
}
int main(){
LinkedList list;
// Add nodes to the list
Person person1(1, "John", "Doe");
Person person2(2, "Jane", "Doe");
Person person3(3, "Mary", "Doe");
Person person4(4, "Paul", "Doe");
list.head = new Node(person1);
list.head->next = new Node(person2);
list.head->next->next = new Node(person3);
list.head->next->next->next = new Node(person4);
list.tail = list.head->next->next->next;
// Test listRemoveAfter
listRemoveAfter(&list, nullptr); // removes head
listRemoveAfter(&list, list.head->next); // removes tail
// Print the updated list
cout listDescriptor(&list) endl;
return 0;
}
//singlelinkedlist.h
#ifndef SINGLY_LINKED_LIST
#define SINGLY_LINKED_LIST
#include
using namespace std;
/* class: Node */
class Node {
public:
Person* person;
Node* next;
Node(const Person& p) : person(p), next(nullptr){}
};
/* class: LinkedList */
class LinkedList {
public:
Node* head;
Node* tail;
LinkedList() : head(nullptr), tail(nullptr){}
};
#endif
/ / main . cpp #include "SinglyLinkedList.h "

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!