栈和队列 方法复盘

在这里插入图片描述

耍一耍

package com.beyond;

import java.util.Stack;

public class StackTest {
    
    
    public static void main(String[] args) {
    
    
        Stack<String> stack = new Stack<>();
        Stack<String> stack1 = new Stack<>();
        stack.push("DHL");
        stack.push("CWQ");
        stack.push("XXI");
        System.out.println(stack);
        System.out.println(stack.peek());
        System.out.println(stack.search("XXI"));
        System.out.println(stack.search("CWQ"));
        System.out.println(stack.search("DHL"));
        stack1.push(stack.pop());
        stack1.push(stack.pop());
        stack1.push(stack.pop());
        System.out.println(stack.empty());
        System.out.println(stack1);
    }
}

队列

在这里插入图片描述

耍一耍

package com.beyond;

import java.util.ArrayDeque;
import java.util.Queue;

public class QueueTest {
    
    
    public static void main(String[] args) {
    
    
        Queue<String> queue = new ArrayDeque<>();
        queue.add("DHL");
        queue.offer("CWQ");
        System.out.println(queue);

        System.out.println(queue.element());
        System.out.println(queue.peek());

        queue.remove();
        System.out.println(queue);
        queue.poll();
        System.out.println(queue);

    }
}

猜你喜欢

转载自blog.csdn.net/Beyond_Nothing/article/details/114685148