Javaの学習 - データ構造 - 2つのキュー実装スタック、2つのスタックキュー

2つのキューには、2つのスタックキューをスタックを実装しました

1、2つのスタックキューの実装

スタックプッシュ()、ポップ():2つのキューに実装してください。

ステップリファレンス:

(1)従来の2つのキューQ1及びQ2は、Q1スタックは、要素に適用される
第一の解釈は、スタックが空Q1、Q1であるか否かの要素は常に、後LIFO来ているので場合を除いて、(2)キューの最後の要素は、Q2に他の要素を追加するために、最後の素子Q1デキュー
スタック(3)のうち場合が判定Q1 2にQ2他の要素を追加し、最後素子Q2を除いて、空である場合Q1、Q2のチームの最後の要素

/**
 * 两个队列实现栈
 * 
 * @author Linlin Zhao
 * 
 */
public class StackAndQueue02 {
	public Queue<Integer> queue1;
	public Queue<Integer> queue2;

	public StackAndQueue02() {
		queue1 = new LinkedList<Integer>();
		queue2 = new LinkedList<Integer>();
	}

	/**
	 * 压栈(入队queue1)
	 * 
	 * @param num
	 * @return
	 */
	public void push(int num) {
			queue1.offer(num);
	}
	
	/**
	 * 弹栈
	 * @return
	 */
	public Integer pop() {
		 if (queue1.isEmpty() && queue2.isEmpty()) {
	            return null;
	        }
	        // 先判断 q1 是否为空 
	        if (!queue1.isEmpty()) {
	            int size = queue1.size();
	            for (int i = 0; i < size - 1; i++) {//其他元素转移
	                queue2.offer(queue1.poll());
	            }
	            return queue1.poll();//最后一个元素出队
	        } else {
	            int size = queue2.size();
	            for (int i = 0; i < size - 1; i++) {
	                queue1.offer(queue2.poll());
	            }
	            return queue2.poll();
	        }

	}

	public static void main(String[] args) {
		StackAndQueue02 myStack=new StackAndQueue02();
		myStack.push(3);
		myStack.push(6);
		myStack.push(8);
		myStack.push(3);
		myStack.push(2);
		myStack.push(0);
		myStack.push(1);
		
		System.out. println(myStack.pop());
		myStack.pop();
		System.out. println(myStack.pop());
			
	}
}

2、2つのスタックキュー

実装2つのスタックを使用してください:追加のキューを()、ポール()、PEEK()。

ステップリファレンス:

(1)2つのスタックstack1 stack2とがある
ライン上のみstack1添加元素にキューと(2)
第一キューが空である(3)が決定stack2は、要素のstack2がFIFOに進んでいます。stack2が空でない場合は、ポップ先頭の要素stack2を指示。あなたはstack2に追加する場合は空である、そしてstack1要素は、その後、トップ要素stack2をポップ

/**
 * 两个栈实现队列
 * 
 */
public class StackAndQueue {
	public Stack<Integer> stack1;
	public Stack<Integer> stack2;

	public StackAndQueue() {
		stack1 = new Stack<Integer>();
		stack2 = new Stack<Integer>();
	}

	// 入队列
	public boolean add(int num) {
		stack1.push(num);
		return true;
	}

	// 获取队首元素
	public int peek() {
		while (!stack2.isEmpty()) {
			return stack2.pop();
		}
		while (!stack1.isEmpty()) {
			stack2.push(stack1.pop());
		}
		return stack2.peek();
	}

	// 获取队首元素,并移除
	public int poll() {
		while (!stack2.isEmpty()) {
			return stack2.pop();
		}
		while (!stack1.isEmpty()) {
			stack2.push(stack1.pop());
		}
		return stack2.pop();

	}

	public static void main(String[] args) {
		StackAndQueue myQueue =new StackAndQueue();
		myQueue.add(9);
		myQueue.add(5);
		myQueue.add(8);
		myQueue.add(1);
		myQueue.add(3);
		myQueue.add(2);
		
		System.out.println(myQueue.peek());
		System.out.println(myQueue.poll());
		System.out.println(myQueue.poll());
	
	}

}

3、Dueueアナログ実装スタック

/**
 * 使用队列实现自定义栈
 * 1、弹栈
 * 2、压栈
 * 3、获取头
 * @author Linlin Zhao
 *
 */
public class MyStack01<E> {

	//容器
	private Deque<E> container =new ArrayDeque<E>();
	//容量
	private int cap;
	
	public MyStack01(int cap) {
		super();
		this.cap = cap;
	}
	
	//压栈
	public boolean push(E e){
		if(container.size()+1>cap){
			return false;
		}
		return container.offerLast(e);//加到队尾
	}
	
	//弹栈
	public E pop(){
		return container.pollLast();//移除队尾
	}
	
	//获取
	public E peek(){
		return container.peekLast();//获取队尾
	}

	public int size(){
		return this.container.size();//队列长度
	}

	public static void main(String[] args) {
		
	}
}
公開された57元の記事 ウォン称賛13 ビュー1089

おすすめ

転載: blog.csdn.net/weixin_42924812/article/details/105316558