Question

In: Computer Science

DATA STRUCTURES   What is returned when calling this method with the singly list contains (1,5,5,7,8)   public...

DATA STRUCTURES  

What is returned when calling this method with the singly list contains (1,5,5,7,8)  

public static int m1(SinglyList  list1)

{

  if(list1.size()!=2)

return list1.removeFirst()+m1(list1);

     return s;

}

Solutions

Expert Solution

In the above code snippet, 's' is not defined.

Here also removeFirst () is not defined

Returning or addition of an array removeFirst(as the name suggests) and m1(list) is possibly not possible.

if they return length after removal of first and integer in case of s. then the following will hold true.

def abc(list1):
    s=0#assumed
    if(len(list1)!=2):
        
        return (len(list1)-1)+abc(list1[1:])#[1:] represents the list without the first element
    return s
a=[1,5,5,7,8]
print(abc(a))

The expected answer returned is 9

A screenshot is provided for viewing

There is a recursive call function here.

i would like to show you at everystep how the return function is getting called.

for further clarification comment below.


Related Solutions

Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. The method should receive only one parameter (the array list) The base case is when the array list has only one element, just remove that, otherwise, you need to remove the first one and the last one then call the method, then you need to add them again after...
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void...
Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void m3(ArrayList list1) {        if (list1.size() ==1)   System.out.println(list1.get(0));        else               if (list1.size()%2==0)     System.out.println(list1.remove(0));               else                   System.out.println(list1.remove(list1.size()-1));             } What will be the output? Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void m3(ArrayList list1) {        if (list1.size() ==1)   System.out.println(list1.get(0));        else               if (list1.size()%2==0)     System.out.println(list1.remove(0));               else                   System.out.println(list1.remove(list1.size()-1));             } What will be the output?
Write a method that returns the result when the calling object is multiplied by a scalar...
Write a method that returns the result when the calling object is multiplied by a scalar value. For example, the PolyTerm 2.4x^3 multiplied by -1.2 should return the PolyTerm object representing -2.88x^3. Language: Java. Method name be like: scalarMultiply(double) Some Outputs: Test 1: Coefficient =1, Exponent = 1 scalarMultiply(1.2).coefficient return 1.2; scalarMultiply(1.2).exponent returns 1. Test 2: Coefficient =2.4, Exponent = 3 scalarMultiply(-1.2).coefficient returns -2.88 scalarMultiply(-1.2).exponent return 3 Test 3: Coefficient =-1.5 Exponent = 0 scalarMultiply(0).coefficient returns 0 scalarMultiply(0).exponent returns 3...
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
1) What are the accessory structures of the eye? 2) List the structures that are in...
1) What are the accessory structures of the eye? 2) List the structures that are in each layer of the intrinsic eye. 3) List the pathway for vision.
1. When a method definition contains an invocation of itself, the method is said to be...
1. When a method definition contains an invocation of itself, the method is said to be _________. 2. When a method definition contains an invocation of itself, the method is said to be _________. 3. True or False. A recursive method needs to have an if-else or other branching leading to different cases. 4. True or False. A recursive method should only contain one call to itself.
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT