-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddin_Middle.java
73 lines (62 loc) · 1.75 KB
/
addin_Middle.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
class Node {
String data;
Node next;
Node(String data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
private Node head;
private int size;
public LinkedList() {
this.head = null;
this.size = 0;
}
public void AddinMiddle(int index, String data) {
// Check if the index is valid
if (index > size || index < 0) {
System.out.println("Invalid index value.");
return;
}
// Create a new node
Node newNode = new Node(data);
size++;
// If we're inserting at the head
if (head == null || index == 0) {
newNode.next = head;
head = newNode;
return;
}
// Traverse to the node just before the desired index
Node currNode = head;
for (int i = 0; i < index - 1; i++) {
currNode = currNode.next;
}
// Insert the new node
newNode.next = currNode.next;
currNode.next = newNode;
}
// Additional methods for testing
public void printList() {
Node currNode = head;
while (currNode != null) {
System.out.print(currNode.data + " -> ");
currNode = currNode.next;
}
System.out.println("null");
}
public int getSize() {
return size;
}
}
// Example usage
public class addin_Middle {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.AddinMiddle(0, "first");
list.AddinMiddle(1, "second");
list.AddinMiddle(1, "middle");
list.printList(); // Output: first -> middle -> second -> null
}
}