题意理解
实现一个双端队列,实现一个随机队列,其中,随机队列是每次出元素是随机从队列中取出一个元素。
问题分析
双端队列利用链表实现,随机队列利用数组实现。
实现迭代器支持循环遍历。
java参数值的获取方法。
其他
链接
/* *****************************************************************************
* Name:
* Date:
* Description:
**************************************************************************** */
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private int n; // size of deque
private Node head; // head of deque
private Node tail; // tail of deque
private class Node {
private Item item;
private Node next; // next pointer
private Node prev; // previous pointer
}
public Deque() {
n = 0;
head = null;
tail = null;
}
public boolean isEmpty() {
return n == 0;
}
public int size() {
return n;
}
public void addFirst(Item item) {
if (item == null) {
throw new IllegalArgumentException();
}
if (head == null) {
head = new Node();
head.item = item;
head.next = null;
head.prev = null;
tail = head;
}
else {
Node lastHead = head;
head = new Node();
head.item = item;
head.prev = null;
head.next = lastHead;
lastHead.prev = head; // old head
}
n++;
}
public void addLast(Item item) {
if (item == null) {
throw new IllegalArgumentException();
}
if (tail == null) { // empty deque
tail = new Node();
tail.item = item;
tail.next = null;
tail.prev = null;
head = tail;
}
else {
Node lastTail = tail;
tail = new Node();
tail.item = item;
tail.next = null;
tail.prev = lastTail;
lastTail.next = tail; // old tail's next point to tail
}
n++;
}
public Item removeFirst() {
Item returnItem;
if (head == null) {
throw new NoSuchElementException("empty queue");
}
else {
Node lastHead = head;
head = head.next;
if (head == null) {
tail = null;
}
else {
head.prev = null;
}
returnItem = lastHead.item;
n--;
}
return returnItem;
}
public Item removeLast() {
Item returnItem;
if (tail == null) {
throw new NoSuchElementException("empty queue");
}
else {
Node lastTail = tail;
tail = tail.prev;
if (tail == null) {
head = null;
}
else {
tail.next = null;
}
returnItem = lastTail.item;
n--;
}
return returnItem;
}
// return an iterator over items in order from front to back
public Iterator<Item> iterator() {
return new DeqIterator();
}
private class DeqIterator implements Iterator<Item> {
private Node current = head;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Node lastCurrent = current;
current = current.next;
return lastCurrent.item;
}
}
/*
public void asString() {
for(Item myItem : this) {
System.out.print(myItem + " ");
}
System.out.println();
}
*/
public static void main(String[] args) {
Deque<String> myDeque = new Deque<String>();
// myDeque.addFirst("aaa");
// myDeque.asString();
myDeque.addLast("bbb");
// myDeque.asString();
myDeque.removeFirst();
// myDeque.asString();
// myDeque.removeLast();
if (myDeque.isEmpty()) {
System.out.println("good job");
}
}
}
/* *****************************************************************************
* Name:
* Date:
* Description:
**************************************************************************** */
import edu.princeton.cs.algs4.StdRandom;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class RandomizedQueue<Item> implements Iterable<Item> {
private int n; // size of n
private Item[] array; // head of queue;
public RandomizedQueue() {
array = (Item[]) new Object[2];
n = 0;
}
public boolean isEmpty() {
return n == 0;
}
public int size() {
return n;
}
private void resize(int length) {
Item[] temp = (Item[]) new Object[length];
for (int i = 0; i < n; i++) {
temp[i] = array[i];
}
array = temp;
}
private void randomSwap() {
if (n <= 1) {
return;
}
int random = StdRandom.uniform(n);
Item temp = array[random];
array[random] = array[n-1];
array[n-1] = temp;
}
public void enqueue(Item item) {
if (item == null) {
throw new IllegalArgumentException();
}
if (n == array.length) {
resize(2*array.length);
}
array[n] = item;
n++;
}
// remove and return a random item
public Item dequeue() {
if (n == 0) {
throw new NoSuchElementException();
}
randomSwap();
Item item = array[n - 1];
array[n - 1] = null;
n--;
if (n > 0 && n == array.length / 4) {
resize(array.length / 2);
}
return item;
}
// return a random item (but do not remove it)
public Item sample() {
if (n == 0) {
throw new NoSuchElementException();
}
randomSwap();
return array[n - 1];
}
public Iterator<Item> iterator() {
return new RandomIterator();
}
private class RandomIterator implements Iterator<Item> {
private final int[] index;
private int current;
public RandomIterator() {
index = new int[n];
for (int i = 0; i < n; i++) {
index[i] = i;
}
for (int i = n - 1; i >= 0; i--) {
int random = StdRandom.uniform(i+1);
int temp = index[random];
index[random] = index[i];
index[i] = temp;
}
current = n - 1;
}
public boolean hasNext() {
return current >= 0;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Item item = array[index[current]];
current--;
return item;
}
}
/*
public void asString() {
Node currentNode = head;
while (currentNode != null) {
System.out.print(currentNode.item + " ");
currentNode = currentNode.next;
}
System.out.println();
}
*/
public static void main(String[] args) {
RandomizedQueue<String> queue = new RandomizedQueue<String>();
queue.enqueue("www");
queue.enqueue("xxx");
queue.enqueue("yyy");
// queue.asString();
queue.dequeue();
// queue.asString();
}
}
/* *****************************************************************************
* Name:
* Date:
* Description:
**************************************************************************** */
import edu.princeton.cs.algs4.StdIn;
public class Permutation {
/*
public static void main1(String[] args) {
int k = StdIn.readInt();
Deque<String> deque = new Deque<String>();
while (!StdIn.isEmpty()) {
String str = StdIn.readString();
deque.addFirst(str);
}
// deque.asString();
if (k == 0) {
System.out.println();
}
else {
for (int i = 0; i < k; i++) {
System.out.print(deque.removeFirst() + ' ');
}
System.out.println();
}
}
*/
public static void main(String[] args) {
int k = Integer.parseInt(args[0]);
RandomizedQueue<String> randomizedQueue = new RandomizedQueue<String>();
while (!StdIn.isEmpty()) {
String str = StdIn.readString();
randomizedQueue.enqueue(str);
}
// deque.asString();
if (k == 0) {
System.out.println();
}
else {
for (int i = 0; i < k; i++) {
System.out.println(randomizedQueue.dequeue());
}
// System.out.println();
}
}
}