栈、队列、循环队列

package dataStructure;
import java.util.Arrays;
/**
 * 栈、队列
 */

class OrderStack<T>{
    
    
    private int top; //栈顶指针
    private T[] stackArrays;
    private static final int defaultcapacity = 5;

    public OrderStack(){
    
    
        this(defaultcapacity);
    }

    public OrderStack(int capacity){
    
    
        this.stackArrays = (T[])new Object[capacity];
    }

    //入栈
    public void push(T value){
    
    
        //判满
        if(isFull()){
    
    
            //扩容
//            T[] destArray = (T[])new Object[stackArrays.length*2];
//            System.arraycopy(stackArrays, 0, destArray, 0, stackArrays.length);
//            this.stackArrays = destArray;
            this.stackArrays = Arrays.copyOf(stackArrays, stackArrays.length*2);
        }
        this.stackArrays[top++] = value;
    }

    public boolean isFull(){
    
    
        return top == stackArrays.length;
    }

    //出栈
    public T pop(){
    
    
        //判空
        if(isEmpty()){
    
    
            throw new UnsupportedOperationException("the stack has been empty");
        }
        T result = stackArrays[top-1];
        stackArrays[--top] = null; //方便垃圾回收
        return result;
    }

    public boolean isEmpty(){
    
    
        return top == 0;
    }

    //获取栈顶元素
    public T peek(){
    
    
        return stackArrays[top-1];
    }

    //遍历输出栈内元素
    public void show(){
    
    
        for(int i=top-1; i>=0; i--){
    
    
            System.out.print(stackArrays[i] + " ");
        }
        System.out.println();
    }
}

class LoopQueue<T>{
    
    
    private int header; //队头位置
    private int tail; //队尾位置
    private T[] queueArrays;
    private static final int defaultcapacity = 5;

    public LoopQueue(){
    
    
        this(defaultcapacity);
    }

    public LoopQueue(int capacity){
    
    
        this.queueArrays = (T[])new Object[capacity];
    }

    //入队
    public boolean enqueue(T value){
    
    
        //判满
        if(isFull()){
    
    
            //扩容
            queueArrays = Arrays.copyOf(queueArrays, queueArrays.length*2);
        }
        queueArrays[tail] = value;
        tail = (tail+1) % queueArrays.length;
        return true;
    }

    public boolean isFull(){
    
    
        return (tail+1)%queueArrays.length == header;
    }

    //出队
    public T dequeue(){
    
    
        if(isEmpty()){
    
    
            throw new UnsupportedOperationException("the queue has been empty");
        }
        T result = queueArrays[header];
        queueArrays[header] = null;
        header = (header+1) % queueArrays.length;
        return result;
    }

    public boolean isEmpty(){
    
    
        return header == tail;
    }

    public void show(){
    
    
        for(int i=header; i<tail; i=(i+1)%queueArrays.length){
    
    
            System.out.print(queueArrays[i]+" ");
        }
        System.out.println();
    }
}

class OrderQueue<T>{
    
    
    private int header; //队头位置
    private int tail; //队尾位置
    private int size; //有效元素
    private T[] queueArrays;
    private static final int defaultcapacity = 5;

    public OrderQueue(){
    
    
        this(defaultcapacity);
    }

    public OrderQueue(int capacity){
    
    
        this.queueArrays = (T[])new Object[capacity];
    }

    //入队
    public boolean enqueue(T value){
    
    
        //判满
        if(isFull()){
    
    
            //扩容
            if(size == queueArrays.length){
    
    
                queueArrays = Arrays.copyOf(queueArrays, queueArrays.length*2);
            }else{
    
    
                System.arraycopy(queueArrays, header, queueArrays, 0, size);
            }
        }
        queueArrays[tail++] = value;
        size++;
        return true;
    }

    public boolean isFull(){
    
    
        return tail == queueArrays.length;
    }

    //出队
    public T dequeue(){
    
    
        if(isEmpty()){
    
    
            throw new UnsupportedOperationException("the queue has been empty");
        }
        T result = queueArrays[header];
        queueArrays[header++] = null;
        size--;
        return result;
    }

    public boolean isEmpty(){
    
    
        return size == 0;
    }

    public void show(){
    
    
        for(int i=header; i<tail; i++){
    
    
            System.out.print(queueArrays[i]+" ");
        }
        System.out.println();
    }
}

public class TestDemo8 {
    
    
    public static void main(String[] args) {
    
    
        OrderStack<Integer> stack = new OrderStack<>();
        stack.push(10);
        stack.push(20);
        stack.push(30);
        stack.push(40);
        stack.push(50);
        stack.push(60);
        stack.show();
        stack.pop();
        stack.show();
        System.out.println(stack.peek());
        stack.show();

        System.out.println("==============");

        OrderQueue<String> queue = new OrderQueue<>();
        queue.enqueue("zjhjdhjhh");
        queue.enqueue("dhjhh");
        queue.enqueue("jhjdhjhh");
        queue.enqueue("dhjhh");
        queue.enqueue("zdhjhh");
        queue.enqueue("zjhdhjhh");
        queue.enqueue("zjhhjhh");
        queue.show();
        queue.dequeue();
        queue.dequeue();
        queue.show();
        System.out.println("==============");

        LoopQueue<Integer> loopQueue = new LoopQueue<>();
        loopQueue.enqueue(10);
        loopQueue.enqueue(20);
        loopQueue.enqueue(30);
        loopQueue.enqueue(40);
        loopQueue.enqueue(50);
        loopQueue.dequeue();
        loopQueue.dequeue();
        loopQueue.dequeue();
        loopQueue.show();
        loopQueue.enqueue(100);
        loopQueue.enqueue(200);
        loopQueue.show();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_47198561/article/details/110405617