-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLList.java
64 lines (56 loc) · 1.25 KB
/
DLList.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
class _DLList<T> {
private class Node {
private T value;
private Node next;
private Node prev;
}
private Node first, last;
private int size = 0;
public void addFirst(T value) {
Node node = new Node();
node.value = value;
node.next = first;
if(first != null) {
first.prev = node;
}
first = node;
if(last == null) {
last = first;
}
size++;
}
public void addLast(T value) {
Node node = new Node();
node.value = value;
node.prev = last;
if(last != null) {
last.next = node;
}
last = node;
if(first == null) {
first = last;
}
size++;
}
public int size() {
return size;
}
public void print() {
Node ptr;
ptr = first;
while(ptr != null) {
System.out.print(ptr.value + " ");
ptr = ptr.next;
}
System.out.println();
}
}
public class DLList {
public static void main(String[] args) {
_DLList<String> l = new _DLList<>();
l.addFirst("bird");
l.addFirst("fish");
l.addLast("mammal");
l.print();
}
}