Question: C++ In this assignment, you create a singly linked list consisting of two classes: List and ListNode. List will manage a collection of ListNodes that

C++

In this assignment, you create a singly linked list consisting of two classes: List and ListNode. List will manage a collection of ListNodes that hold integer values. You will demonstrate that your linked list works correctly by invoking methods of List from a main routine.

List Class

The List class is to be implemented as defined in the following header file.

class List {

private:

ListNode *listHead = nullptr;

protected:

ListNode *getListHead();

void setListHead (ListNode *);

public:

List();

virtual void appendValue(int);

bool containsValue (int);

virtual void insertValue(int);

bool isEmpty();

void removeValue(int);

void print();

virtual ~List();

};

Constructors and Destructors

List(): a zero argument constructor for List

virtual ~List(): deletes all members of the list and itself;

Attributes

ListNode *listHead: points to the head (first element) in the list. This attribute should be initialized to null at List creation when there are no elements in the list. This attribute should also be null when all elements have been removed from the list.

Methods

ListNode *getListHead(): a getter for the listHead attribute, returns the current value of listHead.

void setListHead (ListNode *): a setter for the listHead attribute, sets the value of listHead.

void appendValue(int): add the integer parameter value to the end of the list.

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 removeValue(int): removes the indicated parameter value from the list, if present.

void print(): prints the list of nodes in order in which they are linked.

ListNode Class

The ListNode class is to be implemented as defined in the following header file.

ListNode {

:

*next = nullptr;

nodeValue = 0;

:

ListNode (int);

*getNext();

setNext (ListNode*);

getValue();

putValue (int);

~ListNode();

Constructors and Destructors

ListNode(int): constructor takes a single argument, the integer value.

virtual ~List(): deletes itself;

Attributes

ListNode *next: points to the next element in the list. If there is not next element (this node is the last in the list), the next pointer is set to nullptr.

int nodeValue: an integer that is the value for the node.

Methods

ListNode *getNext(): a getter for the next attribute, returns the address of the next element in the list or null.

void setNext (ListNode *): a setter for the next attribute, sets the value of next attribute to the next element in the list or null.

int getValue(): a getter for the value attribute,returns the integer value stored in this ListNode.

void setValue(int): a setter for the value attribute, returns the integer value stored in this ListNode.

Main Routine

Define your main routine thus:

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();

}

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 Databases Questions!