Question

I need a method for public void ****************** write the method “insertDoubles” that for each value...

I need a method for public void ******************

write the method “insertDoubles” that for each value found in an integer linked list it inserts that value doubled after the original. The method should return nothing and take in in no parameters. If the list is empty, then the method should do nothing. To avoid infinite loops you need to move the iterator past the newly inserted value.

here is the provided code

public class Question02 
{
        public class ListNode//public for testing purposes
        {
                public int data;//public for testing purposes
                public ListNode link;//public for testing purposes
                public ListNode(int aData, ListNode aLink)
                {
                        data = aData;
                        link = aLink;
                }
        }
        public ListNode head;//public for testing purposes
        public void insertDoubles()
        {
//-----------------------------------------------------------------------------------
                //Write your solution here

                
                
                
                
                
                
                
                
                
                
                
                

        }//Do not alter this
    //--------------------------------------------------------------------------------
        //Test your code here.
        public static void main(String[] args)
        {
                //Example
                Question02 intLL = new Question02();
                intLL.head = intLL.new ListNode(0, 
                                                        intLL.new ListNode(1, 
                                                                        intLL.new ListNode(2,
                                                                                        intLL.new ListNode(3,
                                                                                                        intLL.new ListNode(4,null)))));
                intLL.insertDoubles();
                //Printing Results
                for(Question02.ListNode temp = intLL.head;temp != null; temp = temp.link)
                        System.out.println(temp.data);
        }
        //--------------------------------------------------------------------------------
}//Do not alter this

Solution Tests:

  • Does the solution compile?
  • Does the solution have your name in the comments?
  • Does the solution have a high-level solution description (150-300 words) in the comments?
  • If the linked list had the values <0,1,2,3,4> is the resulting linked list <0,0,1,2,2,4,3,6,4,8>?
  • If the linked list had the values <2,4,6,8,10> is the resulting linked list <2,4,4,8,6,12,8,16,10,20>?
  • If the linked list was empty is the resulting linked list empty?

Homework Answers

Answer #1

Code:

public void insertDoubles()
{
 if (this.head == null){
    return;
  }
 for(Question02.ListNode temp = this.head; temp != null;){
   Question02.ListNode newLink = temp.link;
   temp.link = new Question02.ListNode(temp.data*2, temp.link);
   temp = newLink;
   }
}

output snippet:

<0,1,2,3,4>

<2,4,6,8,10>

Empty list: <>

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
I NEED THIS ANSWER FOR MY DATA STRUTURE CLASS: Consider the following two methods: public static...
I NEED THIS ANSWER FOR MY DATA STRUTURE CLASS: Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...