forked from stamps/FAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
48 lines (41 loc) · 1.19 KB
/
DoublyLinkedList.java
File metadata and controls
48 lines (41 loc) · 1.19 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
/******************************************************************************
*
* A list implemented with a doubly linked list.
*
******************************************************************************/
public class DoublyLinkedList {
private Node pre; // sentinel before first item
private Node post; // sentinel after last item
public DoublyLinkedList() {
pre = new Node();
post = new Node();
pre.next = post;
post.prev = pre;
}
public boolean isEmpty() { return pre.next.equals(post); }
// add the item to the list
public void add(Integer item) {
Node last = post.prev;
Node x = new Node(item);
x.next = post;
x.prev = last;
post.prev = x;
last.next = x;
}
// add the node to the list
public void add(Node x) {
Node last = post.prev;
x.next = post;
x.prev = last;
post.prev = x;
last.next = x;
}
// pop off the last node
public Node remove() {
Node last = post.prev;
Node secondLast = last.prev;
secondLast.next = post;
post.prev = secondLast;
return last;
}
}