您的位置:首页 > 编程语言 > Go语言

princeton algorithms part 1 第二周作业题

2018-01-08 21:11 381 查看
Deque

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;
}                           // construct an empty deque

public boolean isEmpty() {
return N == 0;
}                 // is the deque empty?

public int size() {
return N;
}                        // return the number of items on the deque

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++;
}          // add the item to the front

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++;
}           // add the item to the end

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;
}                // remove and return the item from the front

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;
}                 // remove and return the item from the end

public Iterator<Item> iterator() {
return new ListIterator();
}         // return an iterator over items in order from front to end

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();
}
}

}


RandomizedQueue

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;
}// construct an empty randomized queue

public boolean isEmpty() {
return N == 0;
}                 // is the randomized queue empty?

public int size() {
return N;
}// return the number of items on the randomized queue

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++;
}// add the item

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;
}                    // remove and return a random item

public Item sample() {
if (isEmpty())
throw new java.util.NoSuchElementException();
int index = (head + StdRandom.uniform(N)) % queue.length;
return queue[index];
}// return a random item (but do not remove it)

public Iterator<Item> iterator() {
return new RQIterator();
}         // return an independent iterator over items in random order

private class RQIterator implements Iterator<Item> {
private int index = 0;
private Item[] r;
public RQIterator() {
r = (Item[]) new Object
;
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;
}
}

}


Permutation

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--;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