-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha52doubleLL.java
More file actions
84 lines (77 loc) · 2.02 KB
/
a52doubleLL.java
File metadata and controls
84 lines (77 loc) · 2.02 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
public class a52doubleLL {
public class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
public static Node headNode;
public static Node tailNode;
// add first in dll
public void addFirst(int data) {
Node newNode = new Node(data);
if (headNode == null) {
headNode = tailNode = newNode;
return;
}
newNode.next = headNode;
headNode.prev = newNode;
headNode = newNode;
}
// print
public void print() {
Node temp = headNode;
if (headNode == null) {
System.out.println("null");
}
while (temp != null) {
System.out.print(temp.data + "<->");
temp = temp.next;
}
System.out.println("null");
}
// remove first from dll
public void removeFirst() {
if (headNode == null) {
System.out.println("Dll is empty");
return;
}
if (headNode.next.next == null) {
headNode = tailNode = null;
return;
}
headNode = headNode.next;
headNode.prev = null;
}
// reverse a dll
public void revDLL() {
Node prev = null;
Node curr = tailNode = headNode;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
curr.prev = next;
prev = curr;
curr = next;
}
headNode = prev;
}
public static void main(String[] args) {
a52doubleLL dll = new a52doubleLL();
dll.addFirst(1);
dll.addFirst(2);
dll.addFirst(3);
// dll.print();
// dll.removeFirst();
// dll.removeFirst();
// dll.removeFirst();
dll.print();
dll.revDLL();
dll.print();
}
}