Complete the implementation of the appendNode function for t

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

Solution

Please find my implementation.

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;

}

}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site