Make the frequent cases fast and the rare case correct

Queue implementation using a Linked List

A queue is First in First out data structure. We define two methods of a linked list i.e to insert a node at the end of the list and then deleting the node from the beginning of the list.

When an element is inserted into the list it is further away from the start node. a new node becomes the start node if the list is empty.An element is deleted from the front of the list.

class Node{
	public int item;
	public Node next;
	public Node(int val){ 
		item = val; 
	public void displayNode(){ 
		System.out.print("[" + item + "]"); 
class LinkedList{
	private Node start;
	private Node end;
	public LinkedList(){
		start = null;
		end = null;
	public boolean isEmpty(){ 
		return start==null; 
	public void insertEnd(int val){//Insert node at the end of list
		Node newNode = new Node(val);
		if( isEmpty() )
			start = newNode;
		else = newNode;
		end = newNode;
	public int deleteStart(){//delete the node from the beginning of the list
		int temp = start.item;
		if( == null)
			end = null;
		start =;
		return temp;
	public void displayList(){
		Node current = start;
		while(current != null)
			current =;
class Queue{
	private LinkedList listObj;
	public Queue(){
		listObj = new LinkedList(); 
	public boolean isEmpty(){ 
		return listObj.isEmpty(); 
	public void insert(int k){ 
	public int delete(){ 
		return listObj.deleteStart(); 
	public void display(){
		System.out.print("Queue [start to end]: ");
class ListQueueBlog{
	public static void main(String[] args){	
		Queue demo = new Queue();
		System.out.println("Inserting two elements into the queue");
		System.out.println("Inserting one more element into the queue at the end");
		System.out.println("Deleting one element from the front");


~/ListQueue$ java ListQueueBlog
Inserting two elements into the queue
Queue [start to end]: [10][20]
Inserting one more element into the queue at the end
Queue [start to end]: [10][20][30]
Deleting one element from the front
Queue [start to end]: [20][30]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: