forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListInsertion.java
43 lines (37 loc) · 1.32 KB
/
LinkedListInsertion.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
37
38
39
40
41
42
43
package com.example.demo;
import java.util.*;
class example{
public static void main(String[] args){
// create linkedlist
LinkedList<String> ll = new LinkedList<>();
Scanner sc = new Scanner(System.in);
// Add elements to LinkedList
System.out.println("Options:");
System.out.println("1. Enter element.\n2. Exit.");
System.out.println("Enter choice: ");
int ch = 1;
ch = Integer.parseInt(sc.nextLine());
do{
switch(ch){
case 1:
//insert the element.
System.out.println("Enter the element: ");
String element = sc.nextLine();
ll.add(element);
break;
case 2:
//exit the loop,
ch=2;
System.out.println("Ending insertion into linked list...");
break;
default:
//invalid choice:
System.out.println("Enter a valid input!");
break;
}
System.out.println("Enter choice: ");
ch = Integer.parseInt(sc.nextLine());
}while(ch!=2);
System.out.println("LinkedList: " + ll);
}
}