Queue :
A queue is a linear collection, where something to be added to the queue must be placed at the end of the queue. Items are removed from the queue from the front. To keep with the analogy, when you walk into the bank you go to the end of the queue. When the teller is ready to help the next person, the person at the beginning of the queue is removed from the queue. Each person then moves one position closer to the beginning of the queue. Eventually, you will be the first person in the queue and the teller will call you up causing you to be removed from the queue.A queue is referred to as a FIFO data structure: First In, First Out.
Operations on Queue :
enqueue : Add an element to the end of the queue.
dequeue : Remove the element from the front of the queue
peek : Retrieve an item at front of queue.
isEmpty : Determines whether the Queue is empty or not.
isFull : Determines whether the queue is full or not.
Applications :
- For implementing any "natural" FIFO service, like telephone enquiries, reservation requests, traffic flow, etc.
- For implementing any "computational" FIFO service, for instance, to access some resources. Examples: printer queues, disk queues, etc.
- For searching in special data structures (breadth-first search in graphs and trees).
- For handling scheduling of processes in a multitasking operating system.
Program :
package adsa;
import java.util.Scanner;
interface LQueADT{
void insert() throws Exception;
void delete();
void display();
}
public class QueueArr implements LQueADT {
int q[],size;
int front,rear;
Scanner s=new Scanner(System.in);
public QueueArr() throws Exception {
System.out.println("Enter the size of the Linear Queue :");
size=s.nextInt();
q=new int[size];
front=-1;rear=-1;
}
public void insert() throws Exception {
if(rear==q.length-1){
System.out.println("Linear Queue overflow");
}
else{
System.out.println("Enter the element into the queue :");
int ele=s.nextInt();
rear++;
q[rear]=ele;
if(front==-1) front=0;
}
}
public void delete() {
if(front==-1 || front>rear){
System.out.println("Linear Queue underflow");
}
else{
System.out.println("Deleted Element is :"+q[front]);
front++;
}
}
public void display() {
if(front==-1 || front>rear){
System.out.println("Linear Queue is Empty");
}
else{
System.out.println("Linear Queue elements are....");
for(int i=front;i<=rear;i++)
System.out.println(""+q[i]);
}
}
public static void main(String args[]) throws Exception{
System.out.println("Linear Queue Array ADT ");
Scanner s=new Scanner(System.in);
QueueArr qa=new QueueArr();
String ch="y";
while(ch.equalsIgnoreCase("y")){
System.out.println("Linear Queue operations :\n1. Insert 2. Delete 3. Display 4. Exit\nEnter the option :");
int opt=s.nextInt();
switch(opt){
case 1:qa.insert();break;
case 2:qa.delete();break;
case 3:qa.display();break;
case 4:System.exit(0);
default : System.out.println("Invalid Option");
}
System.out.println("Do you want to continue (y/N):");
ch=s.next();
}
}
}
No comments:
Post a Comment