-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedQueue.java
39 lines (30 loc) · 1.19 KB
/
LinkedQueue.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
/*
Implementing a Queue with a Singly Linked List
enqueue(e): Adds element e to the back of queue.
dequeue( ): Removes and returns the first element from the queue (or null if the queue is empty).
The queue ADT also includes the following accessor methods (with first being analogous to the stack’s top method):
first( ): Returns the first element of the queue, without removing it (or null if the queue is empty).
size( ): Returns the number of elements in the queue.
isEmpty( ): Returns a boolean indicating whether the queue is empty.
last update 2022 Dec 26
*/
/** Realization of a FIFO queue as an adaptation of a SinglyLinkedList. */
public class LinkedQueue<E> implements IQueue<E> {
private SinglyLinkedList<E> list = new SinglyLinkedList<>( ); // an empty list
public LinkedQueue( ) { } // new queue relies on the initially empty list
public int size( ) {
return list.size( );
}
public boolean isEmpty( ) {
return list.isEmpty( );
}
public void enqueue(E element) {
list.addLast(element);
}
public E first( ) {
return list.first( );
}
public E dequeue( ) {
return list.removeFirst( );
}
}