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

C++ Complete the implementation of the appendNode function for the following lin

ID: 3606863 • Letter: C

Question

C++ Complete the implementation of the appendNode function for the following linked list class:

class ListNode {
   public:
      int value;           
      ListNode *next;
      ListNode (int nodeValue) {
         value = nodeValue;
         next = NULL;
      }
};
class LinkedList
{
   private:
      ListNode *head;

   public:
     LinkedList() { head = NULL; }
     ~LinkedList();
      void appendNode(int); //add to the end of the linked list
      …

};

// YOUR (function) CODE starts HERE ...

Explanation / Answer

Please find my implementation.

Please let me know in case of any issue.

class ListNode{

public:

int value;

ListNode *next;

ListNode (int nodeValue) {

value = nodeValue;

next = NULL;

}

};

class LinkedList

{

private:

ListNode *head;

public:

LinkedList() {head = NULL; }

~LinkedList();

void appendNode(int); //add to the end of the linked list

...

}

// YOUR (function) CODE STARTS HERE......

void LinkedList::appendNode(int x){

ListNode *newNode = new ListNode(x);

if(head == NULL)

head = x;

else{

ListNode *curr = head;

while(curr->next != NULL)

curr = curr->next;

curr->next = newNode;

}

}