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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package rd222dv_assign2.Queues;

import java.util.Iterator;
import java.util.NoSuchElementException;
/**

 The {@code LinkedQueue} class represents a first-in-first-out (FIFO)

 *  queue of generic items.

 *  It has the <em>enqueue</em> and <em>dequeue</em>

 *  methods, together with other methods, such as first and last, looking at the first and last Object, and 

 *  testing if the queue is empty, iterating through the Objects in first in first out order

 *   and a toString method for a nice String output. 

 *  

 *  The <em>enqueue</em>, <em>dequeue</em>, <em>first</em>, <em>last</em>, <em>size</em>, and <em>is-empty</em>

 *  operations all take constant time in the worst case.

 * <ul>

 * <li>  {@link rd222dv_assign2.Queues.LinkedQueue} </li>

 * </ul>

 *

 * @author	Ruth Dirnfeld

 * @version 1.0

 * @since 2017-02-05

 */

public class LinkedQueue implements Queue {

	// Fields
	private int size = 0;
	private Node head = null;
	private Node tail = null;

	/**

	 * This constructs an empty linked queue.

	 */

	public LinkedQueue() {

	}

	/**

	 * This is a inner Node class which helps to reference the element in the

	 * queue.

	 */

	private class Node {

		// Fields
		private Object data;
		private Node next;

	}

	/**

	 * Return the size of queue

	 */

	@Override
	public int size() {
		return size;
	}

	/**

	 * Return true if queue is empty,Otherwise false.

	 */

	@Override
	public boolean isEmpty() {
		return head == null;
	}

	/**

	 * Insert Object at last position of the queue.

	 * 

	 * @param element  Object to be added

	 * 

	 *            

	 */

	@Override
	public void enqueue(Object element) {

		/*

		 * If queue is empty, insert the element at the beginning. That element

		 * will be the first and last element of the queue. If not, put the

		 * element next to the tail node and change the tail node position which

		 * represent this new element. Increase the size in both cases.

		 */

		if (isEmpty() == true) {
			Node newNode = new Node();
			newNode.data = element;
			head = newNode;
			tail = head;

		} else {
			Node newNode = new Node();
			newNode.data = element;
			tail.next = newNode;
			tail = tail.next;
		}
		size++;
	}

	/**

	 * This method returns and removes the Object at position first in the

	 * queue.

	 * 

	 * If the queue is empty, this methods throws NoSuchElementException.

	 */

	@Override
	public Object dequeue() {

		/*

		 * If empty, throw exception. Otherwise, remove and return the first

		 * element and change the head node next to this element. Decrease the

		 * size.

		 */

		if (isEmpty() == true) {
			throw new NoSuchElementException();
		}

		Object element = head.data;
		head = head.next;
		size--;
		return element;
	}

	/**

	 * This method returns the Object at position first in the queue.

	 * 

	 * If the queue is empty, this methods throws NoSuchElementException.

	 */

	/*

	 * I tried to write the throws like

	 * 

	 * @throws NoSuchElementException, but the console gives error. Then I

	 * searched online http://www.javapractices.com/topic/TopicAction.do?Id=171

	 * and I read that I must avoid throws in java doc because it generates it

	 * automatically. Please suggest me if this is not true.

	 */

	@Override
	public Object first() {

		/*

		 * If empty, throw exception. Otherwise, return the first element.

		 */

		if (head == null) {
			throw new NoSuchElementException();
		} else
			return head.data;
	}

	/**

	 * This method returns the Object at position last in the queue.

	 * 

	 * If the queue is empty, this methods throws NoSuchElementException.

	 */

	@Override
	public Object last() {

		/*

		 * If empty, throw exception. Otherwise, return the last element.

		 */

		if (tail == null) {
			throw new NoSuchElementException();
		} else
			return tail.data;
	}

	/**

	 * Represent all the elements in the queue.

	 * 

	 * @return String 

	 * 

	 */

	@Override
	public String toString() {

		/*

		 * Add all the element from head to tail in the string builder. If

		 * empty, return empty queue.

		 * 

		 */

		StringBuilder str = new StringBuilder();
		Node newNode = new Node();
		newNode = head;
		str.append("[");
		while (newNode != null) {
			str.append(" " + newNode.data);
			newNode = newNode.next;
		}
		str.append(" ]");
		return str.toString();
	}

	/**

	 * Runs over all the element present in the queue.

	 * 

	 * @return Iterator

	 */

