update LinkedQueue
This commit is contained in:
parent
c63dbad401
commit
fce3d838db
@ -80,6 +80,11 @@ public class LinkedQueue {
|
|||||||
front.next = front.next.next;
|
front.next = front.next.next;
|
||||||
destroy = null; /* clear let GC do it's work */
|
destroy = null; /* clear let GC do it's work */
|
||||||
size--;
|
size--;
|
||||||
|
|
||||||
|
if (isEmpty()) {
|
||||||
|
front = rear;
|
||||||
|
}
|
||||||
|
|
||||||
return retValue;
|
return retValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,11 +125,16 @@ public class LinkedQueue {
|
|||||||
* Clear all nodes in queue
|
* Clear all nodes in queue
|
||||||
*/
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
//TODO
|
while (!isEmpty()) {
|
||||||
|
dequeue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
return "[]";
|
||||||
|
}
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
Node cur = front.next;
|
Node cur = front.next;
|
||||||
builder.append("[");
|
builder.append("[");
|
||||||
@ -144,11 +154,16 @@ public class LinkedQueue {
|
|||||||
queue.enqueue(1); /* 1 */
|
queue.enqueue(1); /* 1 */
|
||||||
queue.enqueue(2); /* 1 2 */
|
queue.enqueue(2); /* 1 2 */
|
||||||
queue.enqueue(3); /* 1 2 3 */
|
queue.enqueue(3); /* 1 2 3 */
|
||||||
System.out.println(queue);
|
System.out.println(queue); /* [1, 2, 3] */
|
||||||
|
|
||||||
assert queue.size() == 3;
|
assert queue.size() == 3;
|
||||||
assert queue.dequeue() == 1;
|
assert queue.dequeue() == 1;
|
||||||
assert queue.peekFront() == 2;
|
assert queue.peekFront() == 2;
|
||||||
assert queue.peekRear() == 3;
|
assert queue.peekRear() == 3;
|
||||||
|
|
||||||
|
queue.clear();
|
||||||
|
assert queue.isEmpty();
|
||||||
|
|
||||||
|
System.out.println(queue); /* [] */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user