-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLinkedList.java
132 lines (113 loc) · 3.55 KB
/
DLinkedList.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
public class DLinkedList<T> {
private Node firstNode;
private Node lastNode;
private Node middleNode;
private int numberOfEntries;
private int middlePosition;
public DLinkedList() {
firstNode = null;
lastNode = null;
middleNode = null;
numberOfEntries = 0;
middlePosition = 0;
}
public void add(T newEntry) {
if (firstNode == null) {
firstNode = new Node(null, newEntry, null);
lastNode = firstNode;
} else {
Node newNode = new Node(lastNode, newEntry, null);
lastNode.next = newNode;
lastNode = newNode;
}
numberOfEntries++;
if (numberOfEntries % 2 == 1) {
if (middleNode == null) {
middleNode = firstNode;
} else {
middleNode = middleNode.next;
}
middlePosition++;
}
}
public T getEntry(int givenPosition) {
T result = null;
if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
result = (getNodeAt(givenPosition)).data;
}
return result;
}
public boolean contains(T entry) {
if (this.isEmpty()) {
return false;
}
for (int i = 1; i <= numberOfEntries; i++) {
T listEntry = getEntry(i);
if (listEntry.equals(entry)) {
return true;
}
}
return false;
}
public boolean isEmpty() {
return numberOfEntries == 0;
}
public void clear() {
firstNode = null;
lastNode = null;
middleNode = null;
numberOfEntries = 0;
middlePosition = 0;
}
public int size() {
return numberOfEntries;
}
/**
* Modify this method according to the Solution 2
*/
private Node getNodeAt(int givenPosition) {
// TO DO
Node currentNode = null;
//givenPosition is the first position
//Traverse from firstNode forward
if (givenPosition <= (numberOfEntries / 4)) {
currentNode = firstNode;
for (int i = 1; i < givenPosition; i++) {
currentNode = currentNode.next;
}
} //Traverse from middleNode backwards
else if ((givenPosition <= (numberOfEntries / 2))
&& (givenPosition > (numberOfEntries / 4))) {
currentNode = middleNode;
for (int i = middlePosition; i != givenPosition; i--) {
currentNode = currentNode.previous;
}
} //Traverse from middleNode forwards
else if ((givenPosition >= (numberOfEntries / 2)) && (givenPosition < (numberOfEntries * 3) / 4)) {
currentNode = middleNode;
for (int i = middlePosition; i < givenPosition; i++) {
if (currentNode.next == null) {
break;
}
currentNode = currentNode.next;
}
} //Traverse from lastNode backwards
else if ((givenPosition >= (numberOfEntries * 3) / 4)) {
currentNode = lastNode;
for (int i = numberOfEntries; i != givenPosition; i--) {
currentNode = currentNode.previous;
}
}
return currentNode;
}
private class Node {
private Node previous;
private T data;
private Node next;
private Node(Node previousNode, T aData, Node nextNode) {
previous = previousNode;
data = aData;
next = nextNode;
}
}
}