princeton algorithms part 1 第二周作业题

  • 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[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;
        }
    }

}
  • 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--;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值