Question

In: Computer Science

Write the methods that insert and remove an element at the kth position in Java using...

Write the methods that insert and remove an element at the kth position in Java using recursion (NOT iteration) (Hint for the remove method: we have to recursive position -1 times, and each time we do the recursion, we have to create a method to move the head to the right)

public void insertRecursive(int position, int data)
{
   //enter code here
}

public int removeAtRecursive(int position)
{
   //enter code here
}

Here is the given class for the Node:

public class Node {
  private int data;
  private Node next;

  public Node(int data)
  {
    this.data = data;
    this.next = null;
  }
  public void setNext(Node next)
  {
    this.next = next;
  }
  public Node getNext()
  {
    return next;
  }

  public void setData(int data)
  {
    this.data = data;
  }

  public int getData()
  {
    return data;
  }
}

Solutions

Expert Solution

//Java program

class Node {
   private int data;
   private Node next;

   public Node(int data)
   {
   this.data = data;
   this.next = null;
   }
   public void setNext(Node next)
   {
   this.next = next;
   }
   public Node getNext()
   {
   return next;
   }

   public void setData(int data)
   {
   this.data = data;
   }

   public int getData()
   {
   return data;
   }
}

class List{
   private Node head;
     
   public List(){
       head=null;
   }
   public void insertRecursive(int position, int data)
   {
   Node node = new Node(data);
  
   head = insert(head,position-1,node);
   }

   private Node insert(Node head2, int position, Node node) {
       if(head2==null)return node;
       if(position==1) {
           node.setNext(head2);
           return node;
       }
       head2.setNext(insert(head2.getNext(),position-1,node));
       return head2;
   }
   public void removeAtRecursive(int position)
   {
   head = remove(head,position);
   }
   private Node remove(Node start, int position) {
       if (position < 1) return start;
          
           // If linked list is empty
           if (start == null)
           return null;
          
           // Base case (start needs to be deleted)
           if (position == 1)
           {
           Node res = start.getNext();
           return res;
           }
          
           start.setNext(remove(start.getNext(), position-1));
           return start;
      
   }
}


Related Solutions

Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
Using the trend in the periodic table, and position of an element in it, the smallest element is
Using the trend in the periodic table, and position of an element in it, the smallest element is a. hydrogen b. Helium c. Oxygen d. Carbon
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that is * less than or equal to the limit parameter * if all values are greater than theVal, return -1; * * Precondition: the array is nonempty and all elements are unique. * Your solution must go through the array exactly once. * * <pre> * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0 * 5 == posOfLargestElementLtOeT(3,...
in Java, write a program for methods (Secant, False-Position and Modified Secant) for locating roots. Make...
in Java, write a program for methods (Secant, False-Position and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations. (a) f(x) = 2x3 – 11.7x2 + 17.7x – 5 This function has 3 +ve roots, all of which lie between 0 and 4. Find the roots. Implement the...
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows...
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows classic indexing from 0 to item_count - 1)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT