iris1230xs 6 år sedan
incheckning
cb57441222

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+.idea
+out
+*.iml

+ 75 - 0
src/ch1/sec3/Bag.java

@@ -0,0 +1,75 @@
+package ch1.sec3;
+
+import java.util.Iterator;
+
+public class Bag<Item> implements Iterable<Item> {
+    private Item[] items;
+    private int size;
+
+
+    @Override
+    public BagIterator iterator() {
+        return new BagIterator();
+    }
+
+    public Bag() {
+        items = (Item[]) new Object[4];
+        size = 0;
+    }
+
+    public void add(Item item) {
+        if (size >= items.length) {
+            resize(size * 2);
+        }
+        items[size] = item;
+        size += 1;
+    }
+
+    public boolean isEmpty() {
+        return size == 0;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    public static void main(String[] args) {
+        Bag<Integer> bag = new Bag<>();
+        System.out.println(bag.isEmpty());
+        bag.add(1);
+        bag.add(2);
+        bag.add(3);
+        bag.add(4);
+        bag.add(5);
+        for (Integer i : bag) {
+            System.out.println(i);
+        }
+        System.out.println(bag.size());
+    }
+
+    private class BagIterator implements Iterator<Item> {
+        private int index;
+
+        BagIterator() {
+            index = 0;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return index < size;
+        }
+
+        @Override
+        public Item next() {
+            return items[index++];
+        }
+    }
+
+    private void resize(int newSize) {
+        Item[] newItems = (Item[]) new Object[newSize];
+        for (int i = 0; i < size; i++) {
+            newItems[i] = items[i];
+        }
+        items = newItems;
+    }
+}

+ 136 - 0
src/ch1/sec3/Deque.java

@@ -0,0 +1,136 @@
+package ch1.sec3;
+
+import java.util.Iterator;
+
+public class Deque<Item> implements Iterable<Item> {
+    private Item[] items;
+    private int size;
+    private int left;
+    private int right;
+
+    @Override
+    public DequeIterator iterator() {
+        return new DequeIterator();
+    }
+
+    public Deque() {
+        items = (Item[]) new Object[4];
+        size = 0;
+        left = 0;
+        right = 0;
+    }
+
+    public boolean isEmpty() {
+        return size == 0;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    public void pushLeft(Item item) {
+        if (size >= items.length) {
+            resize(items.length * 2);
+        }
+        if (!isEmpty()) {
+            left = (left + items.length - 1) % items.length;
+        }
+        items[left] = item;
+        size++;
+    }
+
+    public void pushRight(Item item) {
+        if (size >= items.length) {
+            resize(items.length * 2);
+        }
+        if (!isEmpty()) {
+            right = (right + 1) % items.length;
+        }
+        items[right] = item;
+        size++;
+    }
+
+    public Item popLeft() {
+        if (isEmpty()) {
+            return null;
+        }
+        if (size <= items.length / 4) {
+            resize(items.length / 2);
+        }
+        Item item = items[left];
+        items[left] = null;
+        if (size() != 1) {
+            left = (left + 1) % items.length;
+        }
+        size--;
+        return item;
+    }
+
+    public Item popRight() {
+        if (isEmpty()) {
+            return null;
+        }
+        if (size <= items.length / 4) {
+            resize(items.length / 2);
+        }
+        Item item = items[right];
+        items[right] = null;
+        if (size() != 1) {
+            right = (right + items.length - 1) % items.length;
+        }
+        size--;
+        return item;
+    }
+
+    public static void main(String[] args) {
+        Deque<Integer> deque = new Deque<>();
+        System.out.println(deque.isEmpty());
+        deque.pushLeft(1);
+        deque.pushLeft(2);
+        deque.pushRight(3);
+        deque.pushRight(4);
+        deque.pushLeft(5);
+        deque.pushRight(6);
+        for (Integer i : deque) {
+            System.out.println(i);
+        }
+        System.out.println(deque.size());
+        while (!deque.isEmpty()) {
+            if (deque.size() % 2 == 0) {
+                System.out.println(deque.popLeft());
+            } else {
+                System.out.println(deque.popRight());
+            }
+        }
+    }
+
+    private class DequeIterator implements Iterator<Item> {
+        private int index;
+
+        DequeIterator() {
+            index = 0;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return index < size;
+        }
+
+        @Override
+        public Item next() {
+            int nextIndex = (left + index) % items.length;
+            index++;
+            return items[nextIndex];
+        }
+    }
+
+    private void resize(int newsize) {
+        Item[] newItems = (Item[]) new Object[newsize];
+        for (int i = 0; i < size; i++) {
+            newItems[i] = items[(left + i) % items.length];
+        }
+        left = 0;
+        right = size == 0 ? 0 : size - 1;
+        items = newItems;
+    }
+}

+ 108 - 0
src/ch1/sec3/Evaluate.java

@@ -0,0 +1,108 @@
+package ch1.sec3;
+
+public class Evaluate {
+    public static double calculate(String s) {
+        Stack<String> operators = new Stack<>();
+        Stack<Double> values = new Stack<>();
+        int[] index = {0};
+        while (index[0] < s.length()) {
+            int begin = index[0];
+            Status status = checkStatus(s, index);
+            switch (status) {
+                case VALUE:
+                    Double value = Double.parseDouble(s.substring(begin, index[0]));
+                    values.push(value);
+                    break;
+                case OPERATOR:
+                    String operator = s.substring(begin, index[0]);
+                    operators.push(operator);
+                    break;
+                case CLOSE:
+                    String op = operators.pop();
+                    if (op == null) {
+                        break;
+                    }
+                    calculate(values, op);
+                case IGNORE:
+                    break;
+            }
+        }
+        if (values.isEmpty()) {
+            return 0;
+        } else {
+            return values.pop();
+        }
+    }
+
+    public static void main(String[] args) {
+        System.out.println(calculate("(1 + ( (  2 + 3 ) * ( 4 * 5 )  )  )"));
+        System.out.println(calculate("(.1+ .3)"));
+        System.out.println(calculate("((sqrt(4)))"));
+    }
+
+    private enum Status {
+        OPERATOR, VALUE, IGNORE, CLOSE
+    }
+
+    private static Status checkStatus(String s, int[] index) {
+        char c = s.charAt(index[0]);
+        switch (c) {
+            case '(':
+            case ' ':
+                index[0]++;
+                return Status.IGNORE;
+            case ')':
+                index[0]++;
+                return Status.CLOSE;
+            case 's':
+                index[0] += 3;
+            case '+':
+            case '-':
+            case '*':
+            case '/':
+                index[0]++;
+                return Status.OPERATOR;
+            case '.':
+            case '0':
+            case '1':
+            case '2':
+            case '3':
+            case '4':
+            case '5':
+            case '6':
+            case '7':
+            case '8':
+            case '9':
+                char num = s.charAt(++index[0]);
+                while ('0' <= num && num <= '9' || num == '.') {
+                    index[0]++;
+                    num = s.charAt(index[0]);
+                }
+                return Status.VALUE;
+            default:
+                return Status.IGNORE;
+        }
+    }
+
+    private static void calculate(Stack<Double> values, String op) {
+        double result = 0.0;
+        switch (op) {
+            case "sqrt":
+                result = Math.sqrt(values.pop());
+                break;
+            case "+":
+                result = values.pop() + values.pop();
+                break;
+            case "-":
+                result = values.pop() - values.pop();
+                break;
+            case "*":
+                result = values.pop() * values.pop();
+                break;
+            case "/":
+                result = values.pop() / values.pop();
+                break;
+        }
+        values.push(result);
+    }
+}

+ 81 - 0
src/ch1/sec3/LinkBag.java

@@ -0,0 +1,81 @@
+package ch1.sec3;
+
+import java.util.Iterator;
+
+public class LinkBag<Item> implements Iterable<Item> {
+    private Node head;
+    private Node tail;
+    private int size;
+
+    @Override
+    public LinkBagIterator iterator() {
+        return new LinkBagIterator();
+    }
+
+    public LinkBag() {
+        head = null;
+        tail = null;
+        size = 0;
+    }
+
+    public void add(Item item) {
+        if (isEmpty()) {
+            head = new Node(item, null);
+            tail = head;
+        } else {
+            tail.next = new Node(item, null);
+            tail = tail.next;
+        }
+        size++;
+    }
+
+    public boolean isEmpty() {
+        return size == 0;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    public static void main(String[] args) {
+        LinkBag<Integer> linkBag = new LinkBag<>();
+        System.out.println(linkBag.isEmpty());
+        linkBag.add(1);
+        linkBag.add(2);
+        linkBag.add(9);
+        for (Integer i : linkBag) {
+            System.out.println(i);
+        }
+        System.out.println(linkBag.size());
+    }
+
+    private class Node {
+        private Item value;
+        private Node next;
+
+        public Node(Item value, Node next) {
+            this.value = value;
+            this.next = next;
+        }
+    }
+
+    private class LinkBagIterator implements Iterator<Item> {
+        private Node current;
+
+        LinkBagIterator() {
+            current = head;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return current != null;
+        }
+
+        @Override
+        public Item next() {
+            Item item = current.value;
+            current = current.next;
+            return item;
+        }
+    }
+}

+ 129 - 0
src/ch1/sec3/LinkDeque.java

@@ -0,0 +1,129 @@
+package ch1.sec3;
+
+import java.util.Iterator;
+
+public class LinkDeque<Item> implements Iterable<Item> {
+    private Node left;
+    private Node right;
+    private int size;
+
+    @Override
+    public LinkDequeIterator iterator() {
+        return new LinkDequeIterator();
+    }
+
+    public LinkDeque() {
+        left = null;
+        right = null;
+        size = 0;
+    }
+
+    public boolean isEmpty() {
+        return size == 0;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    public void pushLeft(Item item) {
+        left = new Node(item, null, left);
+        if (isEmpty()) {
+            right = left;
+        } else {
+            left.next.prev = left;
+        }
+        size++;
+    }
+
+    public void pushRight(Item item) {
+        right = new Node(item, right, null);
+        if (isEmpty()) {
+            left = right;
+        } else {
+            right.prev.next = right;
+        }
+        size++;
+    }
+
+    public Item popLeft() {
+        if (isEmpty()) {
+            return null;
+        }
+        Item item = left.value;
+        left = left.next;
+        if (size() == 1) {
+            right = null;
+        } else {
+            left.prev = null;
+        }
+        size--;
+        return item;
+    }
+
+    public Item popRight() {
+        if (isEmpty()) {
+            return null;
+        }
+        Item item = right.value;
+        right = right.prev;
+        if (size() == 1) {
+            left = null;
+        } else {
+            right.next = null;
+        }
+        size--;
+        return item;
+    }
+
+    public static void main(String[] args) {
+        LinkDeque<Integer> linkDeque = new LinkDeque<>();
+        System.out.println(linkDeque.isEmpty());
+        linkDeque.pushLeft(1);
+        linkDeque.pushLeft(2);
+        linkDeque.pushRight(3);
+        linkDeque.pushRight(4);
+        for (Integer i : linkDeque) {
+            System.out.println(i);
+        }
+        System.out.println(linkDeque.size());
+        while (!linkDeque.isEmpty()) {
+            if (linkDeque.size() % 2 == 0) {
+                System.out.println(linkDeque.popLeft());
+            } else {
+                System.out.println(linkDeque.popRight());
+            }
+        }
+    }
+
+    private class Node {
+        private Item value;
+        private Node prev;
+        private Node next;
+        public Node(Item value, Node prev, Node next) {
+            this.value = value;
+            this.prev = prev;
+            this.next = next;
+        }
+    }
+
+    private class LinkDequeIterator implements Iterator<Item> {
+        private Node current;
+
+        public LinkDequeIterator() {
+            current = left;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return current != null;
+        }
+
+        @Override
+        public Item next() {
+            Item item = current.value;
+            current = current.next;
+            return item;
+        }
+    }
+}

+ 96 - 0
src/ch1/sec3/LinkQueue.java

@@ -0,0 +1,96 @@
+package ch1.sec3;
+
+import java.util.Iterator;
+
+public class LinkQueue<Item> implements Iterable<Item> {
+    private Node head;
+    private Node tail;
+    private int size;
+
+    @Override
+    public LinkQueueIterator iterator() {
+        return new LinkQueueIterator();
+    }
+
+    public LinkQueue() {
+        head = null;
+        tail = null;
+        size = 0;
+    }
+
+    public void enqueue(Item item) {
+        if (isEmpty()) {
+            head = new Node(item, null);
+            tail = head;
+        } else {
+            tail.next = new Node(item, null);
+            tail = tail.next;
+        }
+        size++;
+    }
+
+    public Item dequeue() {
+        if (isEmpty()) {
+            return null;
+        }
+        Item item = head.value;
+        head = head.next;
+        if (size() == 1) {
+            tail = tail.next;
+        }
+        size--;
+        return item;
+    }
+
+    public boolean isEmpty() {
+        return size == 0;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    public static void main(String[] args) {
+        LinkQueue<Integer> linkQueue = new LinkQueue<>();
+        System.out.println(linkQueue.isEmpty());
+        linkQueue.enqueue(1);
+        linkQueue.enqueue(2);
+        linkQueue.enqueue(3);
+        for (Integer i : linkQueue) {
+            System.out.println(i);
+        }
+        System.out.println(linkQueue.size());
+        while (!linkQueue.isEmpty()) {
+            System.out.println(linkQueue.dequeue());
+        }
+    }
+
+    private class Node {
+        private Item value;
+        private Node next;
+        public Node(Item value, Node next) {
+            this.value = value;
+            this.next = next;
+        }
+    }
+
+    private class LinkQueueIterator implements Iterator<Item> {
+        private Node current;
+
+        public LinkQueueIterator() {
+            current = head;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return current != null;
+        }
+
+        @Override
+        public Item next() {
+            Item item = current.value;
+            current = current.next;
+            return item;
+        }
+    }
+}

+ 88 - 0
src/ch1/sec3/LinkStack.java

@@ -0,0 +1,88 @@
+package ch1.sec3;
+
+import org.omg.CORBA.PUBLIC_MEMBER;
+
+import java.util.Iterator;
+
+public class LinkStack<Item> implements Iterable<Item> {
+    private Node top;
+    private int size;
+
+
+    @Override
+    public LinkStackIterator iterator() {
+        return new LinkStackIterator();
+    }
+
+    public LinkStack() {
+        top = null;
+        size = 0;
+    }
+
+    public void push(Item item) {
+        top = new Node(item, top);
+        size++;
+    }
+
+    public Item pop() {
+        if (isEmpty()) {
+            return null;
+        }
+        Item item = top.value;
+        top = top.next;
+        size--;
+        return item;
+    }
+
+    public boolean isEmpty() {
+        return size == 0;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    public static void main(String[] args) {
+        LinkStack<Integer> linkStack = new LinkStack<>();
+        System.out.println(linkStack.isEmpty());
+        linkStack.push(1);
+        linkStack.push(2);
+        linkStack.push(3);
+        for (Integer i : linkStack) {
+            System.out.println(i);
+        }
+        System.out.println(linkStack.size());
+        while (!linkStack.isEmpty()) {
+            System.out.println(linkStack.pop());
+        }
+    }
+
+    private class Node {
+        private  Item value;
+        private Node next;
+        public Node(Item value, Node next) {
+            this.value = value;
+            this.next = next;
+        }
+    }
+
+    private class LinkStackIterator implements Iterator<Item> {
+        private Node current;
+
+        public LinkStackIterator() {
+            current = top;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return current !=null;
+        }
+
+        @Override
+        public Item next() {
+            Item item = current.value;
+            current = current.next;
+            return item;
+        }
+    }
+}

+ 104 - 0
src/ch1/sec3/Queue.java

@@ -0,0 +1,104 @@
+package ch1.sec3;
+
+import java.util.Iterator;
+
+public class Queue<Item> implements Iterable<Item> {
+    private Item[] items;
+    private int size;
+    private int head;
+    private int tail;
+
+    @Override
+    public QueueIterator iterator() {
+        return new QueueIterator();
+    }
+
+    public Queue() {
+        items = (Item[]) new Object[4];
+        size = 0;
+        head = 0;
+        tail = 0;
+    }
+
+    public void enqueue(Item item) {
+        if (size >= items.length) {
+            resize(items.length * 2);
+        }
+        if (!isEmpty()) {
+            tail = (tail + 1) % items.length;
+        }
+        items[tail] = item;
+        size++;
+    }
+
+    public Item dequeue() {
+        if (isEmpty()) {
+            return null;
+        }
+        if (size <= items.length / 4) {
+            resize(items.length / 2);
+        }
+        Item item = items[head];
+        items[head] = null;
+        if (size() != 1) {
+            head = (head + 1) % items.length;
+        }
+        size--;
+        return item;
+    }
+
+    public boolean isEmpty() {
+        return size == 0;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    public static void main(String[] args) {
+        Queue<Integer> queue= new Queue<>();
+        System.out.println(queue.isEmpty());
+        queue.enqueue(1);
+        queue.enqueue(2);
+        queue.enqueue(3);
+        queue.enqueue(4);
+        queue.enqueue(5);
+        for (Integer i : queue) {
+            System.out.println(i);
+        }
+        System.out.println(queue.size());
+        while (!queue.isEmpty()) {
+            System.out.println(queue.dequeue());
+        }
+    }
+
+    private class QueueIterator implements Iterator<Item> {
+        private int index;
+
+        QueueIterator() {
+            index = 0;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return index < size;
+        }
+
+        @Override
+        public Item next() {
+            int nextIndex = (head + index) % items.length;
+            index++;
+            return items[nextIndex];
+        }
+    }
+
+    private void resize(int newsize) {
+        Item[] newItems = (Item[]) new Object[newsize];
+        for(int i = 0; i < size; i++) {
+            newItems[i] = items[(head + i) % items.length];
+        }
+        head = 0;
+        tail = size == 0 ? 0 : size - 1;
+        items = newItems;
+    }
+}

+ 88 - 0
src/ch1/sec3/Stack.java

@@ -0,0 +1,88 @@
+package ch1.sec3;
+
+import java.util.Iterator;
+
+public class Stack<Item> implements Iterable<Item> {
+    private Item[] items;
+    private int size;
+
+    @Override
+    public StackIterator iterator() {
+        return new StackIterator();
+    }
+
+    public Stack() {
+        items = (Item[]) new Object[4];
+        size = 0;
+    }
+
+    public void push(Item item) {
+        if (size >= items.length) {
+            resize(items.length * 2);
+        }
+        items[size++] = item;
+    }
+
+    public Item pop() {
+        if (isEmpty()) {
+            return null;
+        }
+        if (size <= items.length / 4) {
+            resize(items.length / 2);
+        }
+        Item item = items[--size];
+        items[size] = null;
+        return item;
+    }
+
+    public boolean isEmpty() {
+        return size == 0;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    public static void main(String[] args) {
+        Stack<Integer> stack = new Stack<>();
+        System.out.println(stack.isEmpty());
+        stack.push(1);
+        stack.push(2);
+        stack.push(3);
+        stack.push(4);
+        stack.push(5);
+        for (Integer i : stack) {
+            System.out.println(i);
+        }
+        System.out.println(stack.size());
+        while (!stack.isEmpty()) {
+            System.out.println(stack.pop());
+        }
+    }
+
+    private class StackIterator implements Iterator<Item> {
+        private int index;
+
+        StackIterator() {
+            index = size;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return index != 0;
+        }
+
+        @Override
+        public Item next() {
+            return items[--index];
+        }
+    }
+
+    private void resize(int newSize) {
+        Item[] newItems = (Item[]) new Object[newSize];
+        for(int i = 0; i < size; i++) {
+            newItems[i] = items[i];
+        }
+        items = newItems;
+    }
+}

+ 29 - 0
src/ch2/sec1/Insertion.java

@@ -0,0 +1,29 @@
+package ch2.sec1;
+
+public class Insertion {
+    public static void sort(Comparable[] a) {
+        for (int i = 0; i < a.length; i++) {
+            for (int j = i; j > 0 && less(a[j], a[j - 1]); j--) {
+                exch(a, j, j - 1);
+            }
+        }
+    }
+
+    private static boolean less(Comparable v, Comparable w) {
+        return v.compareTo(w) < 0;
+    }
+
+    private static void exch(Comparable[] a, int i, int j) {
+        Comparable t = a[i];
+        a[i] = a[j];
+        a[j] = t;
+    }
+
+    public static void main(String[] args) {
+        Integer[] num = new Integer[] {2,1,4,2,5};
+        Insertion.sort(num);
+        for (int n:num) {
+            System.out.println(n);
+        }
+    }
+}

+ 35 - 0
src/ch2/sec1/Selection.java

@@ -0,0 +1,35 @@
+package ch2.sec1;
+
+public class Selection {
+    public static void sort(Comparable[] a) {
+        for (int i = 0; i < a.length; i++) {
+            Comparable min = a[i];
+            int index = i;
+            for (int j = i + 1; j < a.length; j++) {
+                if (less(a[j], min)) {
+                    min = a[j];
+                    index = j;
+                }
+            }
+            exch(a, i, index);
+        }
+    }
+
+    private static boolean less(Comparable v, Comparable w) {
+        return v.compareTo(w) < 0;
+    }
+
+    private static void exch(Comparable[] a, int i, int j) {
+        Comparable t = a[i];
+        a[i] = a[j];
+        a[j] = t;
+    }
+
+    public static void main(String[] args) {
+        Integer[] num = new Integer[] {2,1,4,2,5};
+        Selection.sort(num);
+        for (int n:num) {
+            System.out.println(n);
+        }
+    }
+}

+ 25 - 0
src/ch2/sec1/Shell.java

@@ -0,0 +1,25 @@
+package ch2.sec1;
+
+public class Shell {
+    public static void sort(Comparable[] a) {
+
+    }
+
+    private static boolean less(Comparable v, Comparable w) {
+        return v.compareTo(w) < 0;
+    }
+
+    private static void exch(Comparable[] a, int i, int j) {
+        Comparable t = a[i];
+        a[i] = a[j];
+        a[j] = t;
+    }
+
+    public static void main(String[] args) {
+        Integer[] num = new Integer[] {2,1,4,2,5};
+        Shell.sort(num);
+        for (int n:num) {
+            System.out.println(n);
+        }
+    }
+}

+ 38 - 0
src/ch2/sec1/SortCompare.java

@@ -0,0 +1,38 @@
+package ch2.sec1;
+
+import java.time.Instant;
+import java.util.concurrent.ThreadLocalRandom;
+
+public class SortCompare {
+    public static long time(String alg, Double[] a) {
+        long start = Instant.now().toEpochMilli();
+        if (alg.equals("Insertion")) {
+            Insertion.sort(a);
+        }
+        if (alg.equals("Selection")) {
+            Selection.sort(a);
+        }
+        long end = Instant.now().toEpochMilli();
+        return end - start;
+    }
+
+    public static double timeRandomInput(String alg, int N, int T) {
+        long total = 0;
+        Double[] a = new Double[N];
+        for (int i = 0; i < T; i++) {
+            for (int j = 0 ; j < N; j++) {
+                a[j] = ThreadLocalRandom.current().nextDouble(0.0, 1.0);
+            }
+            total += time(alg, a);
+        }
+        return total/1000.0;
+    }
+
+    public static void main(String[] args) {
+        int N = 1000;
+        int T = 100;
+        double tSelect = timeRandomInput("Selection", N, T);
+        double tInsert = timeRandomInput("Insertion", N, T);
+        System.out.printf("Selection: %f \n Insertion: %f\n", tSelect, tInsert);
+    }
+}