import java.io.*;
import java.lang.*;
import java.util.*;
class queue
{
int front = -1;
int stk[];
int max;
queue(int size)
{
max=size;
stk=new int[max];
}
void insert(int item)
{
if(front==max-1)
{
System.out.println("queue overflow");
}
else
{
front=front+1;
stk[front]=item;
display();
}
}
public boolean queueoverflow()
{
if(front==max)
return true;
else
return false;
}
boolean isempty()
{
if(front== -1)
return true;
else
return false;
}
public void remove()
{
if(isempty())
{
System.out.println("queue Underflow...");
}
else
{
front=front-1;
System.out.println("Element is Deleted..");
display();
}
}
void queuefront()
{
if (isempty())
System.out.println("queue underflow..");
else
System.out.println("queue front is" +(stk[front]));
}
void display()
{
if (isempty())
System.out.println("queue underflow..");
else
{
System.out.println("queue--->");
for(int i=front; i>=0 ;i--)
System.out.println(stk[i]);
}
}
int peek()
{
if(isempty())
{
System.out.println("queue Empty...");
return 0;
}
else
{
System.out.println("The front most element is: " +stk[front]);
return stk[front];
}
}
public void search(int item)
{
if(isempty())
{
System.out.println("queue Empty...");
}
else
{
for(int i=front; i>=0 ;i--)
{
if(stk[i]==item)
System.out.println("Element found..");
else
System.out.println("Element not found..");
}
}
}
}
class Q1
{
public static void main(String[] args)
{
int s,el,ch;
Scanner src=new Scanner(System.in);
System.out.println("\n\n\t\tQUEUE IMPLEMENTATION USING ARRAY");
System.out.println("\n\t======================================================");
System.out.println("\n\n\t1.Insert Element...");
System.out.println("\n\t2.Remove Element...");
System.out.println("\n\t3.Display Queue...");
System.out.println("\n\t4.Peek...");
System.out.println("\n\t5.Search Element...");
System.out.println("\n\t6.Exit...");
System.out.println("\n\n\tEnter the size of Array: ");
s=src.nextInt();
queue p=new queue(s);
while(true)
{
System.out.println("\n\tEnter the input : ");
ch=src.nextInt();
switch(ch)
{
case 1:
System.out.println("\n\tEnter the element : ");
el=src.nextInt();
p.insert(el);
break;
case 2: p.remove();
break;
case 3:p.display();
break;
case 4:
p.peek();
break;
case 5:
System.out.println("\n\tEnter the element : ");
el=src.nextInt();
p.search(el);
break;
}
if(ch==6)
break;
}
}
}
No comments:
Post a Comment