Queues in Java

import java.util.ArrayList;
import java.util.Scanner;

/**
*
*/

/**
* @author asharda
*
*/
public class queuetest {

/**
* @param args
*/
static Scanner in=new Scanner(System.in);
static ArrayList<Integer> list=new ArrayList<Integer>();
static int elements,choice;
public static void main(String[] args) {
// TODO Auto-generated method stub

do
{
System.out.println(“Enter your choice”);
System.out.println(“1: Enqueue”);
System.out.println(“2: Dequeue”);
System.out.println(“3: Display”);
System.out.println(“4: Exit”);
choice=in.nextInt();
switch(choice)
{
case 1:
enqueue();
break;

case 2:
dequeue();
break;

case 3:
display();
break;

case 4:
System.out.println(“Exiting”);
System.exit(0);
}

}while(choice!=4);

}

public static void enqueue()
{
System.out.println(“Enter number of elements you want to add to Queue”);
elements=in.nextInt();
int temp;
System.out.println(“Enter the elements”);
for(int i=0;i<elements;i++)
{
temp=in.nextInt();
list.add(temp);
}
}

public static void dequeue()
{
if(!(list.isEmpty()))
{
list.remove(list.get(0));
}
else
System.out.println(“Queue is Empty”);
}

public static void display()
{
System.out.println(“Elements of the Queue are”);
if(!(list.isEmpty()))
{
for(int i:list)
System.out.println(i);
}
else
System.out.println(“Queue is Empty”);
}
}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s