-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedListOperations.java
More file actions
172 lines (165 loc) · 4.52 KB
/
Copy pathLinkedListOperations.java
File metadata and controls
172 lines (165 loc) · 4.52 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.util.Scanner;
class Node<T>{
T data;
Node<T> next; // Reference Variable
Node(T data){
this.data = data;
this.next = null;
}
}
class LinkedListOperations<T>{
Scanner scanner = new Scanner(System.in);
Node<T> head;
Node<T> tail;
void add(T data){
Node<T> newNode = new Node<>(data);
if(head==null){
head = newNode;
tail = newNode;
}
else{
tail.next = newNode;
tail = newNode;
}
}
void addPositionWise(int position, T data){
Node<T> newNode = new Node<>(data);
// Put the New Node on Head
if(position ==0){
newNode.next = head;
head = newNode;
return ;
}
int i = 1;
Node<T> temp = head;
while(i<position){
temp = temp.next;
i++;
}
newNode.next = temp.next;
temp.next = newNode;
}
void print(Node<T> start){
Node<T> temp = start;
while(temp!=null){
System.out.println(temp.data);
temp = temp.next;
}
}
void midElement(){
Node<T> slow = head;
Node<T> fast = head;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
}
System.out.println(slow.data); // Mid Element
}
void findKthElementFromLast(int kth){
Node<T> p = head;
Node<T> q = head;
// move q till kth
for(int i = 1; i<=kth; i++){
q = q.next;
}
// now q move till end
while(q!=null){
p = p.next;
q = q.next;
}
System.out.println("Kth Element "+p.data);
}
void detectCycleAndRemoveCycle(){
Node<T> slow = head;
Node<T> fast = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){ // Detect the Cycle
System.out.println("Yes Cycle...");
break;
}
}
if(slow!=fast){
System.out.println("No Cycle...");
return ;
}
slow = head;
while(slow.next!=fast.next){
slow = slow.next;
fast = fast.next.next;
}
fast.next = null; // Break the Cycle
}
void deletePositionWise(int position){
if(head == null){
System.out.println("Linked List is Empty !");
return ;
}
Node<T> temp = head;
Node<T> temp2 = null;
// Delete Head
if(position ==0){
Node<T> temp3 = head;
temp = head.next;
head = temp;
temp3.next = null;
temp3 = null;
return ;
}
int i =1;
temp = head;
while(i<position){
temp2 = temp;
temp = temp.next;
i++;
}
temp2.next = temp.next;
temp.next = null;
temp = null;
}
public static void main(String[] args) {
LinkedListOperations<Integer> opr = new LinkedListOperations<>();
int choice =0;
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("1. Add");
System.out.println("2. Print");
System.out.println("3. Add Position Wise...");
System.out.println("4. Delete Position Wise...");
System.out.println("Enter the Choice...");
choice = scanner.nextInt();
switch(choice){
case 1:
opr.add(10);
opr.add(20);
opr.add(30);
opr.add(40);
break;
case 2:
opr.print(opr.head);
break;
case 3:
opr.addPositionWise(2, 1000);
break;
case 4:
opr.deletePositionWise(2);
break;
default:
System.out.println("Wrong Choice....");
return ;
}
}
// opr.add(100);
// opr.add(200);
// opr.add(300);
// opr.print(opr.head);
// // Create a node
// Node<Integer> node = new Node<>(100);
// // Connect the Nodes
// Node<Integer> node2 = new Node<>(200);
// node.next = node2;
// LinkedListOperations<Integer> l = new LinkedListOperations<>();
// l.print(node);
}
}