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 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)
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,...
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
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 c++ member function that attempts to insert a NON DUPLICATE element to a doubly...
Write a c++ member function that attempts to insert a NON DUPLICATE element to a doubly linked list, After the attempted insertion return the SIZE of the doubly linked list whether or not the insertion was successful.
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do...
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do the work
• 1. Write a java program using methods to find the information about a account holder...
• 1. Write a java program using methods to find the information about a account holder at bank. d) Perform the functionalities(Deposit, Withdraw and print) until the user selects to stop.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT