Write a function double lastValue(ListNode *ptr) that returns the value stored in the last node of a nonempty list passed to it as parameter. The function should print an error message and terminate the program if the list passed to it is empty.

Short Answer

Expert verified
Question: Write a function that takes a non-empty linked list as input and returns the value stored in the last node of the list. Answer: To write a function that returns the value stored in the last node of a non-empty linked list, follow these steps: 1. Declare the function with the name 'double lastValue(ListNode *ptr)', where 'ptr' is a pointer to the head of the linked list. 2. Check if the list is empty. If it is, print an error message and terminate the program. 3. Create a loop that iterates through the linked list until the last node is reached. 4. Return the value of the last node, which should be stored in the 'current' pointer after iteration.

Step by step solution

01

1. Function Declaration

Declare the function with the name 'double lastValue(ListNode *ptr)', where 'ptr' is a pointer to the head of the linked list.
02

2. Check if the list is empty

Inside the function, first check if the list passed to the function is empty. If it is, print an error message and terminate the program. To do this, you can use the 'if' statement: ```cpp if(ptr == nullptr){ cout << "Error: The list is empty." << endl; exit(EXIT_FAILURE); } ```
03

3. Iterate through the list

Now, create a loop that iterates through the linked list until the last node is reached. You can use a 'while' loop for this purpose: ```cpp ListNode *current = ptr; while(current->next != nullptr){ current = current->next; } ```
04

4. Return the value of the last node

At this point, the 'current' pointer should be pointing to the last node in the linked list. Simply return the value stored in this node: ```cpp return current->value; ``` Putting everything together, the final function will look like this: ```cpp double lastValue(ListNode *ptr){ if(ptr == nullptr){ cout << "Error: The list is empty." << endl; exit(EXIT_FAILURE); } ListNode *current = ptr; while(current->next != nullptr){ current = current->next; } return current->value; } ```

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Write a function ListNode *removeFirst(ListNode *ptr) that is passed a linked list as parameter, and returns the tail of the list: that is, it removes the first node and returns what is left. The function should deal locate the storage of the removed node. The function returns NULL if the list passed to it is empty.

To indicate that a linked list is empty, you should set the pointer to its head to the value __________.

You are the leader of a programming team. You want the programmers on your team to attend a two-day workshop on linked lists, stacks and queues. One of the managers points out that the STL already supplies each one of those data structures, making it unnecessary for your programmers to write their own. Write the manager a short memo that justifies the need for the workshop.

Each of the following member functions for performing an operation on a linked list of type NumberList has at least one error. Explain what is wrong and how to fix it. A) NumberList: :printList ( ) while (head) cout \(<<\) head- \(>\) value head \(=\) head- \(>\) next B) NumberList: :printList ( ) ListNode \(\star \mathrm{p}=\) head; while \((\mathrm{p}->\text { next })\) cout \(<\) value \(p=p->n e x t\) C) NumberList: :printList ( ) Listrode \(* p=\) head while \((p)\) cout \(<\) value \(p++;\) D) NumberList: : NumberList ( ) ListNode \(*\) nodePtr, *nextNode; nodePtr = head; while (nodePtr != NULL) nextNode \(=\) nodePtr-next nodePtr- \(>\) next \(=\) NULL \(;\) nodePtr \(=\) nextNode;

Write a function void printSecond(ListNode *ptr) that prints the value stored in the second node of a list passed to it as parameter. The function should print an error message and terminate the program if the list passed to it has less than two nodes.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free