Google+ Followers

Connect

Get Updates Via Email

Enter your email address:

Delivered by FeedBurner

Friday, 15 November 2013

Data Structure and Algorithms : Queue Program in Java

Program:

public class queuepgm {
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;

public queuepgm(int s)

{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}

public void enqueue(long j)

{
if(rear == maxSize-1)
rear = -1;
queArray[++rear] = j;
nItems++;
}

public long dequeue()
{
long temp = queArray[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public long peekFront()

{
    return queArray[front];
}
public boolean isEmpty()      
{
return (nItems==0);
}
public boolean isFull()
{
return (nItems==maxSize);
}
public int size()
{
return nItems;
}
public static void main(String[] args)
{
queuepgm theQueue = new queuepgm(5); // queue holds 5 items
theQueue.enqueue(10);
theQueue.enqueue(20);
theQueue.enqueue(30);
theQueue.enqueue(40);
System.out.println("Dequeue Values: FIFO");
while( !theQueue.isEmpty() )
{
long n = theQueue.dequeue();
System.out.print(n+"\n");

}
}

}

Output:


1 comment :

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

    Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

    ReplyDelete

Share this to your friends