import java.util.Iterator;
public class Deque<Item> implements Iterable<Item> {
private int N;
private Node first;
private Node last;
private class Node {
private Item node;
private Node next;
private Node past;
}
public Deque() {
first = null;
last = null;
N = 0;
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
public void addFirst(Item item) {
if(item == null) throw new java.lang.IllegalArgumentException();
Node temp = first;
first = new Node();
first.node = item;
first.next = temp;
if (isEmpty())
last = first;
else temp.past = first;
N++;
}
public void addLast(Item item) {
if(item == null) throw new java.lang.IllegalArgumentException();
Node temp = last;
last = new Node();
last.node = item;
last.past = temp;
if (isEmpty())
first = last;
else temp.next = last;
N++;
}
public Item removeFirst() {
if(isEmpty())
throw new java.util.NoSuchElementException("Queue underflow");
Item temp = first.node;
first = first.next;
N--;
if(isEmpty())
last=null;
else first.past = null;
return temp;
}
public Item removeLast() {
if(isEmpty())
throw new java.util.NoSuchElementException("Queue underflow");
Item temp = last.node;
last = last.past;
N--;
if(isEmpty())
first=null;
else last.next = null;
return temp;
}
public Iterator<Item> iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() {
return current != null;
}
public Item next() {
if(!hasNext())
throw new java.util.NoSuchElementException();
Item item = current.node;
current = current.next;
return item;
}
public void remove() {
throw new java.lang.UnsupportedOperationException();
}
}
}
import edu.princeton.cs.algs4.StdRandom;
import java.util.Iterator;
import edu.princeton.cs.algs4.StdOut;
public class RandomizedQueue<Item> implements Iterable<Item> {
private int head, tail;
private Item[] queue;
private int N;
public RandomizedQueue() {
queue = (Item[]) new Object[1];
head = 0;
tail = 0;
N = 0;
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
private void resize(int max) {
Item[] temp = (Item[]) new Object[max];
for(int i = 0; i < N; i++)
temp[i] = queue[(head + i) % queue.length];
queue = temp;
head = 0;
tail = N;
}
public void enqueue(Item item) {
if(item == null)
throw new java.lang.IllegalArgumentException();
if(N == queue.length)
resize(2*queue.length);
queue[tail++] = item;
if (tail == queue.length) tail = 0;
N++;
}
public Item dequeue() {
if (isEmpty())
throw new java.util.NoSuchElementException();
int index = (head + StdRandom.uniform(N)) % queue.length;
Item item = queue[index];
if (tail == 0) {
tail = queue.length - 1;
queue[index] = queue[tail];
queue[tail] = null;
} else {
queue[index] = queue[--tail];
queue[tail] = null;
}
N--;
if(N>0 && N==queue.length/4) resize(queue.length/2);
return item;
}
public Item sample() {
if (isEmpty())
throw new java.util.NoSuchElementException();
int index = (head + StdRandom.uniform(N)) % queue.length;
return queue[index];
}
public Iterator<Item> iterator() {
return new RQIterator();
}
private class RQIterator implements Iterator<Item> {
private int index = 0;
private Item[] r;
public RQIterator() {
r = (Item[]) new Object[N];
for(int i=0; i<N; i++)
r[i] = queue[i];
StdRandom.shuffle(r);
}
public boolean hasNext() {
return index < N;
}
public void remove() {
throw new java.lang.UnsupportedOperationException();
}
public Item next() {
if(!hasNext()) throw new java.util.NoSuchElementException();
Item item = r[index++];
return item;
}
}
}
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class Permutation {
public static void main(String[] args) {
RandomizedQueue<String> queue = new RandomizedQueue<String>();
int num = Integer.parseInt(args[0]);
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
queue.enqueue(item);
}
for (String s : queue) {
if(num == 0)
break;
StdOut.println(s);
num--;
}
}
}