This repository was archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
171 lines (149 loc) · 3.73 KB
/
Queue.java
File metadata and controls
171 lines (149 loc) · 3.73 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* The <tt>Queue</tt> class represents a first-in-first-out (FIFO) queue of
* generic items. It supports the usual <em>enqueue</em> and <em>dequeue</em>
* operations, along with methods for peeking at the top item, testing if the
* queue is empty, and iterating through the items in FIFO order.
* <p>
* All queue operations except iteration are constant time.
* <p>
* For additional documentation, see <a href="/algs4/13stacks">Section 1.3</a>
* of <i>Algorithms in Java, 4th Edition</i> by Robert Sedgewick and Kevin
* Wayne.
*/
public class Queue<Item> implements Iterable<Item> {
protected int N; // number of elements on queue
protected Node first; // beginning of queue
protected Node last; // end of queue
// helper linked list class
protected class Node {
private Item item;
private Node next;
}
/**
* Create an empty queue.
*/
public Queue() {
first = null;
last = null;
}
/**
* Is the queue empty?
*/
public boolean isEmpty() {
return first == null;
}
/**
* Return the number of items in the queue.
*/
public int size() {
return N;
}
/**
* Return the item least recently added to the queue. Throw an exception if
* the queue is empty.
*/
public Item peek() {
if (isEmpty())
throw new RuntimeException("Queue underflow");
return first.item;
}
/**
* Add the item to the queue.
*/
public void enqueue(Item item) {
Node x = new Node();
x.item = item;
if (isEmpty()) {
first = x;
last = x;
} else {
last.next = x;
last = x;
}
N++;
}
/**
* Remove and return the item on the queue least recently added. Throw an
* exception if the queue is empty.
*/
public Item dequeue() {
if (isEmpty())
throw new RuntimeException("Queue underflow");
Item item = first.item;
first = first.next;
N--;
if (isEmpty())
last = null; // to avoid loitering
return item;
}
/**
* Return string representation.
*/
public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}
/**
* Return an iterator that iterates over the items on the queue in FIFO
* order.
*/
public Iterator<Item> iterator() {
return new FIFOIterator();
}
// an iterator, doesn't implement remove() since it's optional
private class FIFOIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext())
throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
/**
* A test client.
*/
public static void main(String[] args) {
/***********************************************
* A queue of strings
***********************************************/
Queue<String> q1 = new Queue<String>();
q1.enqueue("Vertigo");
q1.enqueue("Just Lose It");
q1.enqueue("Pieces of Me");
System.out.println(q1.dequeue());
q1.enqueue("Drop It Like It's Hot");
while (!q1.isEmpty())
System.out.println(q1.dequeue());
System.out.println();
/*********************************************************
* A queue of integers. Illustrates autoboxing and auto-unboxing.
*********************************************************/
Queue<Integer> q2 = new Queue<Integer>();
for (int i = 0; i < 10; i++)
q2.enqueue(i);
// test out iterator
for (int i : q2)
System.out.print(i + " ");
System.out.println();
// test out dequeue and enqueue
while (q2.size() >= 2) {
int a = q2.dequeue();
int b = q2.dequeue();
int c = a + b;
System.out.println(c);
q2.enqueue(a + b);
}
}
}