Repository navigation
Expand file tree
/
Copy pathIntDLList.java
More file actions
70 lines (69 loc) · 2.09 KB
/
Copy pathIntDLList.java
File metadata and controls
70 lines (69 loc) · 2.09 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
//************************ IntDLList.java ***************************/
public class IntDLList {
private IntDLLNode head, tail;
public IntDLList() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void setToNull() {
head = tail = null;
}
public int firstEl() {
if (!isEmpty())
return head.info;
else return 0;
}
public void addToDLListHead(int el) {
if (!isEmpty()) {
head = new IntDLLNode(el,head,null);
head.next.prev = head;
}
else head = tail = new IntDLLNode(el);
}
public void addToDLListTail(int el) {
if (!isEmpty()) {
tail = new IntDLLNode(el,null,tail);
tail.prev.next = tail;
}
else head = tail = new IntDLLNode(el);
}
public int deleteFromDLListHead() {
if (!isEmpty()) { // if at least one node in the list;
int el = head.info;
if (head == tail) // if only one node in the list;
head = tail = null;
else { // if more than one node in the list;
head = head.next;
head.prev = null;
}
return el;
}
else return 0;
}
public int deleteFromDLListTail() {
if (!isEmpty()) {
int el = tail.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else { // if more than one node in the list;
tail = tail.prev;
tail.next = null;
}
return el;
}
else return 0;
}
public void printAll() { //OutputStream Out) {
for (IntDLLNode tmp = head; tmp != null; tmp = tmp.next)
System.out.print(tmp.info + " ");
}
public int find(int el) {
IntDLLNode tmp;
for (tmp = head; tmp != null && tmp.info != el; tmp = tmp.next);
if (tmp == null)
return 0;
else return tmp.info;
}
}