Question
Extend the OrderedLinkedList Class, its superclass and the Person class to accomplish the following:You have to keep a linked list of persons sorted by their
Extend the OrderedLinkedList Class, its superclass and the Person class to accomplish the following:You have to keep a linked list of persons sorted by their age.Find and delete the node with the smallest info (in this case smallest age). Delete only the first occurrence by traversing the list only once. The signature of the function should be void FindDeleteSmallest().Find and delete all occurrences of a given info or value from the list. The signature of the function should be void FindDeleteAllOccurances(Type p) where this function should be able to accept objects of Type Person as well. As before, two Person objects are equal if their ages are same. include .cpp file and put in Bold the added funcitions and modifications made
---------------------------------------------------------------
//orderedLinkedList.h
#ifndef H_orderedListType
#define H_orderedListType
#include "linkedList.h"
using namespace std;
template
class orderedLinkedList: public linkedListType
{
public:
bool search(const Type& searchItem) const;
void insert(const Type& newItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
void print() const;
};
template
bool orderedLinkedList
{
bool found = false;
nodeType
current = first; //start the search at the first node
while (current != NULL && !found)
if (current->info >= searchItem)
found = true;
else
current = current->link;
if (found)
found = (current->info == searchItem); //test for equality
return found;
}//end search
template
void orderedLinkedList
{
nodeType
nodeType
nodeType
bool found;
newNode = new nodeType
newNode->info = newItem; //store newItem in the node
newNode->link = NULL; //set the link field of the node
//to NULL
if (first == NULL) //Case 1
{
first = newNode;
last = newNode;
count++;
}
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == first) //Case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //Case 3
{
trailCurrent->link = newNode;
newNode->link = current;
if (current == NULL)
last = newNode;
count++;
}
}//end else
}//end insert
template
void orderedLinkedList
{
insert(newItem);
}//end insertFirst
template
void orderedLinkedList
{
insert(newItem);
}//end insertLast
template
void orderedLinkedList
{
nodeType
nodeType
bool found;
if (first == NULL) //Case 1
cout
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == NULL) //Case 4
cout
else
if (current->info == deleteItem) //the item to be
//deleted is in the list
{
if (first == current) //Case 2
{
first = first->link;
if (first == NULL)
last = NULL;
delete current;
}
else //Case 3
{
trailCurrent->link = current->link;
if (current == last)
last = trailCurrent;
delete current;
}
count--;
}
else //Case 4
cout
}
}//end deleteNode
template
void orderedLinkedList
{
bool found = false;
nodeType
current = first; //start the search at the first node
while (current != NULL)
{
(current->info).print();
current = current->link;
}
return;
}//end search #endif ----------
//person.h
#ifndef H_Person
#define H_Person
#include
#include
#include "Person.h"
using namespace std;
class Person
{
public:
Person ();
Person (string lname, string fname, int age);
void setLname(string lname);
void setFname(string fname);
void setAge (int a);
string getFname();
string getLname();
const int getAge();
void print();
private:
string lastname;
string firstname;
int age;
};
Person::Person()
{
firstname = "";
lastname = "";
age = -1;
}
Person::Person(string lname, string fname, int age)
{
setFname(fname);
setLname(lname);
setAge(age);
}
void Person::setLname(string lname)
{
lastname = lname;
}
void Person::setFname(string fname)
{
firstname = fname;
}
void Person::setAge(int a)
{
age = a;
}
string Person::getFname()
{
return firstname;
}
string Person::getLname()
{
return lastname;
}
const int Person::getAge()
{
return age;
}
void Person::print()
{
cout
cout
cout
}
#endif -------------------------
linkedList.h #1fndef H-LinkedListType adefine H LinkedListType current = current-link nodeType
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started