QueueApp.java

import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class QueueX
{
private int maxSize;
private double queueArr[];
private int top;
public QueueX(int max)
{
maxSize = max;
queueArr = new double[maxSize];
top = -1;
}
public void insert(double number)
{
queueArr[++top] = number;
}
public double remove()
{
double removeItem = queueArr[0];
for(int i = 0;i<top;i++)
{
queueArr[i] = queueArr[i+1];
}
top- -;
return removeItem;
}
public double peak()
{
return queueArr[0];
}
public boolean isEmpty()
{
return(top == -1);
}
public boolean isFull()
{
return(top == (maxSize-1));
}
public void display()
{
System.out.println(“The queue are: “);
for(int i=0;i<= top;i++)
{
System.out.print(queueArr[i]+” “);
}
System.out.println(“\n”);
}
}
class QueueApp
{
public static void main(String[] args)
{
QueueX theQueue = new QueueX(5); // queue holds 5 items
theQueue.insert(10); // insert 4 items
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
System.out.println(“After inserting items:”);
theQueue.display();
theQueue.remove(); // remove 3 items
theQueue.remove(); // (10, 20, 30)
theQueue.remove();
System.out.println(“After removing items:”);
theQueue.display();
theQueue.insert(50); // insert 4 more items
theQueue.insert(60); // (wraps around)
theQueue.insert(70);
theQueue.insert(80);
System.out.println(“After inserting items again:”);
theQueue.display();
System.out.println(“The removed items:”);
while( !theQueue.isEmpty() ) // remove and display
{ // all items
double n = theQueue.remove(); // (40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(” “);
}
System.out.println(“”);
}
}

The result is as the following image:

resultHappy queuing 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s