-
Notifications
You must be signed in to change notification settings - Fork 0
/
qb218.java
36 lines (29 loc) · 963 Bytes
/
qb218.java
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
/* QB 218:
Write a Java program to insert the specified element at the specified position in the linked
list.
*/
import java.util.*;
class QB218 {
public static void main(String[] args) {
LinkedList<Integer> a1 = new LinkedList<>();
a1.add(10);
a1.add(20);
a1.add(30);
a1.add(40);
a1.add(50);
System.out.println("Original List" + a1);
Scanner sc = new Scanner(System.in);
System.out.println("Enter Specific position(index) to insert new element: ");
int pos = sc.nextInt();
if (pos >= 0 && pos <= a1.size()) {
System.out.println("Enter Element to insert: ");
int ele = sc.nextInt();
a1.add(pos, ele);
System.out.println("Updated list: " + a1);
} else {
System.out.println("Invalid Index");
System.out.println();
System.out.println("List not Updated");
}
}
}