forked from CodersForLife/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
45 lines (39 loc) · 837 Bytes
/
Copy pathDoublyLinkedList.java
File metadata and controls
45 lines (39 loc) · 837 Bytes
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
public class DoublyLinkedList extends LinkedList{
public void insertAtHead(int value){
Node newNode = new Node(value);
if(this.head == null){
this.head = newNode;
}
else{
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
}
public void insertAtPos(int k,int value){
Node newNode = new Node(value);
int cnt = 0;
Node temp = this.head;
while(temp != null){
if(cnt == k){
temp.prev.next = newNode;
newNode.next = temp;
newNode.prev = temp.prev;
temp.prev = newNode;
return;
}
temp = temp.next;
}
System.out.println("invalid position");
}
public void deleteValue(int value){
Node temp = this.head;
while(temp!= null){
if(temp.data == value){
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
return;
}
}
}
}