Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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::search(const Type& searchItem) const

{

bool found = false;

nodeType *current; //pointer to traverse the list

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::insert(const Type& newItem)

{

nodeType *current; //pointer to traverse the list

nodeType *trailCurrent; //pointer just before current

nodeType *newNode; //pointer to create a node

bool found;

newNode = new nodeType; //create the node

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::insertFirst(const Type& newItem)

{

insert(newItem);

}//end insertFirst

template

void orderedLinkedList::insertLast(const Type& newItem)

{

insert(newItem);

}//end insertLast

template

void orderedLinkedList::deleteNode(const Type& deleteItem)

{

nodeType *current; //pointer to traverse the list

nodeType *trailCurrent; //pointer just before current

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::print() const

{

bool found = false;

nodeType *current; //pointer to traverse the list

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 -------------------------

image text in transcribed image text in transcribed

linkedList.h #1fndef H-LinkedListType adefine H LinkedListType current = current-link nodeType first; //pointer to the first nodeType template cclass Type> IteratorcType> vold copylist(const 1inkedL1s //Definition of the node currentright.current); tesplate cclass Type> int 1inkedListTypecType>:length() const tesplate cclass Type> struct nodeType tesplate cclass Type bool linkedList TypecType>:isEmptyList) const tesplate cclass Type> bool 1inkedListIteratorcType>::operatorla nodeTypecType> link (const 1inkedListIteratorcType>& template Type linkedList Type: ront)const template linkedLstType:1inkedListType() //default assert(firstNULL) template return first->info; I/return the info of the linkedListiterator(nodeTypecType> *ptr); end front linkedListIteratorcType> operator+O LinkedL1stIteratorcType>& right) const; linkedLstTypec Type>&); Type 1inkedList TypecTypex::back) const bool operator! (const tesplate cclass Type void 1inkedList TypecType::destroyList) assert (lastNULL); linkedL1stIteratorcType>& right) const; void initlalizelisto bool isEmptyList() const; void print() const nodeType tep; //pointer to return last->info; /Ireturn the info of the nodeType current; //pointer to point deallocate the menory /ode in the 1inked while (first != NULL) //while there are template 1inkedListTypecType>::begin() nodes in the list void destroyList) Type front) const Type back) const; virtual bool search(const Type& searchItem) virtual void insertFirst(const Type& newiten) virtual void insertLast(const Type& newItem) virtual void deleteNcde(const Type l/set temp to the template LinkedListIterator:linkedListIterator() linkedListIterator temp(first); first-first-1ink; //advance first to delete temp; Ideallocate the nenory tesplate cclass Type last NULL; initze last to NULL; first linkedListIterator(nodeTypecType> *ptr) //been set to NULL by the while linkedListIterator tesp(NULL) deleteIte) -e; inkedListIterator begin); inkedListIterator end) linkedlistType linkedListType( const linkedList TypecTypex& template ::operator*) template kclass Type template void linkedList Type: :copyList void linkedList Type::initializelist) nodes, delete then tesplate cclass Type> destroyList); //if the 1ist has any nodeTypexType newNade; //pointer to create nodeTypeType current; //pointer to f (first NULL) ISf the list is nonenpty, template cclass Type> int count; I/varlable to store the nunber of vald linkedLstType "current; //pointer to destroyLEstO /set current so that it linkedList.h #1fndef H-LinkedListType adefine H LinkedListType current = current-link nodeType first; //pointer to the first nodeType template cclass Type> IteratorcType> vold copylist(const 1inkedL1s //Definition of the node currentright.current); tesplate cclass Type> int 1inkedListTypecType>:length() const tesplate cclass Type> struct nodeType tesplate cclass Type bool linkedList TypecType>:isEmptyList) const tesplate cclass Type> bool 1inkedListIteratorcType>::operatorla nodeTypecType> link (const 1inkedListIteratorcType>& template Type linkedList Type: ront)const template linkedLstType:1inkedListType() //default assert(firstNULL) template return first->info; I/return the info of the linkedListiterator(nodeTypecType> *ptr); end front linkedListIteratorcType> operator+O LinkedL1stIteratorcType>& right) const; linkedLstTypec Type>&); Type 1inkedList TypecTypex::back) const bool operator! (const tesplate cclass Type void 1inkedList TypecType::destroyList) assert (lastNULL); linkedL1stIteratorcType>& right) const; void initlalizelisto bool isEmptyList() const; void print() const nodeType tep; //pointer to return last->info; /Ireturn the info of the nodeType current; //pointer to point deallocate the menory /ode in the 1inked while (first != NULL) //while there are template 1inkedListTypecType>::begin() nodes in the list void destroyList) Type front) const Type back) const; virtual bool search(const Type& searchItem) virtual void insertFirst(const Type& newiten) virtual void insertLast(const Type& newItem) virtual void deleteNcde(const Type l/set temp to the template LinkedListIterator:linkedListIterator() linkedListIterator temp(first); first-first-1ink; //advance first to delete temp; Ideallocate the nenory tesplate cclass Type last NULL; initze last to NULL; first linkedListIterator(nodeTypecType> *ptr) //been set to NULL by the while linkedListIterator tesp(NULL) deleteIte) -e; inkedListIterator begin); inkedListIterator end) linkedlistType linkedListType( const linkedList TypecTypex& template ::operator*) template kclass Type template void linkedList Type: :copyList void linkedList Type::initializelist) nodes, delete then tesplate cclass Type> destroyList); //if the 1ist has any nodeTypexType newNade; //pointer to create nodeTypeType current; //pointer to f (first NULL) ISf the list is nonenpty, template cclass Type> int count; I/varlable to store the nunber of vald linkedLstType "current; //pointer to destroyLEstO /set current so that it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Optimization And Data Science Trends And Applications 5th Airoyoung Workshop And Airo Phd School 2021 Joint Event

Authors: Adriano Masone ,Veronica Dal Sasso ,Valentina Morandi

1st Edition

3030862887, 978-3030862886

More Books

Students also viewed these Databases questions

Question

Write short notes on Interviews.

Answered: 1 week ago

Question

Define induction and what are its objectives ?

Answered: 1 week ago