Question: / / Main . cpp #include SinglyLinkedList.h #include #include #include using namespace std; int main ( ) { LinkedList list; list.append ( new Person

//Main.cpp
#include "SinglyLinkedList.h"
#include
#include
#include
using namespace std;
int main(){
LinkedList list;
list.append(new Person(1, "John", "Doe"));
list.append(new Person(2, "Jane", "Doe"));
list.append(new Person(3, "Paul", "Harvey"));
Node* searchResult = listSearch(&list, 3);
if (searchResult){
cout "listSearch found: " searchResult->person->descriptor() endl;
} else {
cout "listSearch did not find the key." endl;
}
string descriptor = listDescriptor(&list);
cout "listDescriptor output:
" descriptor;
Node* current = list.head;
while (current){
Node* next = current->next;
delete current->person;
delete current;
current = next;
}
return 0;
}
//singleylinkedlist.h
#ifndef SINGLY_LINKED_LIST
#define SINGLY_LINKED_LIST
using namespace std;
class Node {
public:
Person* person; // Pointer to a Person instance
Node* next; // Pointer to the next Node instance
Node(Person* p = nullptr) : person(p), next(nullptr){}
};
class LinkedList {
public:
Node* head; // Pointer to the head of the linked list
Node* tail; // Pointer to the tail of the linked list
LinkedList() : head(nullptr), tail(nullptr){}
void append(Person* p){
Node* newNode = new Node(p);
if (!head){
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
};
Node* listSearch(LinkedList* list, const int key){
Node* current = list->head;
while (current){
if (current->person->id == key){
return current;
}
current = current->next;
}
return nullptr; // Key not found
}
string listDescriptor(LinkedList* list){
ostringstream oss;
Node* current = list->head;
while (current){
oss current->person->descriptor() endl;
current = current->next;
}
return oss.str();
}
#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!