Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions