1. 程式人生 > >Coursera Programming Assignment 2: Deques and Randomized Queues

Coursera Programming Assignment 2: Deques and Randomized Queues

完整程式碼如下

Deque.java

import java.util.Iterator;
import java.util.NoSuchElementException;


public class Deque<Item> implements Iterable<Item> {

	private Node first, last;
	private int N;
	
	private class Node {
		Item value;
		Node next;
		Node pre;
		Node(Item item) {
			this.value =
item; next = null; pre = null; } } public Deque() { N = 0; first = last = null; } public boolean isEmpty() { return N == 0; } public int size() { return N; } public void addFirst(Item item) { if(null == item) { throw new IllegalArgumentException(); } Node node =
new Node(item); if(isEmpty()) { first = last = node; N++; return; } N++; node.pre = null; node.next = first; first.pre = node; first = node; } public void addLast(Item item) { if(null == item) { throw new IllegalArgumentException(); } Node node = new Node
(item); if(isEmpty()) { first = last = node; N++; return; } N++; node.pre = last; node.next = null; last.next = node; last = node; } public Item removeFirst() { if(isEmpty()) { throw new NoSuchElementException(); } if(last == first) { Item ans = first.value; first = last = null; N = 0; return ans; } Node ans = first; Node second = first.next; second.pre = null; ans.next = null; first = second; N--; return ans.value; } public Item removeLast() { if(isEmpty()) { throw new NoSuchElementException(); } if(last == first) { Item ans = last.value; last = first = null; N = 0; return ans; } Node ans = last; Node pre = last.pre; ans.pre = null; pre.next = null; last = pre; N--; return ans.value; } private class MyIter implements Iterator<Item> { private Node current = null; public MyIter(Node first) { this.current = first; } @Override public boolean hasNext() { return current != null; } @Override public Item next() { if(!hasNext()) { throw new NoSuchElementException(); } Item ans = current.value; current = current.next; return ans; } @Override public void remove() { throw new UnsupportedOperationException(); } } @Override public Iterator<Item> iterator() { return new MyIter(first); } }

RandomizedQueue.java


import java.util.Iterator;
import java.util.NoSuchElementException;

import com.sun.org.apache.bcel.internal.generic.NEW;

import edu.princeton.cs.algs4.StdRandom;

public class RandomizedQueue<Item> implements Iterable<Item> {

	private Item[] queue;
	private int size = 0; // the number of current element in the queue
	
	
	public RandomizedQueue() {
		queue = (Item[]) new Object[8];
		size = 0;
	}

	public boolean isEmpty() {
		return size == 0;
	}

	public int size() {
		return size;
	}

	public void enqueue(Item item) {
		if(null == item) {
			throw new IllegalArgumentException();
		}
		if(size == queue.length) {
			resize(2 * queue.length);
		}
		queue[size++] = item;
	}
	
	
	private void resize(int newLen) {
		Item[] temp = (Item[]) new Object[newLen];
		for(int i = 0; i < size; i++) {
			temp[i] = queue[i];
		}
		queue = temp;
	}
	
	public Item dequeue() {
		if(isEmpty()) {
			throw new NoSuchElementException();
		}
		int index = StdRandom.uniform(size);
		Item item = queue[index];
		// Delete an element, and move the last element to the deleted index
		if(index != size - 1) {
			queue[index] = queue[size - 1];
		}
		queue[size - 1] = null;
		size--;
		if(size > 0 && size == queue.length / 4) {
			resize(queue.length / 2);
		}
		return item;
	}

	public Item sample() {
		if(isEmpty()) {
			throw new NoSuchElementException();
		}
		return queue[StdRandom.uniform(size)];
	}

	private class MyIter implements Iterator<Item> {

		private Item[] items = (Item[])new Object[queue.length];
		private int n = size;
		
		public MyIter() {
			for(int i = 0; i < queue.length; i++) {
				items[i] = queue[i];
			}
		}
		
		@Override
		public boolean hasNext() {
			return n != 0;
		}

		@Override
		public Item next() {
			if(!hasNext()) {
				throw new NoSuchElementException();
			}
			int index = StdRandom.uniform(n);
			Item item = items[index];
			if(index != n - 1) {
				items[index] = items[n - 1];
			}
			items[n - 1] = null;
			n--;
			return item;
		}
		
		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
		
	}
	
	public Iterator<Item> iterator() {
		return new MyIter();
	}
}

Permutation.java

import edu.princeton.cs.algs4.StdIn;

public class Permutation {

	public static void main(String[] args) {
		int k = Integer.valueOf(args[0]);
		String str = null;
		RandomizedQueue<String> queue = new RandomizedQueue<String>();
		while(!StdIn.isEmpty()) {
			str = StdIn.readString();
			queue.enqueue(str);
		}
		
		
		
		for(int i = 0; i < k; i++) {
			String string = queue.dequeue();
			System.out.println(string);
		}
	}
}