Question: We want to write a program named LinkedList2 to create a linked list of integers. It is assumed that the linked lists of integers consist

We want to write a program named LinkedList2 to create a linked list of integers. It is assumed that the linked lists of integers consist of objects belonging to the class Node. To create PLEASE a linked list, one must create nodes one by one and link them together. In this exercise, we want to create nodes containing copies of the elements in the original list and construct an inverted list of them. We can loop through the original list, examine each element, create a new node containing a copy of that element, and bind this new node to the inverted list we are constructing. We just need to make sure that the nodes in the new list are in the order we want.

We want to display the elements of the linked list of integers, find the number of zeros contained in this list using two ways, recursive and non-recursive. The LinkedList2 class therefore contains a nested static Node class, and shells for each of the methods. This implementation is provided herewith. You must complete it by implementing the body of the following methods (in the places indicated):

- static Node reverse( Node obj ): Returns a new list containing the same elements as the list, but in reverse order. The original list should not be modified.

- static void display(Node first): Displays the elements of the list whose first element is the parameter, first. They are printed on a single line, separated by spaces.

- static int count( Node head ): Counts the number of zeros that occur in a linked list of integers whose first element is its parameter (head); and returns that number.

- static int countRecursive( Node head ): same function as the count method but uses recursion. The class also contains the main main method which is provided to you and should NOT be modified. It allows to build a random linked list of 10 integers, displays this list, the inverted list and the number of zeros in this list obtained in both ways.

/************Example output of the LinkedList2.java program*****/

The list: 52 89 72 66 14 69 82 0 60

The reversed list: 60 0 82 69 14 66 72 89 52

The number of zeros in the list: 1

The number of zeros in the list, using recursion: 1

public class LinkedList2 { /** * Objects of type Node are linked together into linked lists. */ private static class Node { int value; // value of an item in the list. Node next; // Pointer to the next node in the list. } /** * Return a new list containing the same items as the list, * but in the reverse order. */ static Node reverse( Node obj ) { Node reverse = null; // reverse will be the reversed list. Node p = obj; // going through the nodes of list. //YOUR CODE GOES HERE } // end of reverse() /** * Displays the items in the list to which the parameter, first, points. * They are printed on one line, separated by spaces */ static void display(Node first) { //YOUR CODE GOES HERE } // end of display() /** * Return the number of zeros that occur in a given linked list of int. */ static int count( Node head ) { //YOUR CODE GOES HERE }// end of count() /** * Return the number of zeros that occur in a given linked list of int. * Uses recursion */ static int countRecursive( Node head ) { //YOUR CODE GOES HERE } // end of countRecursive() public static void main(String[] args) { Node list = null; // A list, initially empty. Node reverseList; // The reversed list. int count = 0; //The number of elements in the list while (true) { // add a new node onto the head of the list before repeating. count++; if (count == 10) break; Node head = new Node(); // A new node to add to the list. head.value = (int)(Math.random()*100); // A random item. head.next = list; list = head; } // Print the current list ; its reverse // and the number of zeros in the list using both count methods System.out.print("The list: "); display(list); System.out.println(); reverseList = reverse(list); System.out.print("The reversed list: "); display(reverseList); System.out.println(); System.out.println(); System.out.print("The number of zeros in the list : "); System.out.println(count(list)); System.out.print("The number of zeros in the list, using recursion : "); System.out.println(countRecursive(list)); } // end main() } // end LinkedList2 class 

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 Programming Questions!