Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ In this assignment, you create a singly linked list consisting of two classe

ID: 3739825 • Letter: C

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

}

Explanation / Answer

/* insertValue(int) is not specified in the question */


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(){
         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() << " ";
         }
         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){
         p = listHead;
         while(p != NULL){
             if (p->getValue == a){
                return true;
             }
         }
         return false;
      }
     
}