您的位置:首页 > 编程语言 > Java开发

fw12013年12月13日1:17:54 - multi-stack -java

2013-12-13 14:19 204 查看
package test_kepler;

import java.util.ArrayList;

import java.util.Stack;

// the last stack is important ; try to implement it via queue the first queue will be very important

public class wtfStack {

static int max_stack_size = 10;

ArrayList<Stack> stacks = new ArrayList<Stack>();

public wtfStack()

{

Stack stack = new Stack();

stacks.add(stack);

}

public Stack getLast()

{

return (Stack)stacks.get(stacks.size()-1);

}

public void push(int k)

{

if(getLast().size() == max_stack_size )

{

Stack newStack = new Stack();

stacks.add(newStack);

}

getLast().push(k);

}



public Object pop()

{

if(stacks.size()!=0)

{

if(getLast().size() == 0)

{

stacks.remove(stacks.size()-1);

}

return getLast().pop();

}

else

{

return null;

}



}

//print all the element in the stacks;

void print()

{

for(int i = 0;i<stacks.size();++i)

{

Stack stack_temp = stacks.get(i);

for (Object x : stack_temp)

{

System.out.println(x);

}

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

wtfStack ws = new wtfStack();

Stack test_stack = new Stack();

for(int i = 0;i<20;++i)

{

ws.push(i);

test_stack.push(i);

}

System.out.println("for the advanced");

ws.print();

System.out.println("for the naieve");

for(Object x : test_stack)

{

System.out.println(x);

}

for(int j = 0;j<15;++j)

{

System.out.println("advan");

System.out.println(ws.pop());

System.out.println("normal");

System.out.println(test_stack.pop());

}

// ws.print();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: