Question

In: Computer Science

compute the position of the last 5.0 in the list, counting as an offset from the...

compute the position of the last 5.0 in the list, counting as an offset from the end. if 5.0 is the LAST element, the position is 0. if 5.0 does not appear, return a negative number. you can write this using a loop or recursion, in any style, but you should only have one loop or recursive helper

Use a backward recursion.If the number does not appear, return the distance to the END of the list as a NEGATIVE number.

public class MyLinked {
   static class Node {
       public Node (double item, Node next) { this.item = item; this.next = next; }
       public double item;
       public Node next;
   }
   int N;

public int positionOfLastFiveFromEnd () {

You can NOT modify the list or parameters, add fields to the node or list class, or add any methods/use any functions to the node class. Please use a helper and backward recursion.

Solutions

Expert Solution

import java.io.*;
public class MyLinked {
public static Node head;
static class Node{
       public Node (double item, Node next)
        {
          this.item = item; this.next = next;
        }
       public double item;
       public Node next;}
   int N;

   public int positionOfLastFiveFromEnd ()
   {
            Node currNode = head;
        N=0;
          // Traverse through the LinkedList
       while (currNode!= null)
       { N-=1;
               // if currNode value is 5.0 then counter of
               // N will start from 0 again and will reach till
            if (currNode.item==5.0) {N=0;}
                // Go to next node
           currNode = currNode.next;
               }
        // last N's position will start from 0 ,
        //then it will be decremented by 1 until last node is reached.
       return( N);
  
       }
public static void main(String[] args)
    {
        /* Start with the empty list. */
   MyLinked list = new MyLinked();

  
       Node d=new Node (7,null); //making a last node with value equal to 5
   Node c=new Node(5,d); //making a node with value equal to 4
   Node b=new Node(3,c); //making a node with value equal to 3
   Node a=new Node(2,b); //making a node with value equal to 2
   head=a;
   int x =list.positionOfLastFiveFromEnd();// calling function
   System.out.print(x);
}
}


Related Solutions

Use the counting techniques from the last chapter. A bag contains two red marbles, three green...
Use the counting techniques from the last chapter. A bag contains two red marbles, three green ones, one fluorescent pink one, two yellow ones, and three orange ones. Suzan grabs three at random. Find the probability of the indicated event. She gets all the red ones, given that she gets the fluorescent pink one.
. Choose a company from the following list and comment on their market position. Your answer...
. Choose a company from the following list and comment on their market position. Your answer must be in essay format. Address the following issues in your answer: • Choose any one of the following: Tesla, Amazon, Netflix, Zoom, Starbucks. You should get company information from either yahoo finance or WSJ markets. • The P/E • Is the stock expensive? Yes or No? Justify your answer • How many years will it take to recover the investment if earnings remain...
a) List four insights that can be gained on a company’s financial position / performance from...
a) List four insights that can be gained on a company’s financial position / performance from its cash flow statement. b) Briefly explain the relationship and differences between the income statement and the cash flow statement. c) Discuss whether accountants would be prepared to recognise something like an underground water supply as an asset on the company’s balance sheet.
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
You are asked to delete the last occurrence of an item from a linked list. So,...
You are asked to delete the last occurrence of an item from a linked list. So, for instance: Input: 2 -> 3 -> 2 -> 4 Delete last occurrence of 2, result: 2 -> 3 -> 4 Implement the following method to do the deletion. You may NOT use or implement helper methods - all your code must be implemented inside the given method. You may NOT use recursion. public class Node { public int data; public Node next; }...
Suppose the individuals view their loss of income from income taxes as offset by the benefits...
Suppose the individuals view their loss of income from income taxes as offset by the benefits of public services purchased with revenues. How are their labor supply decisions affected? (a) Draw the optimal choice of a representative individual in leisure-consumption space before tax imposition. (b) Describe the change of budget constraint when the income tax is imposed and all the revenue from income tax is returned as the individual’s consumption through public service provision. (c) Show the change of optimal...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Investors can use the first $25,000 of losses from rental property to offset income from any...
Investors can use the first $25,000 of losses from rental property to offset income from any source provided the investor actively participates in the management of the property and has taxable income before the deduction of no more than $100,000. $125,000. $150,000. $175,000.
Research and obtain an application from a position in your field you would pursue. 1.List all...
Research and obtain an application from a position in your field you would pursue. 1.List all the “hard skills” that are required to apply for this role. 2.tell me how soft skills play into demonstrating that you do possess the skills they are seeking. 3. Write down your best soft skills 4.put a list of at least 3 professional references together. List their name, job title and company
To offset the cost of buying a $100,000 house, Julia borrowed $22,500 from her parents at...
To offset the cost of buying a $100,000 house, Julia borrowed $22,500 from her parents at 6% nominal interest, compounded monthly. The loan from her parents is to be paid off in five years in equal monthly payments. She has saved $21,250. Her total down payment is therefore $22,500 + 21,250 = $43,750. The balance will be mortgaged at 9% nominal interest, compounded monthly for 30 years. Find the combined monthly payment that Julia will be making for the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT