If someone can explain to me what this code does by . commenting on it. main.cpp
ID: 3869035 • Letter: I
Question
If someone can explain to me what this code does by . commenting on it.
main.cpp
--------------------------
#include <iostream>
#include <string>
#include <assert.h>
#include "PeopleList.h"
using namespace std;
/*
int main() {
PeopleList result;
PeopleList m1;
PeopleList m2;
result.add("Allen", "Amy", 1000);
m1.add("Skyler", "White", 45);
m1.add("James", "McGill", 49);
m1.add("Charles", "McGill", 58);
m2.add("Walter", "White", 52);
m2.add("Jesse", "Pinkman", 27);
cout << combine(m1, m2, result) << endl;
for (int n = 0; n < result.size(); n++)
{
string f;
string l;
int v;
result.get(n, f, l, v);
cout << f << " " << l << " " << v << endl;
}
}
*/
/*int main() {
PeopleList p1;
PeopleList result;
p1.add("Gustavo", "Fring", 57);
p1.add("Skyler", "White", 45);
p1.add("Walter", "White", 45);
p1.add("Jane", "Doe", 35);
p1.add("Marie", "Schrader", 37);
p1.add("Jane", "Margolis", 27);
search("*", "*", p1, result);
for (int n = 0; n < result.size(); n++)
{
string f;
string l;
int v;
result.get(n, f, l, v);
cout << f << " " << l << " " << v << endl;
}
}*/
#include "PeopleList.h"
#include <string>
#include <iostream>
#include <cassert>
using namespace std;
void test()
{
PeopleList m;
assert(m.add("Fred", "Mertz", 52));
assert(m.add("Ethel", "Mertz", 49));
assert(m.size() == 2);
string first, last;
int a;
assert(m.get(0, first, last, a) && a == 49);
string s1;
assert(m.get(1, first, last, a) &&
(first == "Fred" && a == 52));
}
int main()
{
test();
cout << "Passed all tests" << endl;
}
------------------------------------------------------
PeopleList.cpp
-----------------------------
#include <iostream>
#include <string>
#include "PeopleList.h"
PeopleList::PeopleList()
:listSize(0) {
Node *newNode = new Node;
head = newNode;
head -> next = head;
head -> previous = head;
head -> m_firstName = "";
head -> m_lastName = "";
}
PeopleList::PeopleList(const PeopleList &other) {
Node *newNode = new Node;
head = newNode;
head->next = head;
head->previous = head;
head->m_firstName = "";
head->m_lastName = "";
Node *p = other.head->next;
Node *n = head;
while (p!=other.head) {
Node *newNode = new Node;
newNode->next = head;
newNode->previous = n;
n->next = newNode;
head->previous = newNode;
newNode->m_value = p->m_value;
newNode->m_firstName = p->m_firstName;
newNode->m_lastName = p->m_lastName;
n = newNode;
p = p->next;
}
listSize = other.listSize;
}
PeopleList& PeopleList::operator=(const PeopleList& other) {
PeopleList copy(other);
swap(copy);
return *this;
}
PeopleList::~PeopleList() {
Node *p = head->next;
while (p!=head) {
head->next = p->next;
head->next->previous = head;
Node *n = p;
p = p->next;
delete n;
}
delete head;
}
PeopleList::Node* PeopleList::PosOfFirstName(const std::string &firstname) { //return the place ready to insert. Need to insert the next node.
Node *p = head;
for (p=p->next; p!=head; p=p->next) {
if (p->m_firstName < firstname && p->next->m_firstName > firstname) {
return p;
}
}
if (firstname > p->next->m_firstName)
return p->previous;
else
return p;
}
PeopleList::Node* PeopleList::PosOfLastName(const std::string &lastname) {
Node *p = head;
for (p=p->next; p!=head; p=p->next) {
if (p->m_lastName < lastname && p->next->m_lastName > lastname) {
return p;
}
}
if (lastname > p->next->m_lastName)
return p -> previous;
else
return p;
}
int PeopleList::size() const {
return listSize;
}
bool PeopleList::add(const std::string& firstName, const std::string& lastName, const InfoType& value) {
bool lastNameSame = false;
Node *p = head->next;
for (; p!=head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName){
return false;
}
else if (p->m_lastName == lastName) {
lastNameSame = true;
break;
}
else
;
}
Node *newNode = new Node;
newNode->m_firstName = firstName;
newNode->m_lastName = lastName;
newNode->m_value = value;
if (lastNameSame == true) {
p = PosOfFirstName(firstName);
newNode->next = p->next;
newNode->previous = p;
p->next->previous = newNode;
p->next = newNode;
listSize++;
return true;
}
else {
p = PosOfLastName(lastName);
newNode->next = p->next;
newNode->previous = p;
p->next->previous = newNode;
p->next = newNode;
listSize++;
return true;
}
}
bool PeopleList::get(int i, std::string& firstName, std::string& lastName, InfoType& value) const {
int counter = 0;
for (Node *p = head->next; p != head; p=p->next) {
if (counter == i) {
firstName = p->m_firstName;
lastName = p-> m_lastName;
value = p->m_value;
return true;
}
else
counter++;
}
return false;
}
bool PeopleList::change(const std::string& firstName, const std::string& lastName, const InfoType& value) {
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName) {
p->m_value = value;
return true;
}
}
return false;
}
bool PeopleList::addOrChange(const std::string& firstName, const std::string& lastName, const InfoType& value) {
bool lastNameSame = false;
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName) {
p->m_value = value;
return true;
}
else if (p->m_lastName == lastName) {
lastNameSame = true;
break;
}
else
;
}
Node *newNode = new Node;
newNode->m_firstName = firstName;
newNode->m_lastName = lastName;
newNode->m_value = value;
if (lastNameSame == true) {
p = PosOfFirstName(firstName);
newNode->next = p->next;
newNode->previous = p;
p->next->previous = newNode;
p->next = newNode;
listSize++;
return true;
}
else {
p = PosOfLastName(lastName);
newNode->next = p->next;
newNode->previous = p;
p->next->previous = newNode;
p->next = newNode;
listSize++;
return true;
}
}
bool PeopleList::remove(const std::string& firstName, const std::string& lastName) {
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName) {
p->previous->next = p->next;
p->next->previous = p->previous;
delete p;
listSize--;
return true;
}
}
return false;
}
bool PeopleList::contains(const std::string& firstName, const std::string& lastName) const {
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName)
return true;
}
return false;
}
bool PeopleList::lookup(const std::string& firstName, const std::string& lastName, InfoType& value) const {
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName) {
value = p->m_value;
return true;
}
}
return false;
}
bool PeopleList::empty() const {
if (head->next == head && head->previous == head)
return true;
else
return false;
}
void PeopleList::swap(PeopleList& other) {
Node *temp = new Node;
temp->m_firstName = "";
temp->m_lastName = "";
temp->next = head->next;
head->next->previous = temp;
temp->previous = head->previous;
head->previous->next = temp;
head->next = other.head->next;
other.head->next->previous = head;
head->previous = other.head->previous;
other.head->previous->next = head;
other.head->next = temp->next;
temp->next->previous = other.head;
other.head->previous = temp->previous;
temp->previous->next = other.head;
std::swap(listSize, other.listSize);
}
bool combine(const PeopleList& m1, const PeopleList& m2, PeopleList& result) {
bool isEqualValue = true;
for (int j = 0; j < m1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType m1Value;
InfoType resultValue;
m1.get(j, firstName, lastName, m1Value);
if (result.lookup(firstName, lastName, resultValue) && resultValue == m1Value) {
;
}
else if (result.lookup(firstName, lastName, resultValue) && resultValue != m1Value) {
result.remove(firstName, lastName);
isEqualValue = false;
}
else {
result.add(firstName, lastName, m1Value);
}
}
for (int i = 0; i < m2.size();i++){
std::string firstName;
std::string lastName;
InfoType m2Value;
InfoType resultValue;
m2.get(i, firstName, lastName, m2Value);
if (result.lookup(firstName,lastName,resultValue) && resultValue == m2Value ) {
;
}
else if (result.lookup(firstName, lastName, resultValue) && resultValue != m2Value) {
result.remove(firstName, lastName);
isEqualValue = false;
}
else {
result.add(firstName, lastName, m2Value);
}
}
return isEqualValue;
}
void search(const std::string& fsearch, const std::string& lsearch, const PeopleList& p1, PeopleList& result) {
if (fsearch == "*" && lsearch == "*") {
for (int i = 0; i < p1.size(); i++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(i, firstName, lastName, p1Value);
result.add(firstName, lastName, p1Value);
}
}
else if (fsearch == "*" && lsearch != "*") {
for (int j = 0; j < p1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(j, firstName, lastName, p1Value);
if (lastName==lsearch) {
result.add(firstName, lastName, p1Value);
}
}
}
else if (fsearch != "*" && lsearch == "*") {
for (int j = 0; j < p1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(j, firstName, lastName, p1Value);
if (firstName == fsearch) {
result.add(firstName, lastName, p1Value);
}
}
}
else {
if (p1.contains(fsearch,lsearch)) {
InfoType p1Value;
p1.lookup(fsearch, lsearch, p1Value);
result.add(fsearch, lsearch, p1Value);
}
else{
return;
}
}
}
------------------------------------------------------
PeopleList.h
-----------------------------
#ifndef PeopleList_h
#define PeopleList_h
#include <string>
typedef int InfoType;
class PeopleList
{
private:
struct Node {
InfoType m_value;
std::string m_firstName;
std::string m_lastName;
Node *previous;
Node *next;
};
Node *head;
int listSize;
Node *PosOfLastName(const std::string &lastName);
Node *PosOfFirstName(const std::string &firstName);
public:
PeopleList(); // Create an empty In (i.e., one with no InfoType values)
PeopleList(const PeopleList &other); // Copy constructor
PeopleList& operator=(const PeopleList& other); //assignment operator overloading
~PeopleList(); // When a PeopleList is destroyed, all dynamic memory must be deallocated.
bool empty() const; // Return true if the list is empty, otherwise false.
int size() const; // Return the number of elements in the linked list.
bool add(const std::string& firstName, const std::string& lastName, const InfoType& value);
// If the full name (both the first and last name) is not equal to any full name currently
// in the list then add it and return true. Elements should be added according to
// their last name. Elements with the same last name should be added according to
// their first names. Otherwise, make no change to the list and return false
// (indicating that the name is already in the list).
bool change(const std::string& firstName, const std::string& lastName, const InfoType& value);
// If the full name is equal to a full name currently in the list, then make that full
// name no longer map to the value it currently maps to, but instead map to
// the value of the third parameter; return true in this case.
// Otherwise, make no change to the list and return false.
bool addOrChange(const std::string& firstName, const std::string& lastName, const InfoType& value);
// If full name is equal to a name currently in the list, then make that full name no
// longer map to the value it currently maps to, but instead map to
// the value of the third parameter; return true in this case.
// If the full name is not equal to any full name currently in the list then add it
// and return true. In fact this function always returns true.
bool remove(const std::string& firstName, const std::string& lastName);
// If the full name is equal to a full name currently in the list, remove the
// full name and value from the list and return true. Otherwise, make
// no change to the list and return false.
bool contains(const std::string& firstName, const std::string& lastName) const;
// Return true if the full name is equal to a full name currently in the list, otherwise
// false.
bool lookup(const std::string& firstName, const std::string& lastName, InfoType& value) const;
// If the full name is equal to a full name currently in the list, set value to the
// value in the list that that full name maps to, and return true. Otherwise,
// make no change to the value parameter of this function and return
// false.
bool get(int i, std::string& firstName, std::string& lastName, InfoType& value) const;
// If 0 <= i < size(), copy into firstName, lastName and value parameters the
// corresponding information of the element at position i in the list and return
// true. Otherwise, leave the parameters unchanged and return false.
// (See below for details about this function.)
void swap(PeopleList& other);
// Exchange the contents of this list with the other one.
};
bool combine(const PeopleList& m1, const PeopleList& m2, PeopleList& result);
void search(const std::string& fsearch, const std::string& lsearch,
const PeopleList& p1, PeopleList& result);
#endif /* PeopleList_h */
Explanation / Answer
The needed values with the comments pasted for each and every step is as follows:
#include <iostream>
#include <string>
#include <assert.h>
#include "PeopleList.h"
using namespace std;
/*
int main() {
PeopleList result;
PeopleList m1;
PeopleList m2;
result.add("Allen", "Amy", 1000); // Adds entries to the list
m1.add("Skyler", "White", 45);
m1.add("James", "McGill", 49);
m1.add("Charles", "McGill", 58);
m2.add("Walter", "White", 52);
m2.add("Jesse", "Pinkman", 27);
cout << combine(m1, m2, result) << endl; //Combines m1 and m2
for (int n = 0; n < result.size(); n++) // Printing out the values till the last element is reached or visited
{
string f;
string l;
int v;
result.get(n, f, l, v);
cout << f << " " << l << " " << v << endl; // printing statement
}
}
*/
/*int main() {
PeopleList p1;
PeopleList result;
p1.add("Gustavo", "Fring", 57);
p1.add("Skyler", "White", 45);
p1.add("Walter", "White", 45);
p1.add("Jane", "Doe", 35);
p1.add("Marie", "Schrader", 37);
p1.add("Jane", "Margolis", 27);
search("*", "*", p1, result);
for (int n = 0; n < result.size(); n++)
{
string f;
string l;
int v;
result.get(n, f, l, v);
cout << f << " " << l << " " << v << endl;
}
}*/
#include "PeopleList.h"
#include <string>
#include <iostream>
#include <cassert>
using namespace std;
void test()
{
PeopleList m;
assert(m.add("Fred", "Mertz", 52)); // asserting for the following condition. This will terminate the program with this mssg
assert(m.add("Ethel", "Mertz", 49)); // asserting for the following condition. This will terminate the program with this mssg
assert(m.size() == 2);
string first, last;
int a;
assert(m.get(0, first, last, a) && a == 49); // asserting for the following condition. This will terminate the program with this mssg
string s1;
assert(m.get(1, first, last, a) &&
(first == "Fred" && a == 52)); // asserting for the following condition. This will terminate the program with this mssg
}
int main() // Tester Main Function
{
test(); //testing application
cout << "Passed all tests" << endl;
}
------------------------------------------------------
PeopleList.cpp
-----------------------------
#include <iostream>
#include <string>
#include "PeopleList.h" //Including header files
PeopleList::PeopleList()
:listSize(0) { // Calling Constructor from header file
Node *newNode = new Node; // Operations on the Node and declaring an object to node
head = newNode;
head -> next = head;
head -> previous = head;
head -> m_firstName = "";
head -> m_lastName = "";
}
PeopleList::PeopleList(const PeopleList &other) { // Constructor Overloading
Node *newNode = new Node;
head = newNode;
head->next = head;
head->previous = head;
head->m_firstName = "";
head->m_lastName = "";
Node *p = other.head->next;
Node *n = head;
while (p!=other.head) { // Checking for head and incrementing to next value using variables
Node *newNode = new Node;
newNode->next = head;
newNode->previous = n;
n->next = newNode;
head->previous = newNode;
newNode->m_value = p->m_value;
newNode->m_firstName = p->m_firstName;
newNode->m_lastName = p->m_lastName;
n = newNode;
p = p->next;
}
listSize = other.listSize;
}
PeopleList& PeopleList::operator=(const PeopleList& other) { // Constructor Overloading
PeopleList copy(other); // Copying values
swap(copy); // Swapping Values
return *this;
}
PeopleList::~PeopleList() { // Constructor Calling
Node *p = head->next;
while (p!=head) { // Check for head if it is full it will delete the element
head->next = p->next;
head->next->previous = head;
Node *n = p;
p = p->next;
delete n;
}
delete head;
}
PeopleList::Node* PeopleList::PosOfFirstName(const std::string &firstname) {
//return the place ready to insert. Need to insert the next node.
Node *p = head;
for (p=p->next; p!=head; p=p->next) {
if (p->m_firstName < firstname && p->next->m_firstName > firstname) {
return p;
}
}
//Checking condition for the firstname and moving with the forward point
if (firstname > p->next->m_firstName)
return p->previous;
else
return p;
}
PeopleList::Node* PeopleList::PosOfLastName(const std::string &lastname) {
Node *p = head;
// return the place ready to insert. Need to insert the next node.
for (p=p->next; p!=head; p=p->next) {
if (p->m_lastName < lastname && p->next->m_lastName > lastname) {
return p;
}
}
if (lastname > p->next->m_lastName)
return p -> previous;
else
return p;
}
// size of the list
int PeopleList::size() const {
return listSize;
}
// It checks the list to add new element
bool PeopleList::add(const std::string& firstName, const std::string& lastName, const InfoType& value) {
bool lastNameSame = false;
Node *p = head->next;
for (; p!=head; p = p->next) { // checking the list for adding elements
if (p->m_firstName == firstName && p->m_lastName == lastName){ // return the place ready to insert. Need to insert the next node.
return false;
}
else if (p->m_lastName == lastName) { //else there is place to add elements
lastNameSame = true;
break;
}
else
;
}
Node *newNode = new Node;
newNode->m_firstName = firstName; // referencing the values of firstName
newNode->m_lastName = lastName; // referencing the values of lastName
newNode->m_value = value; // referencing the values of value
if (lastNameSame == true) { // checking if the lastName is same
p = PosOfFirstName(firstName);
newNode->next = p->next;
newNode->previous = p;
p->next->previous = newNode;
p->next = newNode;
listSize++;
return true;
}
else {
p = PosOfLastName(lastName); // it will get the position of the lastName
newNode->next = p->next;
newNode->previous = p;
p->next->previous = newNode;
p->next = newNode;
listSize++; //Increment the node
return true;
}
}
// Accessor & Mutator methods for inserting & retrieving values
bool PeopleList::get(int i, std::string& firstName, std::string& lastName, InfoType& value) const {
int counter = 0;
for (Node *p = head->next; p != head; p=p->next) {
if (counter == i) {
firstName = p->m_firstName; // inserting firstName
lastName = p-> m_lastName; //inserting lastName
value = p->m_value; // inserting value
return true;
}
else
counter++;
}
return false;
}
// Changing the list values at specific position
bool PeopleList::change(const std::string& firstName, const std::string& lastName, const InfoType& value) {
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName) {
p->m_value = value; //Values of the elements
return true;
}
}
return false;
}
//This will check whether the user provided value is already there in the list if there it will
// change the value if not then it will add the value as new to list
bool PeopleList::addOrChange(const std::string& firstName, const std::string& lastName, const InfoType& value) {
bool lastNameSame = false;
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName) {// return the place ready to insert. Need to insert the next node.
p->m_value = value;
return true;
}
else if (p->m_lastName == lastName) {// return the place ready to insert. Need to insert the next node.
lastNameSame = true;
break;
}
else
;
}
Node *newNode = new Node;
newNode->m_firstName = firstName;
newNode->m_lastName = lastName;
newNode->m_value = value;
if (lastNameSame == true) { //lastName is true
p = PosOfFirstName(firstName);
newNode->next = p->next;
newNode->previous = p;
p->next->previous = newNode;
p->next = newNode;
listSize++;
return true;
}
else {
p = PosOfLastName(lastName);
newNode->next = p->next;
newNode->previous = p;
p->next->previous = newNode;
p->next = newNode;
listSize++;
return true;
}
}
//To remove the values in the list
bool PeopleList::remove(const std::string& firstName, const std::string& lastName) {
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName) {// return the place ready to insert. Need to insert the next node.
p->previous->next = p->next;
p->next->previous = p->previous;
delete p; //delete the value
listSize--;
return true;
}
}
return false;
}
//checks whether the list contains the values or not
bool PeopleList::contains(const std::string& firstName, const std::string& lastName) const {
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName)// return the place ready to insert. Need to insert the next node.
return true;
}
return false;
}
//finding the entered element by the user at a particular position
bool PeopleList::lookup(const std::string& firstName, const std::string& lastName, InfoType& value) const {
Node *p = head->next;
for (; p != head; p = p->next) {
if (p->m_firstName == firstName && p->m_lastName == lastName) // return the place ready to insert. Need to insert the next node.
value = p->m_value;
return true;
}
}
return false;
}
//To check whether the list is empty or not
bool PeopleList::empty() const {
if (head->next == head && head->previous == head)
return true;
else
return false;
}
//Swapping the values of FirstName and lastName
void PeopleList::swap(PeopleList& other) {
Node *temp = new Node;
temp->m_firstName = "";
temp->m_lastName = "";
temp->next = head->next; // Swapping logic inserted in the program
head->next->previous = temp;
temp->previous = head->previous;
head->previous->next = temp;
head->next = other.head->next;
other.head->next->previous = head;
head->previous = other.head->previous;
other.head->previous->next = head;
other.head->next = temp->next;
temp->next->previous = other.head;
other.head->previous = temp->previous;
temp->previous->next = other.head;
std::swap(listSize, other.listSize);
}
//Combining two different lists m1 and m2
bool combine(const PeopleList& m1, const PeopleList& m2, PeopleList& result) {
bool isEqualValue = true;
for (int j = 0; j < m1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType m1Value;
InfoType resultValue;
m1.get(j, firstName, lastName, m1Value);
if (result.lookup(firstName, lastName, resultValue) && resultValue == m1Value) {
;
}
else if (result.lookup(firstName, lastName, resultValue) && resultValue != m1Value) {
result.remove(firstName, lastName);
isEqualValue = false;
}
else {
result.add(firstName, lastName, m1Value);
}
}
for (int i = 0; i < m2.size();i++){
std::string firstName;
std::string lastName;
InfoType m2Value;
InfoType resultValue;
m2.get(i, firstName, lastName, m2Value);
if (result.lookup(firstName,lastName,resultValue) && resultValue == m2Value ) {
;
}
else if (result.lookup(firstName, lastName, resultValue) && resultValue != m2Value) {
result.remove(firstName, lastName);
isEqualValue = false;
}
else {
result.add(firstName, lastName, m2Value); //Adding values
}
}
return isEqualValue;
}
//Searching for the entered element
void search(const std::string& fsearch, const std::string& lsearch, const PeopleList& p1, PeopleList& result) {
if (fsearch == "*" && lsearch == "*") {
for (int i = 0; i < p1.size(); i++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(i, firstName, lastName, p1Value); // getting values
result.add(firstName, lastName, p1Value); // returning values to the user of found value
}
}
else if (fsearch == "*" && lsearch != "*") {
for (int j = 0; j < p1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(j, firstName, lastName, p1Value); // getting values
if (lastName==lsearch) {
result.add(firstName, lastName, p1Value); // returning values to the user of found value
}
}
}
else if (fsearch != "*" && lsearch == "*") {
for (int j = 0; j < p1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(j, firstName, lastName, p1Value); //getting values
if (firstName == fsearch) {
result.add(firstName, lastName, p1Value); // returning values to the user of found value
}
}
}
else {
if (p1.contains(fsearch,lsearch)) {
InfoType p1Value;
p1.lookup(fsearch, lsearch, p1Value); // getting values
result.add(fsearch, lsearch, p1Value); // returning values to the user of found value
}
else{
return;
}
}
}
------------------------------------------------------
PeopleList.h
-----------------------------
#ifndef PeopleList_h
#define PeopleList_h
#include <string>
typedef int InfoType;
class PeopleList
{
private:
struct Node {
InfoType m_value;
std::string m_firstName;
std::string m_lastName;
Node *previous;
Node *next;
};
Node *head;
int listSize;
Node *PosOfLastName(const std::string &lastName);
Node *PosOfFirstName(const std::string &firstName);
public:
PeopleList(); // Create an empty In (i.e., one with no InfoType values)
PeopleList(const PeopleList &other); // Copy constructor
PeopleList& operator=(const PeopleList& other); //assignment operator overloading
~PeopleList(); // When a PeopleList is destroyed, all dynamic memory must be deallocated.
bool empty() const; // Return true if the list is empty, otherwise false.
int size() const; // Return the number of elements in the linked list.
bool add(const std::string& firstName, const std::string& lastName, const InfoType& value);
// If the full name (both the first and last name) is not equal to any full name currently
// in the list then add it and return true. Elements should be added according to
// their last name. Elements with the same last name should be added according to
// their first names. Otherwise, make no change to the list and return false
// (indicating that the name is already in the list).
bool change(const std::string& firstName, const std::string& lastName, const InfoType& value);
// If the full name is equal to a full name currently in the list, then make that full
// name no longer map to the value it currently maps to, but instead map to
// the value of the third parameter; return true in this case.
// Otherwise, make no change to the list and return false.
bool addOrChange(const std::string& firstName, const std::string& lastName, const InfoType& value);
// If full name is equal to a name currently in the list, then make that full name no
// longer map to the value it currently maps to, but instead map to
// the value of the third parameter; return true in this case.
// If the full name is not equal to any full name currently in the list then add it
// and return true. In fact this function always returns true.
bool remove(const std::string& firstName, const std::string& lastName);
// If the full name is equal to a full name currently in the list, remove the
// full name and value from the list and return true. Otherwise, make
// no change to the list and return false.
bool contains(const std::string& firstName, const std::string& lastName) const;
// Return true if the full name is equal to a full name currently in the list, otherwise
// false.
bool lookup(const std::string& firstName, const std::string& lastName, InfoType& value) const;
// If the full name is equal to a full name currently in the list, set value to the
// value in the list that that full name maps to, and return true. Otherwise,
// make no change to the value parameter of this function and return
// false.
bool get(int i, std::string& firstName, std::string& lastName, InfoType& value) const;
// If 0 <= i < size(), copy into firstName, lastName and value parameters the
// corresponding information of the element at position i in the list and return
// true. Otherwise, leave the parameters unchanged and return false.
// (See below for details about this function.)
void swap(PeopleList& other);
// Exchange the contents of this list with the other one.
};
bool combine(const PeopleList& m1, const PeopleList& m2, PeopleList& result);
void search(const std::string& fsearch, const std::string& lsearch,
const PeopleList& p1, PeopleList& result);
#endif /* PeopleList_h */
Please rate the answer if it helped......Thankyou
Hope it helps....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.