-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked.java
More file actions
79 lines (55 loc) · 2.05 KB
/
Linked.java
File metadata and controls
79 lines (55 loc) · 2.05 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.LinkedList;
import java.util.Scanner;
public class Linked {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
LinkedList<String> countries = new LinkedList<String>();
int counter = 0;
int index =0;
String place = "";
int choice = 0;
System.out.println("[1] Add Country, [2]Get Country,[3] Set Country, [4] Remove Country, [5]Display Country,[6] Exit");
while(counter != 1) {
try {
System.out.print("Enter choice: ");
choice = console.nextInt();
switch(choice) {
case 1 : System.out.print("Add Country: ");
countries.offer(console.next());
break;
case 2 : System.out.print("Enter index: ");
index = console.nextInt();
if(countries.isEmpty())System.out.println("Error! LinkedList is Empty");
else if(index > countries.size())System.out.println("Error! Index Out of Bounds");
else System.out.println(countries.get(index));
break;
case 3 : System.out.print("Enter index: ");
index = console.nextInt();
if(countries.isEmpty())System.out.println("Error! LinkedList is Empty");
else if(index > countries.size())System.out.println("Error! Index Out of Bounds");
else {
System.out.print("Enter a Country to Insert: ");
countries.set(index, console.next());
}
break;
case 4 : System.out.print("Enter a Country that you want to remove: ");
place = console.next();
if(countries.contains(place)) countries.remove(place);
else System.out.println("No Element");
break;
case 5 : if(countries.isEmpty())System.out.println("Display: Empty");
else System.out.println("Display: " + countries);
break;
case 6 : System.out.println("Confirm Exit? [1] YES , [0] NO");
counter = (int) (console.nextInt() == 1 ? 1 : 0);
break;
default : System.out.println("Please enter a specific Number!");
break;
}
}catch(Exception e) {
System.out.println("Invalid Response!");
break;
}
}
}
}