	@Override
	public Iterator iterator() {
		return new LinkedQueueIterator();
	}

	// Inner iterator class
	private class LinkedQueueIterator implements Iterator {

		// field
		private Node position;

		// constructor
		public LinkedQueueIterator() {
			position = null;

		}

		/**

		 * Return true until the iterator does reached up to last Object of the

		 * queue.

		 * 

		 * @return boolean

		 */

		@Override
		public boolean hasNext() {

			/*

			 * In the beginning, position is null, so this method will return

			 * true only if queue is not empty, otherwise false. In other case,

			 * position is increasing by 1, this method will return true until

			 * position is not reached up to the tail.

			 */

			if (position == null) {
				return head != null;
			} else
				return position.next != null;

		}

		/**

		 * This method return Object until the hasnNext() method is true.

		 * 

		 * @return Object

		 */

		@Override
		public Object next() {

			/*

			 * Throw error if iterator already runs over all the elements in the

			 * queue. In the first case, this method will only run if above

			 * method is true. In the last case, it will return elements until

			 * the position will not reached up to the tail.

			 */

			if (!hasNext()) {
				throw new NoSuchElementException();
			}
			if (position == null) { //
				position = head;
			} else {
				position = position.next;
			}
			return position.data;

		}

	}

}
/*

public class LinkedQueue implements Queue {



	private class Node {

		public Object data;

		public Node next;



		public Node(Object data, Node next) {

			this.data = data;

			this.next = next;

		}

	}



	private Node head = null;

	private Node tail = null;

	

	/**

	 * Constructs a LinkedQueue with no detail message.

	 * Elements can be enqueued and dequeued to the LinkedQueue. 

	 */
/*	public LinkedQueue(){}

	/**

     * Returns the number of items in this queue.

     * Returns the number of items in this queue

     */
/*	@Override

	public int size() { // current queue size

		int count = 0;

		for (Node node = head; node != null; node = node.next) {

			count++;

		}

		return count;

	}

	

	/**

	 * Returns true if this queue is empty;

	 * 

	 * @return true if this queue is empty; otherwise false

	 */
/*	@Override

	public boolean isEmpty() { // true if queue is empty

		return head == null;

	}

	/**

     * Adds the element to this queue.

     * adds element at end of queue

     */
/*	@Override

	public void enqueue(Object element) { // add element at end of queue

		Node newNode = new Node(element, null);

		if (isEmpty()) {

			head = newNode;

		} else {

			tail.next = newNode;

		}

		tail = newNode;

	}

	 /**

     * Removes and returns the element from this queue that was recently added.

     * @return the element from this queue that was recently added

     * @throws java.util.NoSuchElementException if this queue is empty

     */
/*	@Override

	public Object dequeue() { // return and remove first element.

		if (isEmpty()) {

			throw new NoSuchElementException("Is Empty");

		}

		Object element = head.data;

		if (tail == head) {

			tail = null;

		}

		head = head.next;

		return element;

	}

	/**

     * Returns the Object recently added to this queue.

     * @return the Object recently added to this queue

     */
/*	@Override

	public Object first() { // return (without removing) first element

		if (head == null) {

			throw new NoSuchElementException();

		} else {

			return head.data;

		}

	}

	/**

     * Returns (without removing) last element from this queue.

     * @return (without removing) last element from this queue.

     */
/*	@Override

	public Object last() { // return (without removing) last element

		if (tail == null) {

			throw new NoSuchElementException();

		} else {

			return tail.data;

		}

	}

	/**

     * Returns an iterator that iterates over the Objects in first in first out order 

     * @return an iterator that iterates over the Objects in first in first out order 

     */
/*	@Override

	public Iterator<Object> iterator() { // element iterator

		return new QueueIterator();

	}

	/**

     * Returns a nice string output of this queue.

     * @return the Objects in FIFO order, separated by commas

     */
/*	public String toString() { // return a string representation of the queue

								// content

		if (isEmpty()) {

			return "";

		}



		String result = "<";

		Node current = head;

		while (current != null) {

			result += current.data;

			current = current.next;



			if (current != null) {

				result += ",";

			}

		}

		return result + ">";

	}

	

	class QueueIterator implements Iterator<Object> {

		private Node node = head; // First node is head



		public Object next() {

			Object val = node.data; // Read current value

			node = node.next; // Move one step ahead

			return val;

		}



		@Override

		public boolean hasNext() {

			return node != null;

		}

	}

}*/