*************100% will give positive rating*********************** In this assig
ID: 3712196 • Letter: #
Question
*************100% will give positive rating***********************
In this assignment, you will extend the List class from your previous assignment so that all integer values in the list are in sorted ascending order. This new class will be called SortedList and is to be implemented as a subclass of List.
You will also implement a class SetList that uses SortedList as a “backing store” for a mathematical set of integers; that is, SetList will hold a collection of integers, all of which are unique members of the set. You will demonstrate that your implementations work correctly by invoking methods of SortedList and SetList from a main routine.
SortedList Class
The SortedList class is to be implemented as defined in the following header file.
class SortedList: public List {
public:
SortedList();
void appendValue (int);
void insertValue (int);
virtual ~SortedList();
};
Constructors and Destructors
SortedList(): a zero argument constructor for List
virtual ~SortedList(): deletes appropriately;
Methods
void appendValue(int): overrides the inherited appendValue to prohibit the appending of any value into the list. Essentially, only insertValue can be called to add values to the list.
void insertValue(int): overrides the inherited method insertValue to insert elements in sorted order only.
SetList Class
The SetList class is to be implemented as defined in the following header file.
SetList {
:
*sortedList;
:
SetList();
addValue (int);
containsValue (int);
isEmpty();
print();
removeValue(int);
~SetList();
Constructors and Destructors
SetList(): zero argument constructor.
virtual ~SetList(): deletes sortedList;
Attributes
SortedList *sortedList: contains an instance of SortedList that is used as a “backing store”. Methods for SetList should be implemented by calling methods of SortedList.
Methods
int addValue(): adds an integer value to the set if not already present.
bool containsValue(int): returns true if the parameter value is a member of the list, otherwise false.
bool isEmpty(): returns true if the list contains no elements (ListNodes), otherwise false;
void print(): prints the list of nodes in sorted order.
void removeValue(int): removes the indicated parameter value from the list, if present.
Main Routine
Define your main routine thus:
int main () {
SortedList *sortedList = new SortedList ();
sortedList->insertValue (22);
sortedList->insertValue (1);
sortedList->insertValue (-99);
sortedList->insertValue (100);
sortedList->insertValue (0);
sortedList->insertValue (1);
sortedList->print();
std::cout << " Removing value (22)" << std::endl;
sortedList->removeValue(22);
sortedList->print();
std::cout << " Removing value (1)" << std::endl;
sortedList->removeValue(1);
sortedList->print();
delete sortedList;
SetList *setList = new SetList();
setList->addValue(22);
setList->addValue(1);
setList->addValue(-99);
setList->addValue(0);
setList->addValue(1);
std::cout << " Values in new SetList" << std::endl;
setList->print();
delete setList;
return 0;
}
**************************BELOW IS WHAT MY PREVIOUS ASSIGNMENT LOOKED LIKE*********************************
******************************************************************** ListNode.h
#ifndef LIST_H_
#define LIST_H_
#include "ListNode.h"
class List {
private:
ListNode *listHead = nullptr;
protected:
ListNode *getListHead();
void setListHead (ListNode *);
public:
List();
void appendValue(int);
bool containsValue (int);
void insertValue (int);
bool isEmpty();
void removeValue(int);
void print();
virtual ~List();
};
#endif
************************************************************** List.h
#ifndef LIST_H_
#define LIST_H_
#include "ListNode.h"
class List {
private:
ListNode *listHead = nullptr;
protected:
ListNode *getListHead();
void setListHead (ListNode *);
public:
List();
void appendValue(int);
bool containsValue (int);
void insertValue (int);
bool isEmpty();
void removeValue(int);
void print();
virtual ~List();
};
#endif
*************************************************** ListNode.cpp
#include "ListNode.h"
ListNode::ListNode(int value) {
this->nodeValue = value;
}
ListNode * ListNode::getNext() {
return next;
}
void ListNode::setNext (ListNode *next) {
this->next = next;
}
int ListNode::getValue() {
return this->nodeValue;
}
void ListNode::putValue (int value) {
nodeValue = value;
}
ListNode::~ListNode() {
// TODO Auto-generated destructor stub
}
********************************************************************** Main.cpp
#include<iostream>
using namespace std;
class ListNode{
private:
int value;
ListNode *next;
public:
ListNode(){
next = NULL;
value = 0;
}
ListNode(int a){
next = NULL;
value = a;
}
ListNode *getNext(){
return next;
}
void setNext(ListNode *a){
next = a;
}
int getValue(){
return value;
}
void putValue(int a){
value = a;
}
~ListNode(){
delete next;
}
};
class List{
private:
ListNode *listHead = NULL;
protected:
ListNode *getListHead(){
return listHead;
}
void setListHead(ListNode *a){
listHead = a;
}
public:
List(){
listHead = NULL;
}
~List(){
ListNode *p = listHead;
while(listHead != NULL){
p = listHead;
delete p;
listHead = listHead->getNext();
}
}
bool isEmpty(){
if (listHead == NULL)
return true;
else
return false;
}
void removeValue(int a){
ListNode *p = listHead;
if (listHead->getValue() == a){
listHead = listHead->getNext();
delete p;
return;
}
else {
ListNode *q = p;
while (p != NULL && p->getValue() != a){
q = p;
p = p->getNext();
}
if (p == NULL){
cout << "Not found ";
}
else {
q->setNext(p->getNext());
delete p;
}
}
}
void print(){
ListNode *p = listHead;
while (p != NULL){
cout << p->getValue() << " ";
p = p->getNext();
}
cout << endl;
}
virtual void appendValue(int a){
ListNode *n = new ListNode(a);
if (listHead == NULL){
listHead = n;
return;
}
else {
ListNode *q = listHead;
while (q->getNext() != NULL)
q = q->getNext();
q->setNext(n);
}
}
bool containsValue(int a){
ListNode *p = listHead;
while(p != NULL){
if (p->getValue() == a){
return true;
}
}
return false;
}
};
int main () {
List *list = new List();
std::cout << "Appending some values to my list " << std::endl;
list->appendValue(1);
list->appendValue(2);
list->appendValue(3);
list->appendValue(4);
list->appendValue(5);
list->appendValue(6);
list->appendValue(7);
list->print();
//std::cout << " Inserting a value into my list " << std::endl;
//list->insertValue(99);
list->print();
bool result = list->containsValue (7);
std::cout << " The value 7 is contained in mylist: " << result <<
std::endl;
std::cout << " Removing a value from my list (2)" << std::endl;
list->removeValue(2);
list->print();
std::cout << " Removing a value from my list (99)" << std::endl;
list->removeValue(99);
list->print();
std::cout << " Removing a value from my list (1)" << std::endl;
list->removeValue(1);
list->print();
delete list;
}
Explanation / Answer
Chegg doesn't have inbuilt support for indentation so it was tedious to read your code. As far as I have understood your program I have done the implementation of both classes SortedList and SetList. If there is any confusion related to the code drop a comment. I have used insertion sort for inserting into the sorted list and almost all the functions are inherited so it didn't take much work on implementing other functions.
class SortedList: public List {
public:
SortedList();
void appendValue (int a) {
insertValue(a);
}
void insertValue (int value) {
ListNode Node* new_node;
ListNode Node* q;
new_node->putValue(value);
new_node->setNext(NULL);
if (*listHead == nullptr || (*listHead)->getValue() >= new_node->getValue()) {
listHead->setNext(*listHead);
*listHead->putValue(value);
}
else {
q = *listHead;
while (q->getNext() != NULL &&
q->getNext()->getValue() < value) {
q = q->getNext();
}
new_node->setNext(q->getNext());
q->setNext(new_node);
}
}
virtual ~SortedList() {
delete ListNode;
}
};
class SetList {
private:
SortedList *sortedList;
public:
SetList();
void addValue (int data) {
if(!containsValue(a)) {
appendValue(a);
}
}
bool containsValue(int data) {
return sortedList->containsValue(data);
}
bool isEmpty() {
if(sortedList->getListHead() == NULL) {
return true;
} else {
return false;
}
}
void print() {
sortedList->print();
}
void removeValue(int a) {
sortedList->removeValue(a);
}
~SetList() {
delete sortedList;
}
};
int main () {
SortedList *sortedList = new SortedList ();
sortedList->insertValue (22);
sortedList->insertValue (1);
sortedList->insertValue (-99);
sortedList->insertValue (100);
sortedList->insertValue (0);
sortedList->insertValue (1);
sortedList->print();
std::cout << " Removing value (22)" << std::endl;
sortedList->removeValue(22);
sortedList->print();
std::cout << " Removing value (1)" << std::endl;
sortedList->removeValue(1);
sortedList->print();
delete sortedList;
SetList *setList = new SetList();
setList->addValue(22);
setList->addValue(1);
setList->addValue(-99);
setList->addValue(0);
setList->addValue(1);
std::cout << " Values in new SetList" << std::endl;
setList->print();
delete setList;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.