class Queue
{
/*array is used to hold queue elements*/
/*two integer variables are used for front and rear pointers*/
int items[]=new int[10];
int front,rear;
Queue()
{
front=1;
rear=0;
}
int empty()
{
if((front==1)&&(rear==0) ||(front>rear))
return 1;
else
return 0;
}
void display( )
{
/* function to display queue elements */
int i=front;
if(empty()==1)
System.out.println("Q is empty ");
else
{
System.out.print("Front-->");
while(i<=rear)
{
System.out.print(" "+items[i]);
i++;
}
System.out.println("<--Rear");
}
}
void add(int x)
{
/*function to add new element*/
System.out.println("\nInsert: " + x);
if(rear==9)
System.out.println("Queue is Full");
else
{
rear++;
items[rear]=x;
display();
}
}
void delet( )
{
/* Function to delete the largest number */
int large,i,j,k;
if(empty()==1)
System.out.println("Q is empty ");
else
{
i=front+1;
large=items[front];
k=front;
while(i<=rear)
{
if (items[i]>large)
{
large=items[i];
k=i;
}
i++;
}
System.out.println("\nNumber being deleted is : "+items[k]);
if(k==front)
/*if first element is deleted increment front pointer*/
front++;
else
{
/*if middle element or last element is deleted decrement rear pointer*/
/*copy all elements forward*/
for(i=k;i<rear;i++)
items[i]=items[i+1];
rear--;
}
display();
}
}
}
class QPriority
{
public static void main(String args[])
{
Queue q=new Queue();
int i,j;
System.out.println("Starting...");
for(i=0;i<10;i++)
{
j = new Integer((int)(Math.random() * 100));
q.add(j);
}
while(q.empty()==0)
{
q.delet();
}
System.out.println("Done ;-)");
}
}
/*
Sample Input and Output :
Starting...
Insert: 60
Front--> 60<--Rear
Insert: 66
Front--> 60 66<--Rear
Insert: 40
Front--> 60 66 40<--Rear
Insert: 22
Front--> 60 66 40 22<--Rear
Insert: 36
Front--> 60 66 40 22 36<--Rear
Insert: 49
Front--> 60 66 40 22 36 49<--Rear
Insert: 51
Front--> 60 66 40 22 36 49 51<--Rear
Insert: 66
Front--> 60 66 40 22 36 49 51 66<--Rear
Insert: 68
Front--> 60 66 40 22 36 49 51 66 68<--Rear
Insert: 87
Queue is Full
Number being deleted is : 68
Front--> 60 66 40 22 36 49 51 66<--Rear
Number being deleted is : 66
Front--> 60 40 22 36 49 51 66<--Rear
Number being deleted is : 66
Front--> 60 40 22 36 49 51<--Rear
Number being deleted is : 60
Front--> 40 22 36 49 51<--Rear
Number being deleted is : 51
Front--> 40 22 36 49<--Rear
Number being deleted is : 49
Front--> 40 22 36<--Rear
Number being deleted is : 40
Front--> 22 36<--Rear
Number being deleted is : 36
Front--> 22<--Rear
Number being deleted is : 22
Q is empty
Done ;-)
{
/*array is used to hold queue elements*/
/*two integer variables are used for front and rear pointers*/
int items[]=new int[10];
int front,rear;
Queue()
{
front=1;
rear=0;
}
int empty()
{
if((front==1)&&(rear==0) ||(front>rear))
return 1;
else
return 0;
}
void display( )
{
/* function to display queue elements */
int i=front;
if(empty()==1)
System.out.println("Q is empty ");
else
{
System.out.print("Front-->");
while(i<=rear)
{
System.out.print(" "+items[i]);
i++;
}
System.out.println("<--Rear");
}
}
void add(int x)
{
/*function to add new element*/
System.out.println("\nInsert: " + x);
if(rear==9)
System.out.println("Queue is Full");
else
{
rear++;
items[rear]=x;
display();
}
}
void delet( )
{
/* Function to delete the largest number */
int large,i,j,k;
if(empty()==1)
System.out.println("Q is empty ");
else
{
i=front+1;
large=items[front];
k=front;
while(i<=rear)
{
if (items[i]>large)
{
large=items[i];
k=i;
}
i++;
}
System.out.println("\nNumber being deleted is : "+items[k]);
if(k==front)
/*if first element is deleted increment front pointer*/
front++;
else
{
/*if middle element or last element is deleted decrement rear pointer*/
/*copy all elements forward*/
for(i=k;i<rear;i++)
items[i]=items[i+1];
rear--;
}
display();
}
}
}
class QPriority
{
public static void main(String args[])
{
Queue q=new Queue();
int i,j;
System.out.println("Starting...");
for(i=0;i<10;i++)
{
j = new Integer((int)(Math.random() * 100));
q.add(j);
}
while(q.empty()==0)
{
q.delet();
}
System.out.println("Done ;-)");
}
}
/*
Sample Input and Output :
Starting...
Insert: 60
Front--> 60<--Rear
Insert: 66
Front--> 60 66<--Rear
Insert: 40
Front--> 60 66 40<--Rear
Insert: 22
Front--> 60 66 40 22<--Rear
Insert: 36
Front--> 60 66 40 22 36<--Rear
Insert: 49
Front--> 60 66 40 22 36 49<--Rear
Insert: 51
Front--> 60 66 40 22 36 49 51<--Rear
Insert: 66
Front--> 60 66 40 22 36 49 51 66<--Rear
Insert: 68
Front--> 60 66 40 22 36 49 51 66 68<--Rear
Insert: 87
Queue is Full
Number being deleted is : 68
Front--> 60 66 40 22 36 49 51 66<--Rear
Number being deleted is : 66
Front--> 60 40 22 36 49 51 66<--Rear
Number being deleted is : 66
Front--> 60 40 22 36 49 51<--Rear
Number being deleted is : 60
Front--> 40 22 36 49 51<--Rear
Number being deleted is : 51
Front--> 40 22 36 49<--Rear
Number being deleted is : 49
Front--> 40 22 36<--Rear
Number being deleted is : 40
Front--> 22 36<--Rear
Number being deleted is : 36
Front--> 22<--Rear
Number being deleted is : 22
Q is empty
Done ;-)
No comments:
Post a Comment