- 用顺序映像实现栈。
package week7.day36;
import java.util.EmptyStackException;
public class MyStack<E> {
private static final int DEFAULT_CAPACITY = 16;
private static final int MAX_CAPACITY = Integer.MAX_VALUE - 8;
int size;
Object[] elements;
public MyStack() {
elements = new Object[DEFAULT_CAPACITY];
}
public MyStack(int initialCapacity) {
if (initialCapacity <= 0 || initialCapacity > MAX_CAPACITY) {
throw new IllegalArgumentException("initialCapacity=" + initialCapacity);
}
elements = new Object[initialCapacity];
}
public void push(E e) {
if (size == elements.length) {
int newCapacity = calculateCapacity();
grow(newCapacity);
}
elements[size++] = e;
}
private int calculateCapacity() {
if (size == MAX_CAPACITY) {
throw new StackOverflowException();
}
int newCapacity = (elements.length << 1);
if (newCapacity < 0 || newCapacity > MAX_CAPACITY) {
newCapacity = MAX_CAPACITY;
}
return newCapacity;
}
private void grow(int newCapacity) {
Object[] newArr = new Object[newCapacity];
System.arraycopy(newArr, 0, elements, 0, elements.length);
elements = newArr;
}
@SuppressWarnings("unchecked")
public E pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
return (E) elements[--size];
}
@SuppressWarnings("unchecked")
public E peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return (E) elements[size-1];
}
public boolean isEmpty() {
return size == 0;
}
}
class Test2 {
public static String reverse(String original) {
MyStack<Character> stack = new MyStack<>();
for (int i = 0; i < original.length(); i++) {
char c = original.charAt(i);
stack.push(c);
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.toString();
}
public static void main(String[] args) {
String s = "abc";
System.out.println(reverse(s));
}
}
package week7.day36;
public class StackOverflowException extends RuntimeException {
public StackOverflowException() {
}
public StackOverflowException(String message) {
super(message);
}
}
- 用非顺序映像实现队列。
package week7.day36;
import java.util.NoSuchElementException;
public class MyQueue<E> {
private int size;
private Node front;
private Node rear;
public MyQueue() {
front = new Node();
rear = new Node();
front.next = rear;
}
public MyQueue(E value) {
front = new Node();
rear = new Node(value);
front.next = rear;
size++;
}
public MyQueue<E> enqueue(E e) {
rear=rear.next = new Node(e);
size++;
return this;
}
@SuppressWarnings("unchecked")
public E dequeue() {
if (isEmpty()) {
throw new NoSuchElementException();
}
E removeValue = (E) front.next.value;
front.next = front.next.next;
size--;
return removeValue;
}
@SuppressWarnings("unchecked")
public E peek() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return (E) front.next.value;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
@Override
public String toString() {
return "MyQueue{" + front.next + '}';
}
private static class Node {
Object value;
Node next;
public Node() {
}
public Node(Object value) {
this.value = value;
}
public Node(Object value, Node next) {
this.value = value;
this.next = next;
}
@Override
public String toString() {
if (this.next == null) {
return "" + value;
}
return value + "-->" + this.next.toString();
}
}
}
class Test3 {
public static void main(String[] args) {
MyQueue<Integer> queue = new MyQueue<>(1);
queue.enqueue(2).enqueue(3).enqueue(4).enqueue(5);
System.out.println(queue);
System.out.println(queue.dequeue());
System.out.println(queue);
System.out.println(queue.peek());
}
}