Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  QueueDemo.java
Language: JAVA
Code:
import java.io.*;
class QueueX
{
int maxSize,qArray[],rear,front,nItems;
public QueueX(int s)
{
maxSize=s;
qArray=new int[maxSize];
rear=-1;
front=0;
nItems=0;
}
public void insert(int j)
{
qArray[++rear]=j;
nItems++;
}
public int delete()
{
int temp=qArray[front++];
nItems--;
return temp;
}
public boolean isEmpty()
{
return(nItems==0);
}
public int  Size()
{
return nItems;
}
public void display()
{
for(int i=front;i<=rear;i++)
{
System.out.println(qArray[i]);
}
}
public boolean isFull()
{
return(nItems==maxSize);
}
}
class QueueDemo
{
public static void main(String args[])
{
try
{
QueueX q=new QueueX(5);
System.out.println("Enter how many numbers you want to insert 1 to 5");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
if(q.isFull())
{
System.out.println("Queue is full");
}
else
{
System.out.println("Enter  a number");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
int s=Integer.parseInt(br1.readLine());
q.insert(s);
}
}
System.out.println("Display");
if(q.isEmpty())
{
System.out.println("Queue is empty");
}
else
{
q.display();
}
System.out.println("Deleted Elements");
while(!q.isEmpty())
{
int p=q.delete();
System.out.println(p);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Comments: