(15 pts) Write a deletescdeAtEndt) member fupction for the class ii.st (dynamica
ID: 3732557 • Letter: #
Question
(15 pts) Write a deletescdeAtEndt) member fupction for the class ii.st (dynamically singly tinked) that does not accept any arguments and returns a copy of the integer data in the Node deleted. This function should delete the last Node in the list. Given: a class Node with the following private data members Node "mpNext; int mData; and a class List with the following private data mermber: ListNode mpHead; points to the front of the list and the class List is a friend of class Xode. The friendship allows the List class to access Node private data members directly. // precondition: list must not be emptylExplanation / Answer
int List::deleteNodeAtEnd() {
Node *temp = mpHead;
if(mpHead == NULL) {
return -1;
}
if(mpHead->mpNext == NULL) {
mpHead = NULL;
return temp->mData;
} else {
while(temp->mpNext->mpNext != NULL) {
temp = temp->mpNext;
}
int ret = temp->mpNext->mData;
temp->mpNext = NULL;
return ret;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.