forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chap03.question.29.MyLinkedListPrint.java
141 lines (125 loc) · 3.79 KB
/
Chap03.question.29.MyLinkedListPrint.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
133
134
135
136
137
138
139
140
141
//Chap03.question.29.MyLinkedListPrint.java
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyLinkedList<T> implements Iterable<T> {
private Node<T> head, tail;
private int size, modCount;
private int deletedNodeCount;
public MyLinkedList() {
size = modCount = deletedNodeCount = 0;
head = new Node<>(null, null, null);
tail = new Node<>(null, null, null);
head.next = tail;
tail.prev = head;
}
public void print() {
Node<T> cursor = tail.prev;
while (cursor != head) {
System.out.print(cursor.data);
if (cursor.prev != head && cursor.next != tail)
System.out.print("<=>");
}
System.out.println();
}
@Override
public Iterator<T> iterator() {
return new MyListIterator();
}
private void addBefore(Node<T> node, T t) {
Node<T> before = node.prev;
Node<T> newNode = new Node<T>(null, t, null);
before.next = newNode;
newNode.next = node;
node.prev = newNode;
newNode.prev = before;
size++;
modCount++;
}
private void remove(Node<T> node) {
//node must be in the list
node.deleted = true;
deletedNodeCount++;
size--;
modCount++;
if (deletedNodeCount >= size) {
Node<T> cursor = head.next;
while (cursor != tail) {
if (cursor.deleted) {
Node<T> before = cursor.prev;
Node<T> after = cursor.next;
before.next = after;
after.prev = before;
cursor.prev = null;
cursor.next = node;
}
cursor = cursor.next;
}
deletedNodeCount = 0;
}
}
private Node<T> getNode(int index) {
if (index < 0 || index >= size)
throw new NoSuchElementException();
if (index < size / 2) {
Node<T> cursor = head;
int i = 0;
while (i < index) {
cursor = cursor.next;
if (!cursor.deleted)
i++;
}
return cursor.next;
} else {
Node<T> cursor = tail;
int i = 0;
while (i < size - index) {
cursor = cursor.prev;
if (!cursor.deleted)
i--;
}
return cursor;
}
}
private static class Node<T> {
T data;
Node<T> prev, next;
boolean deleted;
private Node(Node<T> p, T d, Node<T> n) {
prev = p;
data = d;
next = n;
deleted = false;
}
}
private class MyListIterator implements Iterator<T> {
private Node<T> cursor = head;
private int expectedModCount = modCount;
private boolean canRemove = false;
@Override
public boolean hasNext() {
return cursor.next != tail;
}
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
T t = cursor.data;
cursor = cursor.next;
canRemove = true;
return t;
}
@Override
public void remove() {
if (!canRemove)
throw new IllegalStateException();
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
MyLinkedList.this.remove(cursor.prev);
canRemove = false;
expectedModCount++;
}
}
}