diff --git a/744 b/744 new file mode 100644 index 0000000000000000000000000000000000000000..bc46dbd62df0af4c6be73c68cdeb92de8a7f822c --- /dev/null +++ b/744 @@ -0,0 +1,427 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class a { + public class ResizingArrayStack implements Iterable { + private Item[] a; // 数组表示栈 + private int n; // 栈元素数 + + + // 初始化一个空栈 + public ResizingArrayStack() { + a = (Item[]) new Object[2]; + n = 0; + } + + // 判断栈内是否有元素 + public boolean isEmpty() { + return n == 0; + } + + // 返回栈元素数 + public int size() { + return n; + } + + // 改变栈的大小 + private void resize(int capacity) { + assert capacity >= n; + Item[] temp = (Item[]) new Object[capacity]; + for (int i = 0; i < n; i++) { + temp[i] = a[i]; + } + a = temp; + } + + public void push(Item item) { + //判断n的大小,改变栈的大小 + if (n == a.length) resize(2 * a.length); + a[n++] = item; + } + + //弹出并返回元素 + public Item pop() { + if (isEmpty()) throw new NoSuchElementException("Stack underflow"); + Item item = a[n - 1]; + a[n - 1] = null; + n--; + if (n > 0 && n == a.length / 4) resize(a.length / 2); + return item; + } + + + // 返回栈 + public Item peek() { + if (isEmpty()) throw new NoSuchElementException("Stack underflow"); + return a[n - 1]; + } + + //返回一个可以进行先进后出迭代的迭代器 + public Iterator iterator() { + return new ReverseArrayIterator(); + } + + // 用内部类实现迭代器接口,实现从栈顶往栈底的先进后出迭代,没有实现remove()方法。 + private class ReverseArrayIterator implements Iterator { + private int i; + + public ReverseArrayIterator() { + i = n - 1; + } + + public boolean hasNext() { + return i >= 0; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + public Item next() { + if (!hasNext()) throw new NoSuchElementException(); + return a[i--]; + } + } + +} +} + + + + +2.import java.util.Iterator; +import java.util.NoSuchElementException; + +public class b { + public class Stack implements Iterable { + private Node first; + private int N; + + // 辅助类Node,用于形成链表 + private class Node { + private Item item; + private Node next; + } + + /** + * 初始化栈 + */ + public Stack() { + first = null; + N = 0; + } + + /** + * 判断栈是否为空 + */ + public boolean isEmpty() { + return first == null; + } + + /** + * 返回栈内元素数量 + */ + public int size() { + return N; + } + + /** + * 压入元素 + */ + public void push(Item item) { + Node oldfirst = first; + first = new Node(); + first.item = item; + first.next = oldfirst; + N++; + } + + /** + * 弹出元素 + */ + public Item pop() { + if (isEmpty()) throw new NoSuchElementException("Stack underflow"); + Item item = first.item; + first = first.next; + N--; + return item; + } + + + /** + * 返回但不弹出元素 + */ + public Item peek() { + if (isEmpty()) throw new NoSuchElementException("Stack underflow"); + return first.item; + } + + /** + * 从栈顶到栈底打印元素 + */ + public String toString() { + StringBuilder s = new StringBuilder(); + for (Item item : this) + s.append(item + " "); + return s.toString(); + } + + + /** + * 实现Iterable接口 + */ + public Iterator iterator() { + return new ListIterator(first); + } + + // 实现Iterator接口用于迭代,没有实现remove方法 + private class ListIterator implements Iterator { + private Node current; + + //初始化时,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; + } + } + + +} +} + + + + + +3.import java.util.Iterator; +import java.util.NoSuchElementException; + +public class c { + + public class ResizingArrayQueue implements Iterable { + private Item[] q; + private int N; + private int first; + private int last; + + + /** + * 初始化队列,此时头尾下标重合 + */ + public ResizingArrayQueue() { + q = (Item[]) new Object[2]; + N = 0; + first = 0; + last = 0; + } + + /** + * 依旧用N判断队列是否为空 + */ + public boolean isEmpty() { + return N == 0; + } + + /** + * 队列中元素数量 + */ + public int size() { + return N; + } + + // 调整数组大小 + private void resize(int max) { + assert max >= N; + Item[] temp = (Item[]) new Object[max]; + /**个元素放入总大小为max的队列 + *因为循环使用数组,从first开始的第i个元素可能保存在了first + *前面(即last在first前面)。 + */ + for (int i = 0; i < N; i++) { + temp[i] = q[(first + i) % q.length]; + } + q = temp; + //把小队列按顺序复制到大队列后重置队头和队尾 + first = 0; + last = N; + } + + /** + * 元素入列 + */ + public void enqueue(Item item) { + if (N == q.length) resize(2*q.length); + q[last++] = item; + if (last == q.length) last = 0; + N++; + } + + /** + * 元素出列 + */ + public Item dequeue() { + if (isEmpty()) throw new NoSuchElementException("Queue underflow"); + Item item = q[first]; + q[first] = null; + N--; + first++; + if (first == q.length) first = 0; + if (N > 0 && N == q.length/4) resize(q.length/2); + return item; + } + + /** + * 返回队头元素但不出列 + */ + public Item peek() { + if (isEmpty()) throw new NoSuchElementException("Queue underflow"); + return q[first]; + } + /** + * 实现Iterable接口 + */ + public Iterator iterator() { + return new ArrayIterator(); + } + + //实现迭代器 + private class ArrayIterator implements Iterator { + //维护一个i用于迭代 + private int i = 0; + public boolean hasNext() { return i < N; } + public void remove() { throw new UnsupportedOperationException(); } + + //直接利用first进行遍历,注意可能存在数组的循环利用 + public Item next() { + if (!hasNext()) throw new NoSuchElementException(); + Item item = q[(i + first) % q.length]; + i++; + return item; + } + } + + + } + + } + + + + +4.import java.util.Iterator; +import java.util.NoSuchElementException; + +public class d { + + public class Queue implements Iterable { + private Node first; + private Node last; + private int N; + + private class Node { + private Item item; + private Node next; + } + + /** + * 初始化队列 + */ + public Queue() { + first = null; + last = null; + N = 0; + } + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return N; + } + + /** + * 返回但不删除头元素 + */ + public Item peek() { + if (isEmpty()) throw new NoSuchElementException("Queue underflow"); + return first.item; + } + + /** + * 元素入列 + */ + public void enqueue(Item item) { + //记录尾节点 + Node oldlast = last; + //创建新的尾节点 + last = new Node(); + last.item = item; + last.next = null; + //如果队列是空的,将first置为last,因为这时候队列中只有一个元素 + if (isEmpty()) first = last; + //否则执行正常的在尾节点插入新节点的操作 + else oldlast.next = last; + N++; + } + + /** + *元素出列 + */ + public Item dequeue() { + if (isEmpty()) throw new NoSuchElementException("Queue underflow"); + //队头元素出列 + Item item = first.item; + first = first.next; + N--; + //如果这时候队列为空,表示原来只有一个元素,这时候也将last置为null + if (isEmpty()) last = null; + return item; + } + + public String toString() { + StringBuilder s = new StringBuilder(); + for (Item item : this) + s.append(item + " "); + return s.toString(); + } + public Iterator iterator() { + return new ListIterator(first); + } + + // 实现迭代 + private class ListIterator implements Iterator { + + private Node current; + //要实现迭代,我们只需要维护一个节点,并在开始的时候将它置为first + 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; + } + } + + } + } \ No newline at end of file