Question

7. hello_world.iml hello # C D Q , eaProjects hello_world - 7. hello_world.iml x Ant //Here is an example problem with answerCan you please just explain what the method does and provide a recurrence relation for the method. And State the methods runThe first image is example of how I want answers and the second image is questions. Can u please give me answers asap. Thank u.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q->1:

given code:

  1. public void problem1(int index) {
  2.     if (index >= 0) {
  3.         MyListNode<E> current = first;
  4.         for (int i = 0; i < index; i++) {
  5.             current = current.getNext();
  6.         }
  7.         System.out.print(current.getData() + " ");
  8.         problem1(index - 1);
  9.     }
  10. }

Answer:

  1. This method prints linked list in reverse from index, means it prints first index elements in reverse order.
  2. f(n) = n + f(n-1), first loop i from 0 to index then recursive call for index-1
  3. O(n2), because index goes upto 0, and every time it loops from 0 to index in line 4

.

Q->2:

given code:

  1. public void problem2(MyListNode<E> node) {
  2.     if (node.getNext() != null) {
  3.         problem2(node.getNext());
  4.     }
  5.     System.out.println(node.getData() + " ");
  6. }

Answer:

  1. This method prints given linked list in reverse order.
  2. f(n) = f(n - 1) + 1
  3. O(n), because it does a constant time work for printing, and it goes till n.
Add a comment
Know the answer?
Add Answer to:
The first image is example of how I want answers and the second image is questions....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you please give me an answer asap. Thank you. Can you please just explain what...

    Can you please give me an answer asap. Thank you. Can you please just explain what the method does and provide a recurrence relation for the method. And State the method's runtime in big-Oh notation of following. Question no 2) //This is an instance method for MyLinkedList from project 1. //For writing the recurrence relation, n is size - index of node. 1/Precondition: node is an element of the list public void problem2 (MyListNode<E> node) { if(node.getNext() != null){ problem2...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of...

    Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of this assignment page on eClass). In this file, you have been given the complete code for a LinkedList class. Familiarize yourself with this class, noticing that it uses the Node class from Task 1 and is almost identical to the SLinkedList class given in the lectures. However, it also has a complete insert(pos, item) method (which should be similar to the insert method you...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • Add a method to the DoubleLinkedList class built in class to reverse every set of values...

    Add a method to the DoubleLinkedList class built in class to reverse every set of values For example: 1, 2, 3, 4, 5, 6 Reverse 3: 3,2,1,6,5,4 Reverse 2: 2,1,4,3,6,5 Reverse 6: 6,5,4,3,2,1 Method header: public void reverseSegments(int setSize) outcome should be like this: Input:​​​​​​​​​​​​​​ 3 1 2 3 4 5 6 output: 3 2 1 6 5 4 Input:​​​​​​​​​​​​​​​​​​​​​ 2 ​​​​​​​1 2 3 4 5 6 output: 2 1 6 5 4 3 ============================================code====================================================================== public class MyDoubleLinkedList<E> { private...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal....

    Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "bcaahttaetersse" 4. String outputDepthFirstSearch(): returns a string represenng a pre order depth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "batcathateersse This is my code so far public class Trie { final TrieNode root; public Trie() { this.root...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT