diff --git a/.idea/workspace.xml b/.idea/workspace.xml index ecd92f0..200b51a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,48 +2,18 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + - - - + - + + + + + - - + + + + + - - @@ -194,7 +171,8 @@ 1629438904473 - + + diff --git a/Percolation/PercolationStats.java b/Percolation/PercolationStats.java index d352ecf..4dc639f 100644 --- a/Percolation/PercolationStats.java +++ b/Percolation/PercolationStats.java @@ -1,8 +1,4 @@ -/* ***************************************************************************** - * Name: Alan Turing - * Coursera User ID: 123456 - * Last modified: 1/1/2019 - **************************************************************************** */ + import edu.princeton.cs.algs4.StdRandom; public class PercolationStats { diff --git a/Queues/src/Dequeue.java b/Queues/src/Deque.java similarity index 58% rename from Queues/src/Dequeue.java rename to Queues/src/Deque.java index ce65914..7687d18 100644 --- a/Queues/src/Dequeue.java +++ b/Queues/src/Deque.java @@ -1,11 +1,7 @@ -import javax.print.DocFlavor; -import java.util.ArrayList; import java.util.Iterator; -import java.util.Scanner; -public class Dequeue implements Iterable { - - public class Node { +public class Deque implements Iterable { + private class Node { private Item item; private Node next; @@ -27,10 +23,11 @@ public Item getItem() { } } - private Node first; + private Node first; + private int size; // construct an empty deque - public Dequeue() { + public Deque() { first = null; } @@ -44,19 +41,7 @@ public boolean isEmpty() { // return the number of items on the deque public int size() { - Node tem = first; - if(first == null) { - return 0; - } - int count = 0; - while(true) { - if(tem.getNext() == null) { - break; - } - count++; - tem = tem.getNext(); - } - return count; + return size; } // add the item to the front @@ -64,13 +49,15 @@ public void addFirst(Item item) { if(item == null) { throw new java.lang.IllegalArgumentException(); } - Node tem = new Node(item); + Node tem = new Node(item); if(first == null) { first = tem; + size++; return; } tem.setNext(first); first = tem; + size++; } // add the item to the back @@ -78,12 +65,13 @@ public void addLast(Item item) { if(item == null) { throw new java.lang.IllegalArgumentException(); } - Node tem = new Node(item); + Node tem = new Node(item); if(first == null) { first = tem; + size++; return; } - Node iter = first; + Node iter = first; while(true) { if(iter.getNext() == null) { break; @@ -91,6 +79,7 @@ public void addLast(Item item) { iter = iter.getNext(); } iter.setNext(tem); + size++; } // remove and return the item from the front @@ -100,6 +89,7 @@ public Item removeFirst() { } Item tem = first.getItem(); first = first.getNext(); + size--; return tem; } @@ -108,22 +98,25 @@ public Item removeLast() { if(first == null) { throw new java.util.NoSuchElementException(); } - Node iter = first; - while(true) { - if(iter.getNext() == null) { - break; - } - iter = iter.getNext(); + Node iter = first; + Node finder = first.getNext(); + if(finder == null) { + Item tem = iter.getItem(); + first = null; + size--; + return tem; } - Node finder = first; while(true) { - if(finder.getNext() == iter) { + if(finder.getNext() == null) { break; } - finder = first.getNext(); + iter = iter.getNext(); + finder = finder.getNext(); } - Item tem = iter.getItem(); - finder.setNext(null); + + Item tem = finder.getItem(); + iter.setNext(null); + size--; return tem; } @@ -134,17 +127,20 @@ public Iterator iterator() { } private class QueueIterator implements Iterator { - private Node currnet = first; + private Node current = first; @Override public boolean hasNext() { - return currnet != null; + return current != null; } @Override public Item next() { - Item item = currnet.getItem(); - currnet = currnet.getNext(); + if(hasNext() == false) { + throw new java.util.NoSuchElementException(); + } + Item item = current.getItem(); + current = current.getNext(); return item; } @@ -157,29 +153,6 @@ public void remove() { // unit testing (required) public static void main(String[] args) { - Dequeue app = new Dequeue<>(); - Scanner sc = new Scanner(System.in); - StringBuilder builder = new StringBuilder(); - builder.append("----DEQUEUE----\n") - .append("(1) add (2) print (3) exit\n"); - while(true) { - System.out.print(builder.toString()); - - int menu = sc.nextInt(); - if(menu == 1) { - app.addLast(sc.nextInt()); - } - else if(menu == 2) { - for(Integer i: app) { - System.out.print(i + " "); - } - System.out.print("\n"); - } - else { - break; - } - - } } diff --git a/Queues/src/Node.java b/Queues/src/Node.java deleted file mode 100644 index ef51ecc..0000000 --- a/Queues/src/Node.java +++ /dev/null @@ -1,21 +0,0 @@ -public class Node { - private Item item; - private Node next; - - public Node(Item item) { - this.item = item; - next = null; - } - - public void setNext(Node next) { - this.next = next; - } - - public Node getNext() { - return next; - } - - public Item getItem() { - return item; - } -} diff --git a/Queues/src/Permutation.java b/Queues/src/Permutation.java index 69de679..692a1f1 100644 --- a/Queues/src/Permutation.java +++ b/Queues/src/Permutation.java @@ -1,29 +1,43 @@ +import edu.princeton.cs.algs4.StdIn; +import edu.princeton.cs.algs4.StdOut; +import edu.princeton.cs.algs4.StdRandom; import edu.princeton.cs.algs4.In; -import java.util.ArrayList; - public class Permutation { - private ArrayList usedIndex = new ArrayList<>(); - Permutation(int n, String[] s) { - int tem; - while(usedIndex.size() <= s.length) { - tem =(int)(Math.random()*s.length); + public static void main(String[] args) { + RandomizedQueue rQ = new RandomizedQueue(); + int cnt; - for(int i = 0; i < usedIndex.size(); i++) { - if(usedIndex.get(i) == tem) { - continue; - } - } + while (!StdIn.isEmpty()) + { + rQ.enqueue(StdIn.readString()); + } + cnt = Integer.parseInt(args[0]); - usedIndex.add(tem); - System.out.print(s[tem]); + int i = 0; + while (i < cnt) { + System.out.print(rQ.dequeue() + " \n"); + i++; } } - public static void main(String[] args) { + /* + private Permutation(int n, String[] s) { + int tem = n; + RandomizedQueue rQ; + rQ = new RandomizedQueue<>(); + for (int i = 0; i < s.length; i++) { + rQ.enqueue(s[i]); + } + + while (n != 0) { + System.out.print(rQ.dequeue()); + } } + */ + } \ No newline at end of file diff --git a/Queues/src/RandomizedQueue.java b/Queues/src/RandomizedQueue.java index ce9d457..555da70 100644 --- a/Queues/src/RandomizedQueue.java +++ b/Queues/src/RandomizedQueue.java @@ -1,9 +1,10 @@ -import java.util.ArrayList; +//import java.util.ArrayList; import java.util.Iterator; +import java.lang.Object; public class RandomizedQueue implements Iterable { - public class Node { + private class Node { private Item item; private Node next; @@ -24,21 +25,26 @@ public Item getItem() { return item; } } + private int queueSize; - private ArrayList randomizedQueues = new ArrayList<>(); + //private ArrayList randomizedQueues = new ArrayList<>(); + private Item[] randomizeQueues; // construct an empty randomized queue public RandomizedQueue() { + randomizeQueues = (Item[]) new Object[1]; } // is the randomized queue empty? public boolean isEmpty() { - return randomizedQueues.size() == 0; + //return randomizedQueues.size() == 0; + return queueSize == 0; } // return the number of items on the randomized queue public int size() { - return randomizedQueues.size(); + //return randomizedQueues.size(); + return queueSize; } // add the item @@ -46,11 +52,22 @@ public void enqueue(Item item) { if(item == null) { throw new java.lang.IllegalArgumentException(); } - randomizedQueues.add((int)(Math.random() * randomizedQueues.size()),item); + //randomizedQueues.add((int)(Math.random() * randomizedQueues.size()),item); + if(queueSize == randomizeQueues.length) { + Item[] tem = (Item[]) new Object[randomizeQueues.length * 2]; + for(int i = 0; i < queueSize; i++) { + tem[i] = randomizeQueues[i]; + } + randomizeQueues = tem; + } + randomizeQueues[queueSize] = item; + queueSize++; + return; } // remove and return a random item public Item dequeue() { + /* if(randomizedQueues.size() == 0) { throw new java.util.NoSuchElementException(); } @@ -58,14 +75,38 @@ public Item dequeue() { Item tem = randomizedQueues.get(index); randomizedQueues.remove(index); return tem; + */ + if(queueSize == 0) { + throw new java.util.NoSuchElementException(); + } + + int index = (int)(Math.random() * queueSize); + + Item answer = randomizeQueues[index]; + + for(int i = index; i < queueSize - 1; i++) { + randomizeQueues[i] = randomizeQueues[i+1]; + } + randomizeQueues[queueSize - 1] = null; + queueSize--; + + if(queueSize*2 == randomizeQueues.length) { + Item[] tem = (Item[]) new Object[randomizeQueues.length / 2]; + for(int i = 0; i < queueSize; i++) { + tem[i] = randomizeQueues[i]; + } + randomizeQueues = tem; + } + return answer; } // return a random item (but do not remove it) public Item sample() { - if(randomizedQueues.size() == 0) { + if(queueSize == 0) { throw new java.util.NoSuchElementException(); } - return randomizedQueues.get((int)(Math.random() * randomizedQueues.size())); + + return randomizeQueues[(int)(Math.random() * queueSize)]; } // return an independent iterator over items in random order @@ -74,14 +115,20 @@ public Iterator iterator() { } private class RandomizedQueueIterator implements Iterator { + private int i; @Override public boolean hasNext() { - return randomizedQueues.size() != 0; + return randomizeQueues[i] != null; } @Override public Item next() { - return randomizedQueues.get((int)(Math.random() * randomizedQueues.size())); + if(hasNext() == false) { + throw new java.util.NoSuchElementException(); + } + Item answer = randomizeQueues[i]; + i++; + return answer; } @Override @@ -93,6 +140,7 @@ public void remove() { // unit testing (required) public static void main(String[] args) { + } } \ No newline at end of file diff --git a/out/production/Coursera_Algorithms/PercolationStats.class b/out/production/Coursera_Algorithms/PercolationStats.class index 0d0bd62..a1f44b9 100644 Binary files a/out/production/Coursera_Algorithms/PercolationStats.class and b/out/production/Coursera_Algorithms/PercolationStats.class differ diff --git a/out/production/Queues/Deque$Node.class b/out/production/Queues/Deque$Node.class new file mode 100644 index 0000000..ff3ec6e Binary files /dev/null and b/out/production/Queues/Deque$Node.class differ diff --git a/out/production/Queues/Deque$QueueIterator.class b/out/production/Queues/Deque$QueueIterator.class new file mode 100644 index 0000000..359abc1 Binary files /dev/null and b/out/production/Queues/Deque$QueueIterator.class differ diff --git a/out/production/Queues/Deque.class b/out/production/Queues/Deque.class new file mode 100644 index 0000000..cfa6c0a Binary files /dev/null and b/out/production/Queues/Deque.class differ diff --git a/out/production/Queues/Dequeue$Node.class b/out/production/Queues/Dequeue$Node.class deleted file mode 100644 index affe605..0000000 Binary files a/out/production/Queues/Dequeue$Node.class and /dev/null differ diff --git a/out/production/Queues/Dequeue$QueueIterator.class b/out/production/Queues/Dequeue$QueueIterator.class deleted file mode 100644 index 5d45e7a..0000000 Binary files a/out/production/Queues/Dequeue$QueueIterator.class and /dev/null differ diff --git a/out/production/Queues/Dequeue.class b/out/production/Queues/Dequeue.class deleted file mode 100644 index 826da15..0000000 Binary files a/out/production/Queues/Dequeue.class and /dev/null differ diff --git a/out/production/Queues/Permutation.class b/out/production/Queues/Permutation.class index b13699d..a0fc812 100644 Binary files a/out/production/Queues/Permutation.class and b/out/production/Queues/Permutation.class differ diff --git a/out/production/Queues/RandomizedQueue$Node.class b/out/production/Queues/RandomizedQueue$Node.class index 0b1baf5..e06afc0 100644 Binary files a/out/production/Queues/RandomizedQueue$Node.class and b/out/production/Queues/RandomizedQueue$Node.class differ diff --git a/out/production/Queues/RandomizedQueue$RandomizedQueueIterator.class b/out/production/Queues/RandomizedQueue$RandomizedQueueIterator.class index 53fd72c..d138432 100644 Binary files a/out/production/Queues/RandomizedQueue$RandomizedQueueIterator.class and b/out/production/Queues/RandomizedQueue$RandomizedQueueIterator.class differ diff --git a/out/production/Queues/RandomizedQueue.class b/out/production/Queues/RandomizedQueue.class index 6c56496..e67fd8f 100644 Binary files a/out/production/Queues/RandomizedQueue.class and b/out/production/Queues/RandomizedQueue.class differ