アルゴリズム第4版1.3リュックサック、キューとスタック:練習問題1.3.48
1629 ワード
import edu.princeton.cs.algs4.StdOut;
public class E1_3_48 - {
public static void main(String[]args){
E1_3_48
twoStackWithDeque=new E1_3_48<>();
for (int i=0;i<5;i++)// 0-4
twoStackWithDeque.pushLeftStack(i);
for (int i=5;i<10;i++)// 5-9
twoStackWithDeque.pushRightStack(i);
for (int i:twoStackWithDeque.deque)// , 4,3,2,1,0,5,6,7,8,9
StdOut.print(i+" ");
StdOut.println();
int count=twoStackWithDeque.LeftStackSize();
for (int i=0;i deque=new Deque<>();
private int LeftCount;
private int RightCount;
private class Node{
Item item;
Node next;
}
public boolean LeftStackIsEmpty(){return LeftCount==0;}
public boolean RightStackIsEmpty(){return RightCount==0;}
public int LeftStackSize(){return LeftCount;}
public int RightStackSize(){return RightCount;}
public void pushLeftStack(Item item){
deque.pushLeft(item);
LeftCount++;
}
public void pushRightStack(Item item){
deque.pushRight(item);
RightCount++;
}
public Item popLeftStack(){
if (LeftStackIsEmpty())return null;
LeftCount--;
return deque.popLeft();
}
public Item popRightStack(){
if (RightStackIsEmpty())return null;
RightCount--;
return deque.popRight();
}
}