Skip to content

Instantly share code, notes, and snippets.

@LXZE
Created April 3, 2013 15:46
Show Gist options
  • Save LXZE/5302388 to your computer and use it in GitHub Desktop.
Save LXZE/5302388 to your computer and use it in GitHub Desktop.
package code;
import java.util.*;
public class problem1 {
static int max = 10000000;
static Queue queue = new Queue(max);
static int count;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("input");
String[] line = {"Sheldon","Leonard","Penny","Rajesh","Howard"};
for(int i=0;i<line.length;i++)
queue.insertQ(line[i]);
count = sc.nextInt();
String temp;
for(int i=1;i<count;i++)
{
temp = queue.peekQ();
queue.insertQ(temp);
queue.insertQ(temp);
queue.removeQ();
}
System.out.println(queue.peekQ());
}
}
class Queue
{
int maxSize;
String[] queue;
int front;
int rear;
int count;
public Queue(int max)
{
maxSize = max;
queue = new String[maxSize];
front=0;
rear =-1;
count=0;
}
public boolean isEmpty() { // check queue empty
return (count==0);}
public boolean isFull() { // check queue full
return (count >= maxSize);}
void insertQ(String item) { // add item to queue
//System.out.println(item);
if (rear==maxSize-1)
rear = -1 ;
queue[++rear] = item;
count++;
}
String removeQ() { // take item from queue
String temp = queue[front++];
if (front==maxSize) front = 0;
count--;
return temp;
}
public String peekQ() { // peek value at front
return queue[front];}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment