Website Design United States, Website Design California, Website Designing United States, Website Designing California

Queue Data Structure Class - Java

This Code is a Queue Data Structure Class which can be used by other programs in Java.

/*

QUEUE DATA STRUCTURE
-Sanchit Karve
born2c0de@hotmail.com

A Queue Data Structure works on two ends.....the appending end and the
deletion end.Unlike the STACK Data Structure which adds and deletes
data from one end, the Queue Adds data from one end and deletes it from
the other.Hence the Queue Data Structure is also known as:
FIFO--> First In , First Out.
*/

// Save this file as "Queue.java"
import javax.swing.JOptionPane;

class Queue
{
private int MAX=10; // MAXIMUM Contents of the Queue
private int arr[]; // The Array where Data will be stored
private int al; // The End where Data is Added
private int dl; // The End where Data is Deleted

private String qf="Queue is Full . Cannot Store Data in Queue Beyond it's MAXimum Limit.";
private String qe="Queue is Empty. Cannot Delete Non-existing Data.";

public Queue()
{
arr=new int[MAX];
al=-1;
dl=-1;
}

public void add(int data)
{
if(dl==-1 && al==-1)
{
al++;
dl++;
}
else
{
al++;
}
if(al>=MAX)
{
JOptionPane.showMessageDialog(null,qf,"WARNING!!!",JOptionPane.WARNING_MESSAGE);
al--;
}
else
{
arr[al]=data;
}

}

public int del()
{
int deld=0;
int tmp;
if(dl==-1)
{
JOptionPane.showMessageDialog(null,qe,"ERROR!!!",JOptionPane.ERROR_MESSAGE);
return -1;
}
else
{
int j;
deld=arr[dl];
for(j=0;j<=al;j++)
{
if((j+1)<=al)
{
tmp=arr[j+1];
arr[j]=tmp;
}
else
{
al--;
dl=(al==-1) ? -1 : 0;
}
}
}
return deld;
}
}

WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com