Question: I have a program which prompts a user for an integer (n) and then builds a linked list with numbers between 1 and n. For

I have a program which prompts a user for an integer (n) and then builds a linked list with numbers between 1 and n. For example, if the user enters 5, the program will build a linked list with 1 through 5, and display it as 5, 4, 3, 2, 1. I would like to also display every other node data in this program i.e. 5, 3, 1 for the above example. Given the following class for the nodes in a linked list: public class Node { ... public Node getNext() {...} // return next field public int getData() {...} // returns data } Assuming that the variable head points to (i.e. contains the address of) the first node of the linked list, write the statement(s) to print the data value in every other node of the linked list to the console. For example if the list has 5->4->3->2->1, the output should be 5 3 1 i.e. the numbers only with spaces in-between. If the list is empty 0 size), you code should not print out anything. You may declare additional variables and assume that the list may have any number of nodes including zero. If the list is empty, do not print anything, otherwise print the data separated by white spaces.

I have a program which prompts a user for an integer (n)

Sample console l/O execution sequence Enter list size: 5 List data: 5 432 1 Every other data: 5 3 1 Enter list size: e List data: Every other data

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!