顺序栈和非顺序队列

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值