- import java.util.*;
-
- /**
- * @author zhangchaoliang
- * create 2022
- */
- public class DoubleEndsQueueToStackAndQueue {
- public static class Node
{ - public T value;
- public Node
last; - public Node
next; -
- public Node(T data){
- value=data;
- }
- }
-
- public static class DoubleEndsQueue
{ - public Node
head; - public Node
tail; -
- public void addFromHead(T value){
- Node
cur = new Node<>(value); - if (head==null){
- head=cur;
- tail=cur;
- }else {
- cur.next=head;
- head.last=cur;
- head=cur;
- }
- }
-
- public void addFromBottom(T value){
- Node
cur = new Node<>(value); - if (head==null){
- head=cur;
- tail=cur;
- }else {
- cur.last=tail;
- tail.next=cur;
- tail=cur;
- }
- }
-
- public T popFromHead(){
- if (head==null)
- return null;
- Node
cur = head; - if (head==tail){
- head=null;
- tail=null;
- }else {
- head=head.next;
- cur.next=null;
- head.last=null;
- }
- return cur.value;
- }
-
- public T popFromBottom(){
- if (head==null){
- return null;
- }
- Node
cur = tail; - if (head==tail){
- head=null;
- tail=null;
- }else {
- tail=tail.last;
- tail.next=null;
- cur.last=null;
- }
- return cur.value;
- }
-
- public boolean isEmpty(){
- return head==null;
- }
- }
-
- public static class MyStack
{ - private DoubleEndsQueue
queue; -
- public MyStack(){
- queue=new DoubleEndsQueue<>();
- }
-
- public void push(T value){
- queue.addFromHead(value);
- }
-
- public T pop(){
- return queue.popFromHead();
- }
-
- public boolean isEmpty(){
- return queue.isEmpty();
- }
- }
-
- public static class MyQueue
{ - private DoubleEndsQueue
queue; -
- public MyQueue(){
- queue=new DoubleEndsQueue<>();
- }
-
- public void push(T value){
- queue.addFromHead(value);
- }
-
- public T poll(){
- return queue.popFromBottom();
- }
-
- public boolean isEmpty(){
- return queue.isEmpty();
- }
- }
-
- public static boolean isEqual(Integer o1,Integer o2){
- if (o1==null&&o2!=null)
- return false;
- if (o1!=null&&o2==null)
- return false;
- if (o1==null&&o2==null)
- return true;
- return o1.equals(o2);
- }
-
- public static void main(String[] args) {
- int oneTestDataNum=100;
- int value = 10000;
- int testTimes = 100000;
- for (int i=0;i
- MyStack
myStack = new MyStack<>(); - MyQueue
myQueue = new MyQueue<>(); - Stack
stack = new Stack<>(); - Queue
queue = new LinkedList<>(); - for (int j=0;j
- int nums = (int)(Math.random()*value);
- if (stack.isEmpty()){
- myStack.push(nums);
- stack.push(nums);
- }else {
- if (Math.random()<0.5){
- myStack.push(nums);
- stack.push(nums);
- }else {
- if (!isEqual(myStack.pop(),stack.pop()))
- System.out.println("Oops!");
- }
- }
-
- int numq = (int)(Math.random()*value);
- if (queue.isEmpty()){
- myQueue.push(numq);
- queue.offer(numq);
- }else {
- if (Math.random()<0.5){
- myQueue.push(numq);
- queue.offer(numq);
- }else {
- if (!isEqual(myQueue.poll(),queue.poll()))
- System.out.println("Oops!");
- }
- }
- }
- }
- System.out.println("finish!");
- }
- }
二、数组表示队列
- public class RingArray {
- public static class MyQueue{
- private int[] arr;
- private int pushi;
- private int polli;
- private int size;
- private final int limit;
-
- public MyQueue(int limit){
- arr = new int[limit];
- pushi=0;
- polli=0;
- size=0;
- this.limit=limit;
- }
-
- public void push(int value){
- if (size==limit){
- throw new RuntimeException("队列满了,不能在加了!");
- }
- size++;
- arr[pushi]=value;
- pushi=nextIndex(pushi);
- }
- public int pop(){
- if (size==0){
- throw new RuntimeException("队列空了,不能再拿了!");
- }
- size--;
- int ans = arr[polli];
- polli=nextIndex(polli);
- return ans;
- }
-
- public boolean isEmpty(){
- return size==0;
- }
-
- private int nextIndex(int i){
- return i
1?i+1:0; - }
- }
-
- }
三、使用栈模拟队列
- public class TwoStacksImplementQueue {
- public static class TwoStacksQueue{
- public Stack
stackPush; - public Stack
stackPop; -
- public TwoStacksQueue(){
- stackPush=new Stack
(); - stackPop=new Stack
(); - }
-
- private void pushToPop(){
- if (stackPop.isEmpty()){
- while (!stackPush.empty()){
- stackPop.push(stackPush.pop());
- }
- }
- }
-
- public void add(int pushInt){
- stackPush.push(pushInt);
- pushToPop();
- }
-
- public int poll(){
- if (stackPop.empty()&&stackPush.empty()){
- throw new RuntimeException("Queue is empty!");
- }
- pushToPop();
- return stackPop.pop();
- }
-
- public int peek(){
- if (stackPop.empty()&&stackPush.empty()){
- throw new RuntimeException("Queue is empty!");
- }
- pushToPop();
- return stackPop.peek();
- }
- }
- public static void main(String[] args) {
- TwoStacksQueue test = new TwoStacksQueue();
- test.add(1);
- test.add(2);
- test.add(3);
- System.out.println(test.peek());
- System.out.println(test.poll());
- System.out.println(test.peek());
- System.out.println(test.poll());
- System.out.println(test.peek());
- System.out.println(test.poll());
- }
- }
四、使用队列模拟栈
- public class TwoQueueImplementStack {
-
- public static class TwoQueueStack
{ - public Queue
queue; - public Queue
help; -
- public TwoQueueStack() {
- queue = new LinkedList<>();
- help = new LinkedList<>();
- }
-
- public void push(T value) {
- queue.offer(value);
- }
-
- public T poll() {
- while (queue.size() > 1) {
- help.offer(queue.poll());
- }
- T ans = queue.poll();
- Queue
tmp = queue; - queue = help;
- help = tmp;
- return ans;
- }
-
- public T peek() {
- while (queue.size() > 1) {
- help.offer(queue.poll());
- }
- T ans = queue.poll();
- help.offer(ans);
- Queue
tmp = queue; - queue = help;
- help = tmp;
- return ans;
- }
-
- public boolean isEmpty() {
- return queue.isEmpty();
- }
-
- }
-
- public static void main(String[] args) {
- System.out.println("test begin");
- TwoQueueStack
myStack = new TwoQueueStack<>(); - Stack
test = new Stack<>(); - int testTime = 1000000;
- int max = 1000000;
- for (int i = 0; i < testTime; i++) {
- if (myStack.isEmpty()) {
- if (!test.isEmpty()) {
- System.out.println("Oops");
- }
- int num = (int) (Math.random() * max);
- myStack.push(num);
- test.push(num);
- } else {
- if (Math.random() < 0.25) {
- int num = (int) (Math.random() * max);
- myStack.push(num);
- test.push(num);
- } else if (Math.random() < 0.5) {
- if (!myStack.peek().equals(test.peek())) {
- System.out.println("Oops");
- }
- } else if (Math.random() < 0.75) {
- if (!myStack.poll().equals(test.pop())) {
- System.out.println("Oops");
- }
- } else {
- if (myStack.isEmpty() != test.isEmpty()) {
- System.out.println("Oops");
- }
- }
- }
- }
-
- System.out.println("test finish!");
-
- }
-
- }
五、找出栈中的最小值
- public class GetMinStack {
- public static class MyStack1{
- private Stack
stackData; - private Stack
stackMin; -
- public MyStack1(){
- this.stackData=new Stack
(); - this.stackMin=new Stack
(); - }
-
- public void push(int newNum){
- if (this.stackMin.isEmpty()){
- this.stackMin.push(newNum);
- }else if (newNum<=this.getmin()){
- this.stackMin.push(newNum);
- }
- this.stackData.push(newNum);
- }
-
- public int pop(){
- if (this.stackData.isEmpty()){
- throw new RuntimeException("You stack is Empty.");
- }
- int value = this.stackData.pop();
- if (value==this.getmin()){
- this.stackMin.pop();
- }
- return value;
- }
-
- public int getmin(){
- if (this.stackMin.isEmpty()){
- throw new RuntimeException("You stack is empty.");
- }
- return this.stackMin.peek();
- }
- }
-
- public static class MyStack2{
- private Stack
stackData; - private Stack
stackMin; -
- public MyStack2(){
- this.stackData=new Stack
(); - this.stackMin=new Stack
(); - }
-
- public void push(int newNum){
- if (this.stackMin.isEmpty()){
- throw new RuntimeException("You stack is empty.");
- }else if (newNum<this.getMin()){
- this.stackMin.push(newNum);
- }else {
- newNum=this.stackMin.peek();
- this.stackMin.push(newNum);
- }
- this.stackData.push(newNum);
- }
-
- public int pop(){
- if (this.stackData.isEmpty()){
- throw new RuntimeException("You stack is empty.");
- }
- this.stackMin.pop();
- return this.stackData.pop();
- }
-
- public int getMin(){
- if (this.stackMin.isEmpty()){
- throw new RuntimeException("You stack is empty.");
- }
- return this.stackMin.peek();
- }
- }
- public static void main(String[] args) {
- MyStack1 stack1 = new MyStack1();
- stack1.push(3);
- System.out.println(stack1.getmin());
- stack1.push(4);
- System.out.println(stack1.getmin());
- stack1.push(1);
- System.out.println(stack1.getmin());
- System.out.println(stack1.pop());
- System.out.println(stack1.getmin());
-
- System.out.println("=============");
-
- MyStack1 stack2 = new MyStack1();
- stack2.push(3);
- System.out.println(stack2.getmin());
- stack2.push(4);
- System.out.println(stack2.getmin());
- stack2.push(1);
- System.out.println(stack2.getmin());
- System.out.println(stack2.pop());
- System.out.println(stack2.getmin());
- }
- }
-
相关阅读:
Android 应用框架层 SQLite 源码分析
DNS解析为什么不生效?DNS解析不生效原因分析
数据结构-并查集刷题
RestTemplate配置和使用
MySQL—MySQL架构
「从零单排canal 07」 parser模块源码解析
非线性二分类——机器学习
算法|最大堆、最小堆和堆排序的实现(JavaScript)
“摸鱼”就能得出设计灵感?他的经验分享得看
23种设计模式之 : 模板方法设计模式
-
原文地址:https://blog.csdn.net/z1171127310/article/details/126841788