-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion of Doubly Link List at Specific Location
More file actions
132 lines (107 loc) · 3.27 KB
/
Insertion of Doubly Link List at Specific Location
File metadata and controls
132 lines (107 loc) · 3.27 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
package DataS;
import java.util.HashSet;
import java.util.Set;
public class Insertion_Doubly_Linked_Specific_Location {
Node head;
Node tail;
Set<Integer> uniqueNumbers;
public class Node {
int data;
Node next;
Node prev;
public Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
public Insertion_Doubly_Linked_Specific_Location() {
this.head = null;
this.tail = null;
this.uniqueNumbers = new HashSet<>();
}
public void insert(int data) {
if (uniqueNumbers.contains(data)) {
System.out.println("This number already exists in the list: " + data);
return;
}
Node newNode = new Node(data);
uniqueNumbers.add(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}
public void insertAtPosition(int data, int position) {
if (uniqueNumbers.contains(data)) {
System.out.println("This number already exists in the list: " + data);
return;
}
Node newNode = new Node(data);
uniqueNumbers.add(data);
if (position == 0) {
newNode.next = head;
if (head != null) {
head.prev = newNode;
}
head = newNode;
if (tail == null) {
tail = newNode;
}
return;
}
Node current = head;
int index = 0;
while (current != null && index < position - 1) {
current = current.next;
index++;
}
if (current == null) {
System.out.println("Position out of bounds");
return;
}
newNode.next = current.next;
if (current.next != null) {
current.next.prev = newNode;
}
current.next = newNode;
newNode.prev = current;
if (newNode.next == null) {
tail = newNode;
}
}
public void displayForward() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public void displayBackward() {
Node current = tail;
while (current != null) {
System.out.print(current.data + " ");
current = current.prev;
}
System.out.println();
}
public static void main(String[] args) {
Insertion_Doubly_Linked_Specific_Location dll = new Insertion_Doubly_Linked_Specific_Location();
dll.insert(10);
dll.insert(20);
dll.insert(30);
dll.insert(10); // This should print a message that the number already exists
dll.insertAtPosition(15, 1); // Insert 15 at position 1
dll.insertAtPosition(25, 3); // Insert 25 at position 3
dll.insertAtPosition(35, 10); // This should print "Position out of bounds"
System.out.println("Display Forward:");
dll.displayForward();
System.out.println("Display Backward:");
dll.displayBackward();
}
}