Friday, December 16, 2011

Implementing a Queue Data Structure using JAVA

package QueueImp;

import java.util.LinkedList;

public
class Queue{

   
    private LinkedList<E> list;   

    public Queue(){       
        list = new LinkedList<E>();
    }

    public boolean isEmpty(){      // Check whether the Queue is empty
        return (list.size() == 0);
    }

    public void enqueue(Object item){      // Add element as the tail of Queue       
        list.add(item);
    }

    public Object dequeue(){        // Remove the head of Queue       
        Object item = list.get(1);       
         list.remove(1);       
         return item;
    }

    public Object peek(){       // Retrieve the head of Queue
        return list.get(1);
    }
}