-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueLinked.java
34 lines (30 loc) · 930 Bytes
/
QueueLinked.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
public class QueueLinked<Item> implements Iterable<Item>
{
private Node first; // link to least recently added node
private Node last; // link to most recently added node
private int N; // number of items on the queue
private class Node {
Item item;
Node next;
}
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
public void enqueue(Item item)
{ // Add item to the end of the list.
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
N++;
}
public Item dequeue()
{ // Remove item from the beginning of the list.
Item item = first.item;
first = first.next;
if (isEmpty()) last = null;
N--;
return item;
}
}