package demo; import java.util.Stack; public class MyStack { public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); /*回顾stack的所有方法*/ /*idea标记黑色的有这五个方法*/ /*1. push压栈*/ stack.push(1); stack.push(2); stack.push(3); stack.push(4); System.out.println(stack); /*2.判断是否为空*/ boolean empty = stack.empty(); System.out.println(empty); /* 3. 观看栈顶元素*/ Integer peek = stack.peek(); System.out.println("peek:"+peek); System.out.println("观看栈顶之后的栈"); System.out.println(stack); /* 4.取出栈顶元素*/ Integer pop = stack.pop(); System.out.println("pop:"+pop); System.out.println("弹栈之后的栈"); System.out.println(stack); /*搜索元素,有则给出差距和 public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; }*/ int search = stack.search(1); System.out.println(search); } } /*总结:最主要的为五个方法 * 1.push pop peek empty size */ package demo; import java.util.LinkedList; import java.util.Queue; public class MyQueue { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<>(); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); System.out.println(queue); /*是否为空*/ Integer element = queue.element(); System.out.println("element:"+element); boolean empty = queue.isEmpty(); System.out.println("isEmpty:"+empty); queue.offer(1); System.out.println(queue); Integer peek = queue.peek(); System.out.println("peek:"+peek); System.out.println("--------------------"); queue.poll(); System.out.println(queue); queue.remove(); System.out.println(queue); } /*总结:方法主要有1.add.offer方法添加 * 2.poll remove方法删除 * 3. 查看队列的顶部元素peek*/ }