初识队列和栈

队列特点

先进先出

队列基本用法

 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);
    queue.add(6);
    System.out.println(queue.peek());//获取队首元素
    System.out.println(queue.poll());//删除队首元素
    System.out.println(queue.peek());
    System.out.println(queue.remove());//移除队首元素
    System.out.println(queue.peek());
    System.out.println(queue.element());//获取队首元素
    System.out.println(queue.size());//获取长度
    queue.clear();//清空
    System.out.println(queue.isEmpty());//判断是否有元素
    }

在这里插入图片描述

栈实现队列

import java.util.Stack;

public class MyQueue {
Stackstack1 = null;
Stackstack2 = null;
public MyQueue() {
stack1 = new Stack<>();
stack2 =new Stack<>();
}

/** Push element x to the back of queue. */
public void push(int x) {

if(stack1.isEmpty()&&stack2.isEmpty()){
stack1.push(x);
}else if(stack2.isEmpty()){
stack1.push(x);
}else if(stack1.isEmpty()){
stack2.push(x);
}
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stack1.size()==1){
    return stack1.pop();
}else if(!stack1.isEmpty()){
    while (stack1.size()>1){
        stack2.push(stack1.pop());
    }
    int i = stack1.pop();
    while (!stack2.isEmpty()) {
        stack1.push(stack2.pop());
    }
    return i;
}
return -1;
}

/** Get the front element. */
public int peek() {
  if(stack1.size()==1){
      return stack1.peek();
  }else if(!stack1.isEmpty()){
      while (stack1.size()>1){
          stack2.push(stack1.pop());
      }
      int i = stack1.pop();
      stack2.push(i);
      while (!stack2.isEmpty()){
          stack1.push(stack2.pop());
      }
      return i;
  }
  return -1;
}

/** Returns whether the queue is empty. */
public boolean empty() {

if(stack1.isEmpty()&&stack2.isEmpty()){
return true;
}
return false;
}
}


队列实现栈

import java.util.LinkedList;
import java.util.Queue;

//队列实现栈
public class MyStack {
    Queue<Integer>queue1 =null;
    Queue<Integer>queue2 = null;
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
  if(queue1.isEmpty()&&queue2.isEmpty()){
      queue1.offer(x);
  }else if(queue2.isEmpty()){
      queue1.offer(x);
  }else if(queue1.isEmpty()){
      queue2.offer(x);
  }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if(!queue1.isEmpty()){
            while (queue1.size()>1){
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        }else if(!queue2.isEmpty()){
            while (queue2.size()>1){
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        }
        return -1;
    }

    /** Get the top element. */
    public int top() {
        if(!queue1.isEmpty()){
            while (queue1.size()>1){
                queue2.offer(queue1.poll());
            }
            int i =queue1.peek();
            queue2.offer(queue1.poll());
            return i;
        }else if(!queue2.isEmpty()){
            while (queue2.size()>1){
                queue1.offer(queue2.poll());
            }
            int i = queue2.peek();
            queue1.offer(queue2.poll());
            return i;
        }
        return -1;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
         if(queue1.isEmpty()&&queue2.isEmpty()){
             return true;
         }
         return false;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值