-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionDeletion.java
More file actions
59 lines (47 loc) · 1.33 KB
/
InsertionDeletion.java
File metadata and controls
59 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;
public class InsertionDeletion {
public static void insertionDeletion(int[] arr, int index, int choice, Scanner t) {
if (choice == 0) {
System.out.println("Enter value to be inserted:");
int val = t.nextInt();
for (int i = arr.length-2; i>=index; i--) {
arr[i + 1] = arr[i];
}
arr[index] = val;
}
else {
if (choice == 1) {
for (int i = index; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
} else
System.out.println("Invalid input of choice");
}
}
public static int[] takeInput() {
Scanner s = new Scanner(System.in);
System.out.println("Enter size of array");
int size = s.nextInt();
int inputArray[] = new int[size];
for (int i = 0; i < size-1; i++) {
System.out.println("Enter " + i + " Element");
inputArray[i] = s.nextInt();
}
return inputArray;
}
public static void print(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + " ");
}
}
public static void main(String[] args) {
int arr[] = takeInput();
Scanner s = new Scanner(System.in);
System.out.println("Enter choice");
int choice = s.nextInt();
System.out.println("Enter index");
int index = s.nextInt();
insertionDeletion(arr, index, choice, s);
print(arr);
}
}