Queue Algorithm:
A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stack and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Operations on Queue:
Mainly the following four basic operations are performed on queue:
Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.
Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.
Front: Get the front item from the queue.
Rear: Get the last item from the queue.
A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stack and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Operations on Queue:
Mainly the following four basic operations are performed on queue:
Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.
Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.
Front: Get the front item from the queue.
Rear: Get the last item from the queue.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Algorithm; /** * * @author RRRR */ public class Queue { private int arra[]; //array to store queue element; private int front; // point the top element of queue private int rear; // poit to the last element of queue private int capacity; //maximum capacity of the queue private int count; // current size of the queue //Constructor Initialize the queue value Queue(int x){ arra=new int[200]; capacity=x; front=0; rear=-1; count=0; } public void enqueue(int x){ // Check Queue is full or not if(isFull()){ System.out.println("Queue is Overflow\nProgram Terminated"); System.exit(1); } System.out.println("Insert value to Queue "+x); rear=(rear+1)%capacity; arra[rear]=x; count++; } public int dequeue(){ if(isEmpty()){ System.out.println("Queue is Underflow\nProgram is terminated"); System.exit(1); } System.out.println("Removing The element "+arra[front]); front=(front+1)%capacity; count--; return arra[front]; } public int peek(){ if(isEmpty()){ System.out.println("Queue is Underflow\nProgram Terminated"); System.exit(1); } return arra[front]; } // Check Queue is full or not public boolean isFull(){ return (size()==capacity); } // Check Queue is Empty or not public boolean isEmpty(){ return (size()==0); } // Size of the current Queue public int size(){ return count; } public static void main(String []args){ Queue queue=new Queue(5); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); System.out.println("Front Element of Queue "+queue.peek()); queue.dequeue(); System.out.println("Front Element of Queue "+queue.peek()); System.out.println("Queue size "+queue.size()); queue.dequeue(); queue.dequeue(); if(queue.isEmpty()) System.out.println("Queue is Empty"); else System.out.println("Queue is not Empty"); } }
Output:
Insert value to Queue 1
Insert value to Queue 2
Insert value to Queue 3
Front Element of Queue 1
Removing The element 1
Front Element of Queue 2
Queue size 2
Removing The element 2
Removing The element 3
Queue is Empty
বিস্তারিত জানতে পড়ুন
সোর্স কোড
0 Comments