Implementing a Simple FIFO Queue in Java Using Linked List

First In First Out (FIFO) queues can be easily implemented in Java using a singly linked list. This example shows a very simple implementation of the queue operations using singly linked list.

package com.icodejava.published.datastructure;
/**
 * 
 * @author Kushal Paudyal
 * Created on: 2/8/2017
 * Last Modified on: 2/8/2017
 *
 * This class shows a simple implementation of Queue using LinkedList
 */
import java.util.LinkedList;

public class QueueUsingLinkedList {

	private LinkedList<Object> data = new LinkedList<Object>();

	public void enqueue(Object item) {
		data.addLast(item);
		System.out.println("Added: " + item);
	}

	public Object dequeue() {
		System.out.println("Removed: " + data.getFirst());
		return data.removeFirst();
		
	}

	public Object peek() {
		return data.getFirst();
	}

	public int size() {
		return data.size();
	}

	public boolean isEmpty() {
		return data.isEmpty();
	}
	
	/**
	 * Testing the stack. The added objects should be returned in same order
	 */
	public static void main (String args[]) {
		QueueUsingLinkedList queue = new QueueUsingLinkedList();
		System.out.println("===ENQUEUE===");
		queue.enqueue("One");
		queue.enqueue("Two");
		queue.enqueue("Three");
		
		System.out.println("\n===DEQUEUE===");
		while (!queue.isEmpty()) {
			queue.dequeue();
		}
	}
}

Here is the output of running the above program:

===ENQUEUE===
Added: One
Added: Two
Added: Three

===DEQUEUE===
Removed: One
Removed: Two
Removed: Three