算法第一部分
Algorithms, Part Iby Kevin Wayne, Robert SedgewickPrinceton University
Week2——编程作业2:Randomized Queues and Deques完整代码
Deque.java
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private int N;
private Node first,last;
private class Node{
Item item;
Node next;
Node previous;
}
public Deque(){
first = 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 NullPointerException();
Node oldfirst = first;
first = new Node();
first.item = item;
first.previous = null;
if(isEmpty()){
last = first;
first.next = null;
}
else{
first.next = oldfirst;
oldfirst.previous = first;
}
N++;
}
public void addLast(Item item){
if(item == null) throw new NullPointerException();
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if(isEmpty()){
first = last;
last.previous = null;
}
else{
last.previous = oldlast;
oldlast.next = last;
}
N++;
}
public Item removeFirst(){
if(isEmpty()){
throw new NoSuchElementException();
}
Item item = first.item;
first = first.next;
N--;
if(isEmpty()){
last = first = null;
}
else{
first.previous = null;
}
return item;
}
public Item removeLast(){
if(isEmpty()) throw new NoSuchElementException();
Item item = last.item;
last = last.previous;
N--;
if(isEmpty()){
first = last = null;
}
else{
last.next = null;
}
return item;
}
public Iterator<Item> iterator(){
return new ListIterator(first);
}
private class ListIterator implements Iterator<Item> {
private Node current;
public ListIterator(Node first) {
current = first;
}
public boolean hasNext() {
return current != null;
}
public void remove(){
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args){
Deque<String> q = new Deque<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-") && !item.equals("#")){
q.addLast(item);
// q.addFirst(item);
}
else if (item.equals("-")){
StdOut.print(q.removeFirst() + " ");
}
else if(item.equals("#")){
StdOut.print(q.removeLast() + " ");
}
}
StdOut.println("(" + q.size() + " left on queue)");
}
}
RandomizedQueue.java
import java.util.Iterator;
import java.util.NoSuchElementException;
public class RandomizedQueue<Item> implements Iterable<Item> {
private Item[] q;
private int N = 0;
public RandomizedQueue(){
q = (Item[]) new Object[1];
}
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] = q[i];
}
q = temp;
}
public void enqueue(Item item){
if(item == null) throw new NullPointerException();
if(N == q.length) resize(2*q.length);
q[N++] = item;
}
public Item dequeue(){
if(isEmpty()) throw new NoSuchElementException();
int offset = StdRandom.uniform(N);
Item item = q[offset];
if(offset != N-1) q[offset] = q[N-1];
q[N-1] = null;
N--;
if(N > 0 && N == q.length/4) resize(q.length/2);
return item;
}
public Item sample(){
if(isEmpty()) throw new NoSuchElementException();
int offset = StdRandom.uniform(N);
return q[offset];
}
public Iterator<Item> iterator(){
return new ArrayIterator();
}
private class ArrayIterator implements Iterator<Item> {
private Item[] copyArray = (Item[]) new Object[q.length];
private int copyN = N;
public ArrayIterator(){
for(int i = 0;i < q.length;i++){
copyArray[i] = q[i];
}
}
public boolean hasNext(){
return copyN != 0;
}
public void remove(){
throw new UnsupportedOperationException();
}
public Item next(){
if (!hasNext()) throw new NoSuchElementException();
int offset = StdRandom.uniform(copyN);
Item item = copyArray[offset];
if(offset != copyN-1){
copyArray[offset] = copyArray[copyN-1];
}
copyArray[copyN-1] = null;
copyN--;
return item;
}
}
public static void main(String[] args){
RandomizedQueue<String> q = new RandomizedQueue<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) q.enqueue(item);
else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");
}
StdOut.println("(" + q.size() + " left on queue)");
}
}
Subset.java
public class Subset {
public static void main(String[] args){
RandomizedQueue<String> q = new RandomizedQueue<String>();
int k = Integer.valueOf(args[0]);
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
q.enqueue(item);
}
while (k > 0){
StdOut.println(q.dequeue());
k--;
}
}
}
另外,在写RandomizedQueue时,我走了弯路,先是尝试利用链表实现,结果timing不通过,其他功能都调试通过了。为了纪念,贴上代码,也给大家做个参考。
RandomizedQueue(link-list).java
import java.util.Iterator;
import java.util.NoSuchElementException;
public class RandomizedQueue<Item> implements Iterable<Item>{
private int N;
private Node first,last;
private class Node{
Item item;
Node next;
}
public RandomizedQueue(){
first = last = null;
N = 0;
}
public boolean isEmpty(){
return N == 0;
}
public int size(){
return N;
}
public void enqueue(Item item){
if(item == null) throw new NullPointerException();
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if(isEmpty()){
first = last;
}
else{
oldlast.next = last;
}
N++;
}
public Item dequeue(){
if(isEmpty()){
throw new NoSuchElementException();
}
Node randnode = first;
Node prenode = first;
int offset = StdRandom.uniform(N);
while(offset > 1){
prenode = prenode.next;
offset--;
}
if(offset != 0){
randnode = prenode.next;
}
Item item = randnode.item;
N--;
if(isEmpty()){
first = last = null;
}
else if(randnode == last){
last = prenode;
prenode.next = null;
}
else if(randnode == first){
first = first.next;
}
else{
prenode.next = randnode.next;
}
return item;
}
public Item sample(){
if(isEmpty()){
throw new NoSuchElementException();
}
Node randnode = first;
int offset = StdRandom.uniform(N);
while(offset != 0){
randnode = randnode.next;
offset--;
}
Item item = randnode.item;
return item;
}
public Iterator<Item> iterator(){
return new ListIterator(first);
}
private class ListIterator implements Iterator<Item> {
private Node first1,last1;
int N1 = N;
public ListIterator(Node first) {
if(N1 == 0) return;
Node newNode = new Node();
newNode.item = first.item;
newNode.next = null;
last1 = newNode;
first1 = last1;
for ( Node cur = first.next; cur != null; cur = cur.next)
{
newNode = new Node();
newNode.item = cur.item;
newNode.next = null;
last1.next = newNode;
last1 = newNode;
}
}
public boolean hasNext() {
return N1 != 0;
}
public void remove(){
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Node randnode = first1;
Node prenode = first1;
int offset = StdRandom.uniform(N1);
while(offset > 1){
prenode = prenode.next;
offset--;
}
if(offset != 0) randnode = prenode.next;
Item item = randnode.item;
N1--;
if(N1 == 0){
first1 = last1 = null;
}
else if(randnode == last1){
last1 = prenode;
prenode.next = null;
}
else if(randnode == first1){
first1 = first1.next;
}
else{
prenode.next = randnode.next;
}
return item;
}
}
public static void main(String[] args){
RandomizedQueue<String> q = new RandomizedQueue<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) q.enqueue(item);
else StdOut.print(q.dequeue() + " ");
// else StdOut.print(q.sample() + " ");
}
StdOut.println("(" + q.size() + " left on queue)");
}
}