队列

创建一个队列

package 队列;

//定义一个结点
class Node{
    Node next = null;
    Integer data;
    public Node(Integer data){
        this.data = data;
    }
}

public class Queue {
    Node head = null;
    Node tail = null;
    
    //判断是否为空
    public boolean isEmpty(){
        return head == null;
    }
    
    //添加元素
    public void push(Integer data){
        Node node = new Node(data);
        if(isEmpty()){
            head = tail = node;
            return;
        }
        tail.next = node;
        tail = node;
    }
    
    //取出元素
    public Integer pop(){
        if(isEmpty()){
            return null;
        }
        
        Integer data = head.data;
        head = head.next;
        return data;
    }
    
    //取队列峰顶
    public Integer peek(){
        if(isEmpty()){
            return null;
        }
        return head.data;
    }
    
    //计算队列中元素的个数
    public Integer size(){
        Node tmp = head;
        Integer size = 0;
        while(tmp!=null){
            size++;
            tmp = tmp.next;
        }
        return size;
    }
    
    //打印队列
    public void printQueue(){
        Node tmp = head;
        while(tmp!=null){
            System.out.println(tmp.data);
            tmp = tmp.next;
        }
    }
    
    //主函数
    public static void main(String[] args) {
        Queue queue = new Queue();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        System.out.println("队列里元素的个数:"+queue.size());
        System.out.println("队列的峰值:"+queue.peek());
        queue.printQueue();
    }
}

 

 

两个队列生成一个栈

package 队列;

/**两个队列生成一个栈
 * 
 *思路:队列1和队列2在任意时刻至少有一个为空,即如果有元素,所有元素只能在一个队列中。
 *当有元素需要插入时,将元素插入到空队列中,并将另一非空队列的所有元素全部转移到该队列
 *中。于是,插入的元素添加到了队列的最前面。
 */
public class Stack {
    Queue queue1 = new Queue();
    Queue queue2 = new Queue();
    
    //判断非空
    public boolean isEmpty(){
        return queue1.isEmpty() && queue2.isEmpty();
    }
    
    //添加元素
    public void push(Integer data){
        if(queue1.isEmpty()){
            queue1.push(data);
            while(!queue2.isEmpty()){
                queue1.push(queue2.pop());
            }
        }else{
            queue2.push(data);
            while(!queue1.isEmpty()){
                queue2.push(queue1.pop());
            }
        }
    }
    
    //从栈中取元素
    public Integer pop(){
        if(queue1.isEmpty() && queue2.isEmpty()){
            return null;
        }
        if(!queue1.isEmpty()){
            return queue1.pop();
        }else{
            return queue2.pop();
        }
    }
    
    //取峰值
    public Integer peek(){
        if(queue1.isEmpty()&&queue2.isEmpty()){
            return null;
        }
        if(!queue1.isEmpty()){
            return queue1.peek();
        }else{
            return queue2.peek();
        }
    }
    
    //主函数
    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        while(!stack.isEmpty()){
            System.out.println(stack.pop());
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值