thespeedracer38

Rearrange

Jun 28th, 2021 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Rearrange
  3. {
  4.     static Scanner sc = new Scanner(System.in);
  5.     int a[];
  6.     int n;
  7.     int capacity;
  8.     int pos1;
  9.     int pos2;
  10.     int item;
  11.    
  12.     void enter()
  13.     {
  14.         // Maximum number of elements that the array
  15.         // can store
  16.         capacity = 1000;
  17.         a = new int[capacity];
  18.         System.out.print("Enter the size of the array: ");  
  19.         n = sc.nextInt();
  20.  
  21.         for(int i = 0; i < n; i++)
  22.         {
  23.             System.out.print("Enter element " + (i + 1) + ": ");
  24.             a[i] = sc.nextInt();
  25.         }
  26.     }
  27.    
  28.     void insert()
  29.     {
  30.         // Take a valid position as input
  31.         if(n == capacity)
  32.         {
  33.             System.out.println("Array does not have space to insert more elements");
  34.         }
  35.         else
  36.         {
  37.             while(true)
  38.             {
  39.                 System.out.println("Valid positions for insertions are");
  40.                 System.out.println(0 + " to " + n);
  41.                 System.out.print("Enter the position to insert element: ");
  42.                 pos1 = sc.nextInt();
  43.                 if(pos1 >= 0 && pos1 <= n)
  44.                 {
  45.                     break;
  46.                 }
  47.                 else
  48.                 {
  49.                     System.out.println(pos1 + " is an invalid position.");
  50.                 }
  51.             }
  52.             System.out.print("Enter the element to be inserted in array: ");
  53.             item = sc.nextInt();
  54.             // Shift all elements from positions
  55.             // pos1 to n-1, one place to the right
  56.             for(int i = n-1; i >= pos1; i--)
  57.             {
  58.                 a[i + 1] = a[i];
  59.             }
  60.             // Insert the new element
  61.             a[pos1] = item;
  62.             // Increase the size of the array
  63.             n = n + 1;
  64.             System.out.println(item + " inserted at position " + pos1);
  65.             System.out.println("New contents of the array are");
  66.             disp1();
  67.         }
  68.     }
  69.    
  70.     void remove()
  71.     {
  72.         if(n == 0)
  73.         {
  74.             System.out.println("Cannot delete from an empty array.");
  75.         }
  76.         else
  77.         {
  78.             while(true)
  79.             {
  80.                 System.out.println("Valid positions for insertions are");
  81.                 System.out.println(0 + " to " + (n-1));
  82.                 System.out.print("Enter the position to delete an element: ");
  83.                 pos2 = sc.nextInt();
  84.                 if(pos2 >= 0 && pos2 <= (n - 1))
  85.                 {
  86.                     break;
  87.                 }
  88.                 else
  89.                 {
  90.                     System.out.println(pos2 + " is an invalid position.");
  91.                 }
  92.             }
  93.             // To delete the element at pos2 position,
  94.             // all elements from pos2 + 1 to n-1 must be shifted by
  95.             // one place to the left
  96.             int deletedElement = a[pos2];
  97.             for(int i = pos2 + 1; i < n; i++)
  98.             {
  99.                 a[i - 1] = a[i];
  100.             }
  101.             // Decrease size of array
  102.             n = n - 1;
  103.             System.out.println(deletedElement + " deleted from position " + pos2);
  104.             System.out.println("New contents of the array are");
  105.             disp2();
  106.         }
  107.     }
  108.    
  109.     void disp1()
  110.     {
  111.         for(int i = 0; i < n; i++)
  112.         {
  113.             System.out.print(a[i] + ", ");
  114.         }
  115.         System.out.println("");
  116.     }
  117.    
  118.     void disp2()
  119.     {
  120.         disp1();
  121.     }
  122.    
  123.     public static void main(String[] args)
  124.     {
  125.         Rearrange obj = new Rearrange();
  126.         obj.enter();
  127.         int choice;
  128.         do
  129.         {
  130.             System.out.println("---------Menu---------");
  131.             System.out.println("Press 1 for inserting an element in the array");
  132.             System.out.println("Press 2 for deleting an element from the array");
  133.             System.out.println("Press 3 for displaying the contents of the array");
  134.             System.out.println("Press 4 to quit");
  135.             System.out.print("Enter your choice(1-4): ");
  136.             choice = sc.nextInt();
  137.            
  138.             switch(choice)
  139.             {
  140.                 case 1: {
  141.                     obj.insert();
  142.                     break;
  143.                 }
  144.                 case 2: {
  145.                     obj.remove();
  146.                     break;
  147.                 }
  148.                 case 3: {
  149.                     obj.disp1();
  150.                     break;
  151.                 }
  152.                 case 4: {
  153.                     System.out.println("Quitting...");
  154.                     break;
  155.                 }
  156.                 default: {
  157.                     System.out.println("Invalid choice.");
  158.                 }
  159.             }
  160.         }while(choice != 4);
  161.     }
  162. }
  163.  
Add Comment
Please, Sign In to add comment