This program will deal with queue.
A data structure based on FIFO principle which states that deletion will occur at front(where array starts 0) and insertion will occur at rear end(where array ends at index length-1).
With every insertion rear becomes rear+1.
And with every deletion front becomes front+1.
class queue
{
public static int q[];// queue
public static int size;// length
public static int rear;
public static int front;
public queue(int n)
{
q=new int[n];
size=n;
front=rear=0;
}
public static void enqueue(int num)
{
if(rear!=size)
{
q[rear]=num;
rear=rear+1;}
else
System.out.println("QUEUE FULL");
}
public static void dequeue()
{
if(front!=rear){
System.out.println("removed "+(q[front]));
front=front+1;}
else {
System.out.println("UNDER FLOW");
}
}
public static void show()
{ System.out.println("queue is");
for(int i=front;i<rear;i++)
System.out.println(q[i]+" ");
}
}
class que2
{
public static void main(String args[])
{
queue Queue1=new queue(5);//size of array
Queue1.enqueue(2);
Queue1.enqueue(3);
Queue1.enqueue(4);
Queue1.enqueue(8);
Queue1.enqueue(9);
Queue1.show();
Queue1.dequeue();
Queue1.show();
}
}
No comments:
Post a Comment