Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Queue implementation using linkedlist in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 275
    Comment on it

    We know that linkedlist is type of Data structure using linked list we can store item of same type.

    Queue can be also be implemented using linkedlist.

    In this example we have two node that are head(Front) and trail(rear) where we can insert only at rear end and we can delete item from front end.

    Here is an example :

    public class QueueLinkedList<T> {
    
        private Node<T> front = null;
        private Node<T> rear = null;
        private int size = 0;
    
        public void insertInQueue(T value){
    
            Node<T> newQueueItem = new Node<T>();
            newQueueItem.setValue(value);
    
            if (rear == null){
                front = newQueueItem;
                rear = newQueueItem;
            }else{
                rear.setNextReference(newQueueItem);
                rear = rear.getNextReference();
            }
            size++;
    
        }
    
        public T removeFromQueue(){
    
            Node<T> temp = front;
    
            front = temp.getNextReference();
    
            if (front == null){
                rear = null;
            }
    
    
            size--;
    
            return temp.getValue();
        }
    
        public void traverseQueue(){
    
            while (front != null){
                Log.e("queue data = "," -> "+ front.getValue());
                front = front.getNextReference();
    
            }
        }
    }

     

    Node structure of linked list :

    private T value;
    private Node<T> nextReference;
    
    public T getValue() {
        return value;
    }
    
    public void setValue(T value) {
        this.value = value;
    }
    
    public Node<T> getNextReference() {
        return nextReference;
    }
    
    public void setNextReference(Node<T> nextReference) {
        this.nextReference = nextReference;
    }
    
    @Override
    public int compareTo(Object another) {
        if (another == this.value){
            return 0;
        }else{
            return 1;
        }
    }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: