剑指offer第二版——面试题9(java)

面试题9:用两个栈实现队列

题目描述: 
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deletedHead,分别完成在队列尾部插入节点和在队列头部删除节点的功能。

方法:

两个栈 stack1 & stack2

※ 插入时,直接放入stack1

※ 删除时,直接弹出pop2中的对象;如果pop2为空,则先将pop1中的对象放入stack2中,再从stack2里pop第一个

如:依次放入stack1中1-2-3,在弹出并放入stack2中时,顺序为3-2-1,stack2弹出时,弹出的为1

       此时插入4,放入stack1中,现在为stack2从顶到底为2-3,stack2中只有4,删除时弹出stack2中数字,即删除2,顺序正确,再删除3,顺序正确;插入5,放入stack1中,此时stack1中从顶到底为5-4,stack2中为空,若要删除,则将stack1中的5-4,放入stack1中为从顶到底4-5,pop的对象为4,顺序正确。

public class Q9 {
	public static void main(String[] args) {
        CQueue<Integer> queue = new CQueue<>();

        //进队列
        for(int i=0; i<5; i++){
            queue.appendTail(i);
        }

        //出队列
        for(int i=0; i<5; i++){
            int temp = queue.deleteHead();
            System.out.print(temp+ "  ");
        }
	}	
}

public class CQueue<E> {
	Stack<E> stack1;
	Stack<E> stack2;
	
	public CQueue(){
		stack1 = new Stack<>();
		stack2 = new Stack<>();
	}
	
	
        // 插入
	public void appendTail(E node) {
		stack1.push(node);
	}
	
        // 删除
	public E deleteHead() {
		if(!stack2.empty()) {
			return stack2.pop();
		}else {
			if(stack1.empty()) {
				return null;
			}
			while(!stack1.empty()) {
				E temp = stack1.pop();
				stack2.push(temp);
			}
			return stack2.pop();
		}
	}
}

相关问题:用两个队列实现一个栈

方法:

插入:放入不为空的那个队列

删除:将不为空的队列中元素,依次移入另一个队列,并在temp=queue.poll()时,检查是否队列为空,如果队列为空,则temp为最后一个元素,需要删除。

public class Q9 {
	public static void main(String[] args) {
		Q9Stack<Integer> queue = new Q9Stack<>();

        //进队列  插入0~4
        for(int i=0; i<5; i++){
            queue.appendTail(i);
        }

        //出队列 4 3 2
        for(int i=0; i<3; i++){
            int temp = queue.deleteHead();
            System.out.print(temp+ "  ");
        }
        
        System.out.println("\n----");
        // 进队列5 6
        queue.appendTail(5);
        queue.appendTail(6);
        
        // 出队列
        for(int i=0; i<4; i++){
            int temp = queue.deleteHead();
            System.out.print(temp+ "  ");
        }
	}	
}

public class Q9Stack<E> {
	Queue<E> queue1;
	Queue<E> queue2;
	
	public Q9Stack(){
		queue1 = new LinkedList<E>();
		queue2 = new LinkedList<E>();
	}
	
	
	public void appendTail(E node) {
		if(queue1.isEmpty()) {
			//System.out.println("queue2");
			queue2.offer(node);
		}else {
			//System.out.println("queue1");
			queue1.offer(node);
		}
	}
	
	public E deleteHead() {
		// 如果队列1为空 则队列2不为空
		if(queue1.isEmpty()) {
			while(!queue2.isEmpty()) {
				E tempE = queue2.poll();
				if(queue2.isEmpty()) {
					return tempE;
				}else {
					queue1.offer(tempE);
				}
			}
		}else {
			while(!queue1.isEmpty()) {
				E tempE = queue1.poll();
				if(queue1.isEmpty()) {
					return tempE;
				}else {
					queue2.offer(tempE);
				}
			}
		}
		return null;
	}		
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值