-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList_basic_2.java
107 lines (80 loc) · 2.04 KB
/
LinkedList_basic_2.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.LinkedList;
import java.util.Scanner;
public class LinkedList_basic_2 {
static Scanner sn = new Scanner(System.in);
static LinkedList<Integer> ll = new LinkedList<>();
static void swap(LinkedList<Integer> ll, int a, int b) {
int temp = ll.get(a);
ll.add(a, ll.get(b));
ll.add(b, temp);
}
static void create(LinkedList<Integer> ll) {
System.out.println("Total inputs : ");
int n = sn.nextInt();
System.out.println("Enter " + n + " elements : ");
for (int i = 1; i <= n; i++) {
int e = sn.nextInt();
ll.add(e);
}
display(ll);
}
static void insert() {
System.out.println("Insertion Enter the index : ");
int i = sn.nextInt();
System.out.println("Enter the element to insert : ");
int e = sn.nextInt();
ll.add(i, e);
display(ll);
}
static void remove() {
if (ll.isEmpty())
return;
System.out.println("Enter the element to remove : ");
int e = sn.nextInt();
if (!ll.contains(e))
return;
ll.remove(ll.indexOf(e));
display(ll);
}
static void display(LinkedList<Integer> ll) {
System.out.println("The list is : " + ll);
}
static void reverse(LinkedList<Integer> ll) {
for (int i = ll.size() - 1; i >= 0; i--) {
ll.add(ll.get(i));
ll.remove(i);
}
System.out.println("Reversed List : " + ll);
}
static void sort(LinkedList<Integer> ll) {
for (int i = 0; i < ll.size() - 1; i++)
for (int j = 0; j < ll.size() - 1; j++) {
if (ll.get(j) > ll.get(j + 1)) {
swap(ll, j, j + 1);
ll.remove(j + 1);
ll.remove(j + 2);
}
}
System.out.println("Sorted List : " + ll);
}
static void SortInsert(LinkedList<Integer> ll) {
insert();
sort(ll);
}
static void removeall(int e) {
if (ll.isEmpty() || !ll.contains(e)) {
display(ll);
return;
}
ll.remove(ll.indexOf(e));
removeall(e);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
create(ll);
insert();
remove();
reverse(ll);
sort(ll);
}
}