/Queue Using LinkedList

Queue Using LinkedList

Queue Using LinkedList

Node Class

public class Node {
    private int data;
    private Node next;
    
    public Node(int data,Node next)
    {
        this.data=data;
        this.next=next;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Node{" + "data=" + data + ", next=" + next + '}';
    }
}

 

Queue Class

public class Queue {

    private Node head;
    private Node tail;

    public Queue() {
        this.head = null;
        this.tail = null;
    }

    public void enque(int data) {
        if (head == null) {
            head = new Node(data, null);
            tail = head;
        } else {
            Node newnode = new Node(data, null);
            tail.setNext(newnode);
            tail = newnode;
        }
    }

    public void deque() {
        head = head.getNext();
    }

    public void printQueue() {
        System.out.println(head);
    }
}

Queue Example

public class QueueExample {
    public static void main(String args[])
    {
        Queue queue=new Queue();
        queue.enque(10);
        queue.enque(20);
        queue.enque(30);
        queue.enque(40);
        
        queue.printQueue();
        queue.deque();
        queue.printQueue();
    }
}