import java.util.EmptyStackException;
/**
* 用顺序映像实现Stack
* @param <E>
*/
public class SequentialStack<E> {
private E[] elements;
private int top = -1;
private int size;
private final static int INITIAL_CAPACITY = 16;
private final static int MAX_CAPACITY = Integer.MAX_VALUE - 8;
@SuppressWarnings("unchecked")
public SequentialStack() {
elements =(E[]) new Object[INITIAL_CAPACITY];
}
@SuppressWarnings("unchecked")
public SequentialStack(int capacity) {
if (capacity < 0 || capacity > Integer.MAX_VALUE - 8) {
throw new IndexOutOfBoundsException("capacity=" + capacity);
}
elements =(E[]) new Object[capacity];
}
//判空
public boolean isEmpty() {
return top == -1;
}
//入栈
public void push(E e) {
if (size == elements.length) {
int newLength=calculateCapacity();
grow(newLength);
}
elements[++top]=e;
size++;
}
//出栈
public E pop() {
if(isEmpty()){
throw new EmptyStackException();
}
E value = elements[top];
elements[top--]=null;
return value;
}
//获取栈顶值
public E peek(){
if(isEmpty()){
throw new EmptyStackException();
}
E value = elements[top];
return value;
}
//扩容
@SuppressWarnings("unchecked")
private void grow(int newLength) {
E[] newArr = (E[]) new Object[newLength];
for (int i = top; i >=0 ; i--) {
newArr[i]=elements[i];
}
elements=newArr;
}
//计算新数组长度
private int calculateCapacity() {
if(size==MAX_CAPACITY){
throw new ArrayStoreException();
}
int len=elements.length+(elements.length>>1);
if(len>MAX_CAPACITY||len<=0){
len=MAX_CAPACITY;
}
return len;
}
public static void main(String[] args) {
SequentialStack<String> ss=new SequentialStack<>(6);
System.out.println(ss.isEmpty());
//ss.pop();
ss.push("FGO"); //java.lang.ArrayStoreException
ss.push("战舰少女R");
ss.push("明日方舟");
System.out.println(ss.peek());
ss.push("万象物语");
ss.pop();
System.out.println(ss.peek());
/*SequentialStack<Character> ss=new SequentialStack<>(6);
System.out.println(ss.isEmpty());
//ss.pop();
ss.push('a'); //java.lang.ArrayStoreException: java.lang.Character
ss.push('b');
ss.push('c');
System.out.println(ss.peek());*/
}
}
import exception.EmptyQueueException;
/**
* 用非顺序映像实现Queue
*/
public class LinkedQueue<E> {
private Node front=new Node(null); //插入的队头结点
private Node rear=new Node(null); //删除的队尾节点
private class Node{
E val;
Node next;
Node prev;
Node(E val){
this.val=val;
}
Node(Node prev,E val,Node next){
this.prev=prev;
this.val=val;
this.next=next;
}
}
LinkedQueue(){
front.prev=rear;
rear.next=front;
}
//判空
public boolean isEmpty(){
return rear.next==front;
}
//入队
public void enQueue(E e){
Node newNode = new Node(e);
newNode.prev=front.prev;
front.prev.next=newNode;
newNode.next=front;
front.prev=newNode;
}
//出队
public E deQueue(){
if(isEmpty()){
throw new EmptyQueueException();
}
E value=rear.next.val;
rear.next.val=null;
rear.next=rear.next.next;
rear.next.prev=rear;
return value;
}
public String toSting(){
StringBuilder sb=new StringBuilder("[");
Node p=front.prev;
while(p!=rear){
if(p!=front.prev){
sb.append(",");
}
sb.append(p.val);
p=p.prev;
}
return sb.append("]").toString();
}
public static void main(String[] args) {
LinkedQueue queue = new LinkedQueue();
System.out.println(queue.isEmpty());
queue.enQueue("德克萨斯");
queue.enQueue("能天使");
queue.enQueue("拉普兰德");
System.out.println(queue.toSting());
System.out.println(queue.deQueue());
queue.enQueue("空");
queue.enQueue("可颂");
System.out.println(queue.toSting());
}
}
package exception;
public class EmptyQueueException extends RuntimeException{
public EmptyQueueException() {
super();
}
public EmptyQueueException(String message) {
super(message);
}
}